10

Here is an tutorial how to pass parameters to an capistrano 3 task.

namespace :task do
  desc 'Execute the specific cmd task'
  task :invoke, :command do |task, args|
    on roles(:app) do
      execute :cmd, args[:command]
    end
  end
end

Can executed with:

$ cap staging "task:invoke[arg]"

How can i use this in my deploy.rb? The following does not work.

before :started, "task:invoke[arg]"
michamilz
  • 101
  • 1
  • 3

2 Answers2

4

Not sure about before/after, but with Capistrano 3 you can always use the rake syntax and call task from within another task:

Rake::Task["mynamespace:mytask"].invoke(arg)
Ivan Zamylin
  • 1,708
  • 2
  • 19
  • 35
3

You can use invoke method:

before :started, :second_param do
  invoke 'task:invoke', 'arg'
end

Also one thing which might be helpful, capistrano and rake allowing to execute task only first time, this might be common issue for task with parameters, cause you may reuse it with different value. To allow doing it you need to re-enable the task:

namespace :task do
  desc 'Execute the specific cmd task'
  task :invoke, :command do |task, args|
    on roles(:app) do
      execute :cmd, args[:command]
      task.reenable                # <--------- this how to re-enable it
    end
  end
end
Mateusz Moska
  • 1,921
  • 1
  • 14
  • 18
  • Using Capistrano v3 on your first example returns "wrong number of arguments (1 for 2+)". – marcovtwout Jan 29 '16 at 15:25
  • Can you show an example? "It was working on my machine" :) – Mateusz Moska Jan 31 '16 at 20:33
  • On latest Capistrano master, bottom of deploy.rb: ``` before "deploy:updated" do #command here end ``` – marcovtwout Feb 01 '16 at 08:12
  • 1
    Ok, I've checked that and you are right 'before' takes to parameters, but point is 'invoke' works fine. Check corrected answer. – Mateusz Moska Feb 08 '16 at 21:45
  • Thanks. What's still unclear to me, what is the meaning of that second parameter? The API docs call it the "prerequisite", and it seems to be used as this anonymous task's name: http://www.rubydoc.info/github/capistrano/capistrano/Capistrano/TaskEnhancements#before-instance_method – marcovtwout Feb 09 '16 at 08:33