203 Capistrano: Configure Capistrano

Posted Over 5 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
set :repo_url, 'YOUR_PROJECTS_GIT_REPO_URL'

# 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 'app01-test.demo.makandra.de', user: 'deploy-cap-demo_s', roles: %w(app web cron db) # first is primary
server 'app02-test.demo.makandra.de', 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 'app01-test.demo.makandra.de', user: 'deploy-cap-demo_s', roles: %w(app web cron db) # first is primary
server 'app02-test.demo.makandra.de', 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
Almost 2 years ago
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Marius Schuller to opscomplete (2018-12-13 15:10)