sinatra on capistrano

posted by alexis reigel on january 31, 2012

Capistrano is really great for deployment. The documentation however is somewhat scattered. The capistrano wiki has a more or less useful From The Beginning instruction and a Getting Started that doesn’t quite get you started.
However, the instructions on github are actually a very good starting point. Neverthelesse I want to show my sample here that has two additions to the one in the github help.

  1. Multistage support (i.e. staging and production server)
  2. Custom task to upload a config file

For further reading I recommend the Capistrano Handbook.

I assume that you’ve setup capistrano as described in the github help. The one thing I suggest is to ignore the part where the password is stored in the file. I highly advise to use ssh agent forwarding instead.

ssh_options[:forward_agent] = true # use local ssh key
set :user, "deployer"

Let’s setup multistage:

require 'capistrano/ext/multistage'
set :stages, %w(production staging)
set :default_stage, "staging"

Also, on most deployment server you may not have rights to sudo:

set :use_sudo, false

As I’m assuming you’re using passenger mod_rails:

namespace :deploy do
  task :start do ; end
  task :stop do ; end
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"

The last part uploads our configuration file to the server. This configuration contains user and password for the database login. We don’t store that in the source code repository.

namespace :config do
  task :db do
    upload('db.yml', "#{deploy_to}/current/db.yml")

before "deploy:restart", "config:db"

The whole capistrano configuration file is on github.

commented by RonaldOr on march 02, 2016

Well written!
I will right away clutch your rss feed as I can’t to find your
email subscription link or e-newsletter service.
Do you’ve any? Kindly let me know so that I could subscribe.

commented by Pesek on october 15, 2015

Another alternative is to use ssh foawrrding for the server to access the repository. Simply set the following in your deploy.rb:set :ssh_options, { :forward_agent => true }then make sure you’ve run ssh-add locally before running th deploy.

commented by natabolestr on february 24, 2016

Very well written!
I’ll immediately seize your rss as I can’t in finding your
e-mail subscription link or e-newsletter service.
Do you have any? Please permit me know in order that I may just subscribe.