I have some shared tasks between my staging and production deploy scripts. What is the best practice of writing shared tasks? Do I need to move them to a separate file under /lib/capistrano/tasks for example? And if so how would I do that? I mean what will be the file extension and how to namespace those tasks and access them through cap command?
A typical multistage configuration of Capistrano would have the following files:
Capfileconfig/deploy.rbconfig/deploy/staging.rbconfig/deploy/production.rbThe tasks placed in staging.rb or production.rb are available only in those environments.
If you want some tasks accessible by any environment, you can place them in config/deploy.rb.
If you want to extract them into a separate file, then just like you said, you can place them in lib/capistrano/tasks/*.rake. (Normally a Capfile should contain a line to load those tasks.)
For example, there is a customized task unicorn:restart which restarts Unicorn. We can create a file called lib/capistrano/tasks/unicorn.rake, and add the following the file:
namespace :unicorn do
desc "Restart Unicorn"
task :restart do
# ...
end
end
EDIT
You can add the following line to your Capfile, to load the shared tasks under lib/capistrano/tasks/*.rake.
# Load custom tasks from `lib/capistrano/tasks' if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }