2

My question is similar to How do I invoke one Capistrano task from another?

The extra thing I want is being able to pass parameters to bar when calling it from foo:

task :foo do
  # this calls bar, I would like to pass params (i.e n = 10)
  # as if I were calling cap bar -s n=10
  # bar does not take arguments
  bar
end

task :bar do
  if exists?(:n)
    puts "n is: #{n}"
  end
end
Community
  • 1
  • 1
Abdo
  • 13,549
  • 10
  • 79
  • 98

2 Answers2

3

In capistrano 3.x

desc "I accept a parameter"
task :foo, :foo_param do |t, args|
  foo_param = args[:foo_param]
  puts "I am #{foo_param}"
end

desc "I call the foo task"
task :bar do
  invoke("foo", "batman")
  # prints "I am batman"
end
rishabhmhjn
  • 1,079
  • 1
  • 8
  • 15
0

Capistrano tasks can't really be parameterized. You can define a helper method, as follows:

task :foo do
  bar(10)
end

def bar(n=variables[:n])
  puts "N is #{n}"  
end

If you're dead set on having :bar be a task as well, try this trick:

task :foo do
  bar(10)
end

task :bar { bar }

def bar(n=variables[:n])
  puts "N is #{n}"  
end

Note that the task MUST be declared before the method.

choover
  • 852
  • 7
  • 12