Skip to content

Instantly share code, notes, and snippets.

@kumagi

kumagi/deploy.rb

Created Jun 22, 2017
Embed
What would you like to do?
# config valid only for current version of Capistrano
lock "3.8.2"
set :application, "my_application"
set :repo_url, "git@github.com:kumagi/my_application"
set :branch, 'master'
set :deploy_to, "/opt/my_application"
set :format, :airbrussh
set :format_options, command_output: true, log_file: "log/capistrano.log", color: :auto, truncate: :auto
set :keep_releases, 3
set :puma_threads, [2, 4]
set :puma_workers, 0
set :pty, true
set :use_sudo, false
set :deploy_via, :remote_cache
set :sidekiq_config, "#{current_path}/config/sidekiq.yml"
set :rails_env, 'production'
set :migration_role, [:web, :db, :app]
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa) }
server "my_server", roles: %w{app db web}, primary: true
set :conditionally_migrate, true
set :assets_roles, [:web, :app, :db]
set :rails_assets_groups, :assets
set :normalize_asset_timestamps, %w{public/images public/javascripts public/stylesheets}
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/puma.sock"
set :puma_default_control_app, "unix://#{shared_path}/tmp/sockets/pumactl.sock"
set :puma_conf, "#{shared_path}/puma.rb"
set :puma_access_log, "#{shared_path}/log/puma_access.log"
set :puma_error_log, "#{shared_path}/log/puma_error.log"
set :puma_role, :app
set :puma_env, fetch(:rack_env, fetch(:rails_env, 'production'))
set :puma_daemonize, true
set :puma_tag, fetch(:application)
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true
set :nginx_use_ssl, true
set :nginx_config_name, "my_application"
set :nginx_flags, 'fail_timeout=0'
set :nginx_http_flags, fetch(:nginx_flags)
set :nginx_server_name, "localhost #{fetch(:application)}.local"
set :nginx_sites_available_path, '/etc/nginx/sites-available'
set :nginx_sites_enabled_path, '/etc/nginx/sites-enabled'
set :nginx_socket_flags, fetch(:nginx_flags)
set :nginx_ssl_certificate, "/home/kumagi/keys/{fetch(:nginx_config_name)}.crt"
set :nginx_ssl_certificate_key, "/home/kumagi/keys/{fetch(:nginx_config_name)}.key"
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
before :restart, :make_dirs
end
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
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
task :restart_nginx do
on roles(:app) do
sudo "service nginx restart"
end
end
after :deploy, "deploy:restart_nginx"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment