Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass arguments to Capistrano 3 tasks in deploy.rb

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]"
like image 494
michamilz Avatar asked Feb 05 '14 22:02

michamilz


People also ask

How does Capistrano work?

How does Capistrano work? Capistrano is a framework written in Ruby that provides automated deploy scripts. It connects to your web server via SSH and executes a bunch of tasks that will make your app ready to shine.

How do I run Capfile?

Navigate to your application's root directory in Terminal and run the following command: capify . This command creates a special file called Capfile in your project, and adds a template deployment recipe at config/deploy.


2 Answers

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)
like image 183
Ivan Zamylin Avatar answered Sep 19 '22 03:09

Ivan Zamylin


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
like image 32
Mateusz Moska Avatar answered Sep 19 '22 03:09

Mateusz Moska