Search code examples
deploymentcapistranocapistrano3

Setting :deploy_to from server config in Capistrano3


In my Capistrano 3 deployment, I would like to set the set :deploy_to, -> { "/srv/www/#{fetch(:application)}" } so the :deploy_to is different for each server it deploys to.

In my staging.rb file I have:

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'

My question is: would it possible to use the "install_path" I defined, in my :deploy_to? If that's possible, how would you do it?


Solution

  • Finally, after looking around, I came onto an issue from one of the developer of Capistrano, stating specifically that it can't be done

    Quote from the 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.