Staging server with Capistrano

Update: This is old documentation; this can now be done in Capistrano itself.

Capistrano has made deploying to the production server quite easy and predictable. So easy that it was only last week that I realized what was missing: the staging server. Duh.

What I desired was something as easy as:

rake deploy_staging

rake deploy_production

So I set out to find it. I came close, but did not exactly find what I sought. So here’s how I did it:

lib/tasks/staging.rb:

namespace :remote do
  desc "A macro-task that updates the code, fixes the symlink, and restarts the application servers."
  task(:deploy_staging) { cap :deploy, '-S','stage=staging' }
  desc "A macro-task that updates the code, fixes the symlink, and restarts the application servers."
  task(:deploy_production) { cap :deploy, '-S','stage=production' }
end
desc "Push the latest revision into staging (delegates to remote:deploy_staging)"
task :deploy_staging => "remote:deploy_staging"
desc "Push the latest revision into production (delegates to remote:deploy_production)"
task :deploy_production => "remote:deploy_production"

config/deploy.rb:

# ...
if stage == "production"
  set :deploy_to, "/usr/www/#{application}"
else
  set :deploy_to, "/usr/www/#{application}_staging"
end

# ...
task :after_update_code do
  if stage == "production"
    run "ln -s #{release_path}/config/mongrel_cluster_production.yml #{release_path}/config/mongrel_cluster.yml"
  else
    run "ln -s #{release_path}/config/mongrel_cluster_staging.yml #{release_path}/config/mongrel_cluster.yml"
  end
end

The after_update_code callback takes care of the Mongrel::Cluster configuration. I also added a staging database to config/database.yml, identical to the development database configuration.

Advertisements
%d bloggers like this: