Capistrano 3 execute within a directory Capistrano 3 execute within a directory ruby-on-rails ruby-on-rails

Capistrano 3 execute within a directory


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 :)