45

How do I invoke one Capistrano task from another?

For example:

task :foo do
  # stuff
end

task :bar do
  # INVOKE :foo
end
weicool
  • 2,613
  • 5
  • 22
  • 19

5 Answers5

104

For the record: in the Capistrano 3, use invoke(), e.g.

desc "Task that does something"
task :do_something do
  invoke 'namespace:task'
end

More at https://github.com/capistrano/capistrano#before--after

notapatch
  • 6,569
  • 6
  • 41
  • 45
Ain Tohvri
  • 2,987
  • 6
  • 32
  • 51
34

You can do it by using namespace:

namespace :test do
  task :one do
  end
  task :two do
    test.one
    #or just directly call it:
    one
  end
end

Just be careful with the name you use to not overwrite some important function.

mpapis
  • 52,729
  • 14
  • 121
  • 158
5

Generally you do this by defining dependencies:

before :bar, :foo
tadman
  • 208,517
  • 23
  • 234
  • 262
2

you also could use Rake::Task["namespace:task"].invoke, this works

andi
  • 311
  • 1
  • 16
1

If the task lives in another namespace, you can use find_and_execute_task instead.

troelskn
  • 115,121
  • 27
  • 131
  • 155