Capistrano 3.0 for Rails app deployment

Capistrano is designed to run commands remotely. To do this we’ll need to specify a server, which we do by calling server. We need to pass this the address of a server and a role. We’ll explain roles later; for now we’ll just set this to web.

First you need to set the configurations. Common settings go to the `deploy.rb` and environment specific settings goes to either 'config/deploy/production.rb' or 'staging.rb'

group :development do
  gem 'capistrano', '~> 3.1'
  gem 'capistrano-rails', '~> 1.1'
$ bundle install
$ bundle exec cap install

Common settings go to ‘deploy.rb

The deploy.rb is a place where the configuration common to each environment can be specified, normally the repository URL and the user as whom to deploy are specified here.

# There should not be any space in application name
set :application, 'my_test_app'
set :repo_url, ''
set :scm, :git


Specific settings go to ‘staging.rb

# Need to set deployer user and server roles to begin
server '23.79.200.XXX', user: 'deployer', roles: [:web]

# `forward_agent: true,` lets the SSH agent to copy all SSH Keys to the
# server and use it on your behalf
set :ssh_options, {forward_agent: true}

set :branch, 'staging'
set :deploy_to, '/home/deployer/www/my_test_app'

# Never try like this; You run into permission issues
# set :deploy_to, 'www/my_test_app'

See Deploy flow (Total possible events)

You may also want to un-comment the following lines from Capfile. These libraries comprises tasks to restart passenger app server, pre-compiling/clearing assets on deployment, database schema migration etc.


require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/passenger'

Remote commands with SSH Kit

Capistrano executes commands on remote servers using SSHKit

An example setting a working directory, user and environment variable:

on roles(:app), in: :sequence, wait: 5 do
  within "/opt/sites/" do
    # commands in this block execute in the
    # directory: /opt/sites/
    as :deploy  do
      # commands in this block execute as the "deploy" user.
      with rails_env: :production do
        # commands in this block execute with the environment
        # variable RAILS_ENV=production
        rake   "assets:precompile"
        runner "S3::Sync.notify"

For more examples, see the file in the SSHKit project:



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s