Summary:
- Add Capistrano and other relevant gems to Gemfile.
# Deployment
gem 'capistrano', '< 5'
gem 'capistrano-rvm'
gem 'capistrano-rails'
gem 'capistrano-bundler'
gem 'capistrano3-puma', '< 5'
gem 'capistrano3-nginx'
gem 'capistrano-rails-console'
gem 'capistrano-rails-tail-log'
gem 'capistrano-rails-db'
gem 'capistrano-rake', require: false
Make sure to add these gems in development group in your Gemfile.
Refer to individual gem instruction on how to install.
Your Capfile
should have this:
# Load DSL and set up stages
require 'capistrano/setup'
# Include default deployment tasks
require 'capistrano/deploy'
# Includes tasks from other gems included in your Gemfile
require 'capistrano/scm/git'
require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'
require 'capistrano/nginx'
require 'capistrano/rails/console'
require 'capistrano/rails_tail_log'
require 'capistrano/rails/db'
require 'capistrano/rake'
# Load the SCM plugin appropriate to your project:
install_plugin Capistrano::SCM::Git
install_plugin Capistrano::Puma
install_plugin Capistrano::Puma::Nginx
# install_plugin Capistrano::Sidekiq
# install_plugin Capistrano::Sidekiq::Systemd
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
Your deploy.rb
should have something like this:
# config valid for current version and patch releases of Capistrano
lock '~> 3.17.0'
# Change these
set :repo_url, [URL_REPO_IN_SSH]
set :user, [USER]
# Don't change these unless you know what you're doing
set :pty, false
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa) }
set :nginx_use_ssl, true
## Defaults:
set :format, :pretty
set :log_level, :debug
set :keep_releases, 5
## Linked Files & Directories (Default None):
set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml', 'config/puma.rb', 'config/master.key', 'sockets/puma.sock')
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/uploads', 'sockets')
# Skip Sprockets build in favour of Webpacker.
Rake::Task['deploy:assets:backup_manifest'].clear_actions
# SSHKit.config.command_map[:sidekiq] = 'bundle exec sidekiq'
# SSHKit.config.command_map[:sidekiqctl] = 'bundle exec sidekiqctl'
namespace :deploy do
after 'deploy:restart', 'deploy:cleanup'
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
And your deploy/[ENVIRONMENT].rb
file should be similar to this:
set :stage, [STAGE]
set :rails_env, [RAILS_ENVIRONMENT]
set :branch, [BRANCH_YOU_WANT_TO_TARGET]
set :application, [APPLICATION_NAME]
set :deploy_to, "/home/#{fetch(:user)}/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/sockets/puma.sock"
server [IP ADDRESS], user: fetch(:user), roles: [:web, :app, :db], primary: true
- Run
cap [stage] deploy:check
and just follow the errors. - When everything looks good, try deploy by typing:
cap [stage] deploy
. - Next, configure Nginx setting for the application. Ask Google to help you with this.
- Do not forget to restart Nginx everytime you make changes to the conf file.
- Check the logs (app log, puma log, Nginx log) for errors.