Sunday, July 1, 2007

Capistrano with multiple environments



A few months ago I started poking around with Capistrano and now cap is all I use everywhere… This thing rocks. The first problem I had was multiple environments, we have Production, Staging, performance, integration, qa1,qa2,stability1 and stability2 ?!?! dude can you say excessive.. anyways heres how I managed around this:

This goes in your deploy.rb file:

desc "Set the target stage to production"
task :production do
before “deploy”, “mysql:backup”
set :deploy_via, :rsync_with_remote_cache
set :stage, “#{current_task.name.to_s}”
role :web, “192.168.1.1″, :ssl => true
role :app, “192.168.1.2″
role :db, “192.168.1.3″, :primary => true
role :db, “192.168.1.4″, :no_release => true, :replica => true
role :db, “192.168.1.5″, :no_release => true, :replica => true
end
desc “Set the target stage to staging”
task :staging do
before “deploy”, “mysql:backup”
set :deploy_via, :rsync_with_remote_cache
set :stage, “#{current_task.name.to_s}”
role :web, “192.168.2.1″, :ssl => true
role :app, “192.168.2.2″
role :db, “192.168.2.3″, :primary => true
end

desc "Set the target stage to perf"
task :perf do
set :stage, “#{current_task.name.to_s}”
role :web, “192.168.3.1″, :ssl => true
role :app, “192.168.3.2″
role :db, “192.168.3.3″, :primary => true
end

I think this worked better than the multi-stage extension the was added to Capistrano. (It’s very similar except you put all the stages in their own files.)

Now when you call a task add the environment

cap production deploy:cold

I also have two different ways of deploying since the production servers are behind a firewall I added the :deploy_via in the task which overrides the deploy method and uses rsync for these hosts. (Btw there is a bug in the rsync code which I blogged about here)

No comments: