如何在Capistrano v3中在服务器上运行shell命令?

74

我是 Capistrano 的新手,我尝试使用 Capistrano 的 DSL 在服务器上运行 shell 命令('run'、'execute' 等),但似乎已经被弃用。在反复搜索了许多功能等效的选项后,我还是不知所措。

当前代码:

desc 'Do something'
task :do_something
  execute 'echo sometext'
end

输出:

    cap aborted!
    undefined method `execute' for main:Object
    /Users/Justin/Dropbox/xxxx/xxxx/xxxx/Capfile:45:in `block (2 levels) in <top (required)>'
    /Users/Justin/.rvm/gems/ruby-2.0.0-p247/bundler/gems/capistrano-2dc1627838f9/lib/capistrano/application.rb:12:in `run'
    /Users/Justin/.rvm/gems/ruby-2.0.0-p247/bundler/gems/capistrano-2dc1627838f9/bin/cap:3:in `<top (required)>'
    /Users/Justin/.rvm/gems/ruby-2.0.0-p247/bin/cap:23:in `load'
    /Users/Justin/.rvm/gems/ruby-2.0.0-p247/bin/cap:23:in `<main>'
    /Users/Justin/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `eval'
    /Users/Justin/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `<main>'
    Tasks: TOP => deploy:do_something

我遇到了与“info”和“error”方法完全相同的问题——这是因为这些方法属于SSHKit,必须在SSHKit块中使用。 - Dave Burt
1个回答

118
在 Capistrano v3 中,您必须通过使用主机名列表调用 on 来指定要运行代码的位置,例如:
task :execute_on_server do
  on "root@example.com" do
    execute "some_command"
  end
end
如果您已经设置了角色,则可以使用roles方法以方便的方式:
role :mailserver, "root@mail.example.com"

task :check_mail do
  on roles(:mailserver) do
    execute "some_command"
  end
end

这里有一些v3的文档: http://www.capistranorb.com/


8
太棒了,我希望在步骤说明中能更加清晰明确。他们只使用测试、信息等词汇。 - Justin Godesky
2
Capistrano的execute方法依赖于sshkit中的实现。目前,您可以在此处找到有关execute的更多信息:https://github.com/leehambley/sshkit Capistrano 3文档仍然不完整。 - Tombart
@KitHo 是的,你可以使用sudo,就像手动ssh连接一样。 - Benubird
3
当我遇到infoerror方法的相同问题时,我使用了run_locally { ... }而不是on ... { ... }。这样可以让你进入SSHKit上下文而不连接到远程主机。 - Dave Burt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接