[英]Capistrano: How to share Capistrano tasks?
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? 例如,是否需要将它们移动到
/lib/capistrano/tasks
下的单独文件中? 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? 我的意思是文件扩展名是什么,以及如何为这些任务命名空间并通过
cap
命令访问它们?
A typical multistage configuration of Capistrano would have the following files: Capistrano的典型多阶段配置将具有以下文件:
Capfile
config/deploy.rb
config/deploy/staging.rb
config/deploy/production.rb
The tasks placed in staging.rb
or production.rb
are available only in those environments. 放置在
staging.rb
或production.rb
中的任务仅在那些环境中可用。
If you want some tasks accessible by any environment, you can place them in config/deploy.rb
. 如果希望某些任务可以在任何环境下访问,可以将它们放在
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
. 如果要将它们提取到一个单独的文件中,则就像您说的那样,可以将它们放在
lib/capistrano/tasks/*.rake
。 (Normally a Capfile
should contain a line to load those tasks.) (通常,
Capfile
应该包含一行以加载这些任务。)
For example, there is a customized task unicorn:restart
which restarts Unicorn. 例如,有一个自定义任务
unicorn:restart
,它将重新启动Unicorn。 We can create a file called lib/capistrano/tasks/unicorn.rake
, and add the following the file: 我们可以创建一个名为
lib/capistrano/tasks/unicorn.rake
的文件,并添加以下文件:
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
. 您可以
Capfile
添加到Capfile
,以将共享任务加载到lib/capistrano/tasks/*.rake
Capfile
下。
# Load custom tasks from `lib/capistrano/tasks' if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.