2

I am using Capistrano v2.9.0.

I run this command:

cap deploy:tryout -S testvar=thing

and my deploy.rb contains this:

namespace :deploy do
    task :tryout do
        if defined? testvar
            puts "param: #{testvar}\n"
        else
            puts "no branch!\n"
        end
    end
end

The output is "no branch!". How do I pass values from the command line? I tried looking into the code, and I can see options.rb where it adds the passed parameter to options[:pre_vars], but that seems to be an instance variable, and I can't figure out how to access it from my deploy script.

Benubird
  • 18,551
  • 27
  • 90
  • 141
  • Is the `-S` a typo? it should be `-s` (lowercase) – Kashyap Feb 05 '14 at 12:22
  • 1
    No, according to the cap docs, lowercase is --set, uppercase is --set-before. I've tried it with both of them, anyway. – Benubird Feb 05 '14 at 12:25
  • Duplicate of: http://stackoverflow.com/questions/10216841/passing-parameters-to-capistrano -- Also, testing variable existence looks like this: https://github.com/capistrano/capistrano/wiki/2.x-DSL-Configuration-Variables-Exists -- I believe vars set on the command line are tested the same way. Try putting your var in the else and see what happens. – juanitogan Oct 30 '14 at 18:47

1 Answers1

0

Solution:

The options can be accessed via @parent.variables hash, so if the command line string is testvar=thing, then @parent.variables[:testvar] has the value string.

This seems really ugly and hacky, but it works.

Edit: Turns out it is also available locally via variables[:testvar]

Benubird
  • 18,551
  • 27
  • 90
  • 141