Capistrano: Configure Capistrano

Posted about 4 years ago. Visible to the public.

If you followed this guide, you will have already executed bundle exec cap install STAGES=staging,production and your project directory will look something like this:

├── Capfile
├── config
│   ├── deploy
│   │   ├── production.rb
│   │   └── staging.rb
│   └── deploy.rb
├── Gemfile
└── lib
    └── capistrano
        └── tasks

Starting from there, we at makandra use the following changes to the config/deploy.rb file ourselves:

# config/deploy.rb

abort 'You must run this using "bundle exec ..."' unless ENV['BUNDLE_BIN_PATH'] || ENV['BUNDLE_GEMFILE']

set :application, 'YOUR_APPLICATION_NAME'
set :scm, :git

# Default value for :log_level is :debug
set :log_level, :info # %i(debug info error), default: :debug

# Default value for :linked_files is []
set :linked_files, %w(config/database.yml config/secrets.yml)

# Default value for linked_dirs is []
set :linked_dirs, %w(log public/system tmp/pids)

# Default value for keep_releases is 5
set :keep_releases, 10

set :ssh_options, {
  forward_agent: true

The config/deploy/staging.rb and config/deploy/production.rb can be quite similar. For staging we write something like this:

# config/deploy/staging.rb

set :stage, :staging

set :deploy_to, 'YOUR_SERVER_DEPLOY_PATH'
set :rails_env, 'staging'
set :branch, ENV['DEPLOY_BRANCH'] || 'master'

server '', user: 'deploy-cap-demo_s', roles: %w(app web cron db) # first is primary
server '', user: 'deploy-cap-demo_s', roles: %w(app web)

Almost identical would be the example for production:

# config/deploy/production.rb

set :stage, :production

set :deploy_to, 'YOUR_SERVER_DEPLOY_PATH'
set :rails_env, 'production'
set :branch, 'production'

server '', user: 'deploy-cap-demo_s', roles: %w(app web cron db) # first is primary
server '', user: 'deploy-cap-demo_s', roles: %w(app web)

Usually you will need for a default rails application the following additions to your Capfile:

# Capfile

require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
Marius Schuller
Last edit
10 months ago
Henning Koch
About this deck
We are makandra and do test-driven, agile Ruby on Rails software development.
Posted by Marius Schuller to opscomplete (2018-12-13 16:10)