任务

server 'example.com', roles: [:web, :app]
server 'example.org', roles: [:db, :workers]
desc "Report Uptimes"
task :uptime do
  on roles(:all) do |host|
    execute :any_command, "with args", :here, "and here"
    info "Host #{host} (#{host.roles.to_a.join(', ')}):\t#{capture(:uptime)}"
  end
end

注意:

tl;dr: execute(:bundle, :install)execute('bundle install') 的行为并不完全相同!

execute() 具有微妙的行为。例如,当调用 within './directory' { execute(:bundle, :install) } 时,execute() 的第一个参数是 字符串型,并且 没有空格。这允许命令通过 SSHKit::CommandMap 传递,从而启用许多强大的功能。

execute() 的第一个参数包含空格时,例如 within './directory' { execute('bundle install') }(或使用 heredoc 时),Capistrano 和 SSHKit 都无法可靠地预测它应该如何进行 shell 转义,因此无法执行任何上下文或命令映射,这意味着 within(){}(以及 with()as() 等)没有任何效果。已经有一些尝试来解决这个问题,但我们不认为这是一个错误,尽管我们承认它可能有点违反直觉。

Fork me on GitHub