I am learning to love capistrano–it’s a fantastic deployment system for remote server management.  I’m even learning enough ruby to be dangerous.

One of the issues I ran into was I wanted to set a variable in one task and use it in another (or, more likely, in more than one other task).  I couldn’t find any examples of how to do this online, so here’s how I did it:

task :set_var
self[:myvar]= localvar

task :read_var
puts self[:myvar]

Note that myvar and localvar need to be different identifiers–“local variables take precedence”.  Also, the variable can be anything, I think.  I use this method to create an array in one task, then iterate over it in another.

[tags]capistrano, remote deployment, ruby newbie[/tags]

2 thoughts on “Setting variables across tasks in capistrano

  1. Stephen Benner says:

    I know this is old, but for whatever benefit it may serve others, there are two official ways of addressing this problem I’d like to point out:

    First, Capistrano variable access just as you describe, that is, ‘globally’, via methods ‘set’ and ‘fetch’. See this well-written post for more on that: http://benediktdeicke.com/2013/02/what-you-did-not-know-about-capistrano-yet/

    Secondly, some people don’t realize this but since Capistrano is built using Rake, you can pass arguments to tasks as you would in Rake. See this detailed post for more info: http://viget.com/extend/protip-passing-parameters-to-your-rake-tasks

  2. moore says:

    Thanks for the extra information, Stephen.

Comments are closed.

© Moore Consulting, 2003-2021