简体   繁体   English

禁用Capistrano3的钩子?

[英]Disable hooks for Capistrano3?

Is there some way to disable some before / after hooks in Capistrano3? 有什么方法可以在Capistrano3的钩子before / after禁用某些功能?

For example, I want to deploy the code for my (Rails) application, but I don't want to start the application on the first deploy. 例如,我想为我的(Rails)应用程序部署代码,但是我不想在第一次部署时启动该应用程序。 (Maybe this is not "ideal", but there are a number of reasons I may not want to do so.) (也许这不是“理想的”,但是出于多种原因,我可能不想这样做。)

I'm aware of the following options: 我知道以下选项:

  1. Disable the tasks I don't want to run as part of my deploy.rb file, eg, Rake::Task['deploy:compile_assets'].clear_actions . 禁用我不想在deploy.rb文件中运行的任务,例如Rake::Task['deploy:compile_assets'].clear_actions
  2. Remove the appropriate require statements from my Capfile , eg, # require 'capistrano/rails/assets' . 从我的Capfile删除适当的require语句,例如, # require 'capistrano/rails/assets' Capfile # require 'capistrano/rails/assets'

What I would like is to be able to call a specific task OR pass some command line flags to cap [stage] deploy . 我想要的是能够调用特定任务或传递一些命令行标志来cap [stage] deploy What I want to do is approximately the same as deploy:setup , but that task is not available in Capistrano3. 我要执行的操作与deploy:setup大致相同,但是Capistrano3中没有该任务。

What is the best way to do this? 做这个的最好方式是什么?

Option 1: Disable require statements with ENV switch 选项1:使用ENV开关禁用require语句

As you already mentioned, removing the require statements is the easiest way to get the result you are looking for. 如前所述,删除require语句是获得所需结果的最简单方法。 To be able to control this on the command line, I would use a ENV option, like this: 为了能够在命令行上控制它,我将使用ENV选项,如下所示:

# In Capfile
# Include tasks from other gems included in your Gemfile
unless ENV["CAP_DISABLE_PLUGINS"]
  require "capistrano/bundler"
  require "capistrano/rails"
  # etc.
end

Then run: 然后运行:

CAP_DISABLE_PLUGINS=1 cap production deploy

Option 2: Define a custom task 选项2:定义自定义任务

For more fine-grained control over what tasks are run, define a custom task. 要对运行哪些任务进行更细粒度的控制,请定义一个自定义任务。

If you run cap [stage] deploy with the --trace option, you will see debug output of all the tasks that are being invoked. 如果使用--trace选项运行cap [stage] deploy ,则将看到正在调用的所有任务的调试输出。 Review that output and decide which of those tasks you need, and which you want to skip. 查看该输出,并确定您需要哪些任务以及要跳过哪些任务。 Then assemble your choices into a custom task. 然后将您的选择组合到一个自定义任务中。

For example, this should deploy your code and symlink it to current , but skip running bundler , compiling assets, or migrating the database: 例如,这应该部署您的代码并将其符号链接到current ,但是跳过运行bundler ,编译资产或迁移数据库:

# Place this in config/deploy.rb
namespace :deploy do
  task :setup do
    invoke "deploy:check"
    invoke "deploy:new_release_path"
    invoke "git:create_release"
    invoke "deploy:set_current_revision"
    invoke "deploy:symlink:shared"
    invoke "deploy:symlink:release"
    invoke "deploy:log_revision"
  end
end

Then just run: 然后运行:

cap production deploy:setup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM