Skip to content

Instantly share code, notes, and snippets.

@charusat09
Last active July 31, 2017 06:32
Show Gist options
  • Save charusat09/18a2055bf7b82a6184e8494a7dd9a1e8 to your computer and use it in GitHub Desktop.
Save charusat09/18a2055bf7b82a6184e8494a7dd9a1e8 to your computer and use it in GitHub Desktop.
# Gemfile
gem 'capistrano', require: false
gem 'capistrano-rvm', require: false
gem 'capistrano-rails', require: false
gem 'capistrano-bundler', require: false
gem 'capistrano3-puma', require: false
# Capfile
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'
install_plugin Capistrano::Puma
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
# config/deploy.rb
lock "3.8.1"
set :repo_url, 'git@github.com:xxxx.git'
set :application, 'abcd'
set :user, 'ubuntu'
set :puma_threads, [4, 8]
set :puma_workers, 1
set :pem_file, %w(/home/mayur/Documents/project\ docs/abcd.pem)
set :assets_roles, [:web, :app]
set :pty, true
set :use_sudo, false
set :deploy_via, :remote_cache
set :deploy_to, "/var/www/apps/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/sockets/puma.sock"
set :puma_state, "#{shared_path}/pids/puma.state"
set :puma_pid, "#{shared_path}/pids/puma.pid"
set :puma_access_log, "#{shared_path}/log/puma.stdout.log"
set :puma_error_log, "#{shared_path}/log/puma.stderr.log"
set :ssh_options, {
forward_agent: true,
user: fetch(:user),
auth_methods: ["publickey"],
keys: fetch(:pem_file),
keepalive: true
}
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/sockets -p"
execute "mkdir #{shared_path}/pids -p"
execute "mkdir #{shared_path}/log -p"
end
end
before :start, :make_dirs
end
# config/deploy/staging.rb
server '0.0.0.0', port: 22, roles: [:web, :app, :db], primary: true
set :branch, :dev
set :stage, :staging
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/dev`
puts "WARNING: HEAD is not the same as origin/dev"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'puma:restart'
end
end
desc "Link shared files"
task :symlink_config_files do
on roles(:app) do
execute "ln -s #{shared_path}/config/application.yml #{release_path}/config/application.yml"
end
end
before :starting, :check_revision
after 'deploy:symlink:linked_dirs', 'symlink_config_files'
after :finishing, :cleanup
after :finishing, :restart
end
# config/deploy/production.rb
server '1.1.1.1', port: 22, roles: [:web, :app, :db], primary: true
set :branch, :master
set :stage, :production
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment