设置 :deploy_to 来自 Capistrano3 中的服务器配置

Setting :deploy_to from server config in Capistrano3

在我的 Capistrano 3 部署中,我想设置 set :deploy_to, -> { "/srv/www/#{fetch(:application)}" } 因此 :deploy_to 对于它部署到的每个服务器都是不同的。

在我的 staging.rb 文件中我有:

server 'dev.myserver.com', user: 'deploy', roles: %w{web app db}, install_path: 'mycustom/path'
server 'dev.myserver2.com', user: 'deploy', roles: %w{web app db}, install_path: 'mycustom/other/path'

我的问题是:是否可以在我的 :deploy_to 中使用我定义的 "install_path"?如果可以的话,你会怎么做?

最后,环顾四周,我发现了 Capistrano 的一位开发人员提出的问题,他明确指出 无法完成

引自Github issue

Not possible, sorry. fetch() (as is documented widely) reads values set by set(), the only reason to use set() and fetch() over regular ruby variables is to provide a consistent API between plugins and extensions, and because set() can take a Proc to be resolved later.

The variables you are setting in the host object via the server() command belong to an individual host, some of them, user, roles, etc have special meanings. For more information see https://github.com/capistrano/sshkit/blob/master/EXAMPLES.md#do-something-different-on-one-host-or-another-depending-on-a-host-property.

If you specifically need to deploy to a different directory on each machine you probably should not be using the built-in tasks (they don't fit your needs), and rather copy the deploy.rake from the Gem into your own project, and modify it as you need. Which in this case might be to not take fetch(:deploy_to), but to read that from a host property.

You could try to do something where before doing anything that relies on calling fetch(:deploy_to), you set() it using the value from host.someproperty but I'm pretty sure that'll break in exciting and interesting ways.