free software, web technologies and heavy metal coding (and ruby of course)

sinatra on capistrano

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')}"
  end
end

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")
  end
end

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

The whole capistrano configuration file is on github.