Abdo
Abdo

Reputation: 14051

Capistrano task from another task with parameters

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

Upvotes: 2

Views: 1283

Answers (2)

rishabhmhjn
rishabhmhjn

Reputation: 1079

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

Upvotes: 3

choover
choover

Reputation: 852

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.

Upvotes: 0

Related Questions