Set custom variable in capistrano 3

Anton Sergeyev picture Anton Sergeyev · Nov 6, 2013 · Viewed 12.2k times · Source

I was using capistrano 2 with my php project, where I used to define custom variables like this:

set :app_environment, "test"

And then I accessed it in my deploy tasks, e.g.:

run "echo '#{app_environment}' > #{releases_path}/#{release_name}/protected/config/mode.php"

Now in capistrano 3 I define this variable in my stage-specific config files. But I don't have them defined in my tasks:

undefined local variable or method `app_environment' for #<SSHKit::Backend::Netssh:0x007f92323d6988> config/deploy.rb:28:in `block (3 levels) in <top (required)>'

Unfortunately there's not much documentation on the newest version of capistrano and I'm not quite familiar with ruby, so I don't see the way how to do that properly.

Answer

Lee Hambley picture Lee Hambley · Nov 6, 2013

The code should read:

run "echo '#{fetch(:app_environment)}' > #{releases_path}/#{fetch(:release_name)}/protected/config/mode.php"

Although even that is incorrect as run() doesn't exist in Cap3, it's now execute(), so:

execute "echo '#{fetch(:app_environment)}' > #{releases_path}/#{fetch(:release_name)}/protected/config/mode.php"

Beware constructing your command like this nothing will use the command map, or respect the within(), as() or with() constructs.