Capistrano 3 - 在多个角色中执行两次任务

Capistrano 3 - task being executed twice when in multiple roles

在我的 deploy.rb 中,我有一些这样的自定义任务:

namespace :deploy do
  desc 'setup environment'
  task :env do
    on roles(:web) do
      invoke 'symlink:env_file'
      invoke 'config:foreman'
    end
  end

  before  'assets:precompile', 'env'
end

我的服务器定义如下所示:

server "my.app.ip", roles: %w{app web}, primary: true
server "my.resque.server.ip", roles: %w{resque web}

基本上,我的 'app' 服务器负责 运行 主 rails 应用程序,而我的响应框 运行 负责后台作业。但他们都有网络角色,因为他们都 configure/run nginx + foreman 通过我自己的自定义挂钩。

当我 运行 我的 cap deploy 命令时,我一直 运行ning 出现这样的错误:

Skipping task `symlink:env_file'.
Capistrano tasks may only be invoked once. Since task `symlink:env_file' was previously invoked, invoke("symlink:env_file") at config/deploy.rb:133 will be skipped.
If you really meant to run this task again, first call Rake::Task["symlink:env_file"].reenable

我真的只希望这些命令对每个服务器 运行 一次。基于任务中的角色定义,我的期望是这些任务将针对我的服务器组中的每个 'web' 服务器调用一次。为什么它会执行多次?

Capistrano 并非设计用于 运行 on 块中的任意任务。唯一应该放在 on 块中的是 SSH-execution 命令,例如:

  • test
  • execute
  • capture
  • upload!
  • download!

永远不要在 on 块中使用 invoke

在您的例子中,on 块在每个 :web 服务器 上执行一次 ,这意味着 invoke 肯定会被多次调用。

相反,为什么不对这两个任务使用 before 声明?

before 'assets:precompile', 'deploy:symlink:env_file'
before 'assets:precompile', 'deploy:config:foreman'