Capistrano – Multiple Environments

Capistrano is designed to deploy your Rails application (yes it can handle others, but more work is required) to production.  However, most applications need to be deployed to more than one environment.  For example, we deploy to a staging environment in addition to production.  This lets select users and internal employees kick the tires before we release it in the wild.  After many complaints, capistrano-ext was born.  I am all in favor of using plug ins, but this is such a trivial issue, it’s easier to add the code than worry about the plugin.

set :stage, "staging" unless variables[:stage]
set :deploy_to, "/var/www/#{application}"
 
case stage
when "staging"
  set :rails_env, "staging"
  role :web, "staging.example.com"
  role :app, "staging.example.com"
  role :db"staging.example.com", :primary => true
when "production"
  set :rails_env, "production"
  role :web, "production.example.com"
  role :app, "production.example.com"
  role :db,  "production.example.com", :primary => true
end

To deploy to the correct environment call capistrano:

$ cap deploy -S stage=staging

If your staging and production share the same machine (not the best idea):

set :stage, "staging" unless variables[:stage]
server "example.com", :app, :web: db
 
case stage
when "staging"
  set :rails_env, "staging"
  set :deploy_to, "/var/www/staging-#{application}"
when "production"
  set :rails_env, "production"
  set :deploy_to, "/var/www/production-#{application}"
end
This entry was posted in Technology and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">