Upgrading to Capistrano 3 With Rails, Sidekiq and Whenever

1. Start

Gemfile

1
gem 'capistrano', group: :development

1
bundle exec cap install

deploy.rb

1
2
3
4
5
6
7
8
9
10
11
set :scm, :git
set :repo_url, 'git_repo'
set :branch, 'develop'
set :repo_subdir, "your_subdir"
set :deploy_to, '/var/www/rails_apps/xxx'

after 'deploy:set_current_revision', :checkout_repo_subdir do
  on roles(:app) do
    execute "rm -rf /tmp/#{fetch(:repo_subdir)} && mv #{release_path}/#{fetch(:repo_subdir)}/ /tmp && rm -rf #{release_path}/* && mv /tmp/#{fetch(:repo_subdir)}/* #{release_path}"
  end
end

2. RVM

Gemfile

1
gem 'capistrano-rvm', group: :development

1
2
set :rvm_type, :auto
set :rvm_ruby_version, '2.0.0-p481'

3. Rails

Gemfile

1
2
3
4
group :development do
  gem 'capistrano-bundler'
  gem 'capistrano-rails'
end

Capify

1
2
3
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'

deploy.rb

1
2
3
4
5
6
7
set :linkeddirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}

set :keepreleases, 5

namespace :deploy do
  after :finishing, 'deploy:cleanup'
end

4. Whenever

Capify

1
require "whenever/capistrano"

If we set whenever_roles to [:db, :app] in deploy.rb, and have the following jobs in schedule.rb:

1
2
3
4
5
6
7
8
9
10
11
every :day, :at => '1:37pm', :roles => [:app] do
  rake 'app:task' # will only be added to crontabs of :app servers
end

every :hour, :roles => [:db] do
  rake 'db:task' # will only be added to crontabs of :db servers
end

every :day, :at => '12:02am' do
  command "run_this_everywhere" # will be deployed to :db and :app servers
end

Here are the basic rules:

If a server's role isn't listed in wheneverroles, it will never have jobs added to its crontab. If a server's role is listed in the wheneverroles, then it will have all jobs added to its crontab that either list that role in their :roles arg or that don't have a :roles arg. If a job has a :roles arg but that role isn't in the whenever_roles list, that job will not be deployed to any server.

5. Sidekiq

Gemfile

1
gem 'capistrano-sidekiq' , group: :development

Capify

1
require 'capistrano/sidekiq'

6. Go

1
cap production deploy

7. References

  1. https://github.com/capistrano/capistrano
  2. http://capistranorb.com/
  3. http://stackoverflow.com/questions/18838930/how-to-run-shell-commands-on-server-in-capistrano-v3
  4. https://github.com/javan/whenever