views:

65

answers:

2

I'm having some issues with variable scope with the capistrano-ext gem's multistage module. I currently have, in config/deploy/staging.rb.

set(:settings) { YAML.load_file("config/deploy.yml")['staging'] }

set :repository,  settings["repository"]
set :deploy_to,   settings["deploy_to"]
set :branch,      settings["branch"]
set :domain,      settings["domain"]
set :user,        settings["user"]

role :app, domain
role :web, domain
role :db,  domain, :primary => true

My config/deploy/production.rb file is similar. This doesn't seem very DRY. Ideally, I think I'd like everything to be in the deploy.rb file. If there were a variable set with the current stage, everything would be really clean.

UPDATE: I found a solution.

I defined this function in deploy.rb:

def set_settings(params)
  params.each_pair do |k,v|
    set k.to_sym, v
  end
  if exists? :domain
    role :app, domain
    role :web, domain
    role :db,  domain, :primary => true
  end
end

Then my staging.rb file is just set_settings(YAML.load_file("config/deploy.yml")['staging'])

A: 

try CAPDEV='staging' cap deploy and ENV['CAPDEV'] in deploy.rb

tomaszsobczak
There's no way to be able to just type `$ cap staging deploy` and have things work?
Eli
+1  A: 

You're making this overly complicated.

Just put your common code into your deploy.rb file:

role :app, domain
role :web, domain
role :db,  domain, :primary => true

and your stage-dependent settings in your config/deploy/staging.rb, production.rb, etc. files.

Then run cap deploy, like you said: cap staging deploy

Your stage.rb files can use common variables as well. For example, my staging file just has one line:

set :deploy_to, "/var/www/#{domain}_staging"

The rest is in deploy.rb

simianarmy
We've done this successfully as well
Tilendor