Capistrano 3 execute within a directory

若如初见. 提交于 2019-11-30 06:14:24

Smells like a Cap 3 bug.

I suggest just guaranteeing you are where you want to be from the shell perspective:

execute "cd '#{release_path}'; #{fetch(:composer_command)} install"

You can retain all the niceties of within(), with(), default_env, etc, while still keeping the natural string syntax:

within release_path do
  execute *%w[ pip install -r requirements.txt ]
end

A couple of tips:

1) Capistrano uses SSHKit for a lot of things, among which command execution. In order to simplify using Composer you could configure the command map (in deploy.rb or production.rb, etc), here are 2 examples:

SSHKit.config.command_map[:composer] = "#{shared_path.join('composer.phar')}"
SSHKit.config.command_map[:composer] = '/usr/bin/env composer.phar'

Next you can execute it like so:

execute :composer, :install

2) From a security perspective it's wise to disable the php setting allow_url_fopen, but unfortunately Composer needs it enabled to function. You can use this trick to leave it disabled globally:

SSHKit.config.command_map[:composer] = "/usr/bin/env php -d allow_url_fopen=On #{shared_path.join('composer.phar')}"

Check out iniscan for more security advise on php settings.

3) Composer has an option -d, --working-dir, which you can point to the directory containing the composer.json file in order to run Composer from any other directory. This should solve your problem:

execute :composer, '-d', release_path, :install

4) You may want to take a look at the capistrano-composer project :)

Actually, your use of the within function is almost correct. You have supplied it an entire string as a command, but the doc points out that this results in unreliable behaviour (which I have experienced myself).

Let the first argument to execute be a symbol instead of a string (which contains whitespace):

within release_path do
    execute fetch(:composer_command).to_sym, "install"
    execute :pwd
    execute :ls
end

just for reference here is the Capistrano Doc explaining why within {} does not work with arguments with whitespace. I hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!