Lukas Oppermann
Lukas Oppermann

Reputation: 2938

Make Capistrano use alias on sever when running scripts

I have the following problem using Capistrano with laravel:

However, Capistrano (as far as I understand due to it starting in a very basic shell http://capistranorb.com/documentation/faq/why-does-something-work-in-my-ssh-session-but-not-in-capistrano/) does not load this alias, so I get an error from the composer scripts e.g. php artisan.

However, on my dev machine I need to keep it as php, since this is where php is here.

How can I solve this problem best? Any more info you need? Thanks.

Just in case it helps, this is how I call the script:

desc 'Composer install'
task :composer_install do
    on roles(:app), in: :groups, limit:1 do
        execute "/usr/local/bin/php5-56STABLE-CLI composer.phar install --working-dir #{fetch(:release_path)}"
        execute "cp #{fetch(:deploy_to)}/shared/.env #{fetch(:release_path)}/.env"
    end
end

Upvotes: 1

Views: 802

Answers (2)

Lukas Oppermann
Lukas Oppermann

Reputation: 2938

Okay, my current workaround is the following:

in your capistrano deploy.rb in the script that you execute at deploy update.

    desc 'Composer install'
    task :composer_install do
        on roles(:app), in: :groups, limit:1 do
            execute "/usr/local/bin/php5-56STABLE-CLI /path/to/composer.phar install --working-dir #{fetch(:release_path)} --no-scripts"
            execute "cd #{fetch(:release_path)} && /usr/local/bin/php5-56STABLE-CLI artisan clear-compiled"
            execute "cd #{fetch(:release_path)} && /usr/local/bin/php5-56STABLE-CLI artisan optimize"
        end
    end

end

after "deploy:updated", "deploy:composer_install"

I am not 100% sure if the artisan clear-compiled is needed. Anyway, those 2 are composer scripts that would normally be called via composer, but the --no-scripts flag keeps them from being called, so that it does not fail on install. When calling them from capistrano, I can easily change which php to use, as you can see.

However if anyone has a better solution, please let me know.

Upvotes: 0

Matt Brictson
Matt Brictson

Reputation: 11102

It sounds like your scenario is the perfect fit for Capistrano's "command map" feature, as documented here: https://github.com/capistrano/sshkit#the-command-map.

Here are the two main takeaways:

  1. Write your Capistrano execute commands so that the binary name (php) is a separate argument. This will allow it to be substituted using the command map. For example:

    execute :php, "composer.phar install --working-dir #{fetch(:release_path)}"
    
  2. In your Capistrano deployment config, tell the command map how to substitute the :php command, like this:

    SSHKit.config.command_map[:php] = "/usr/local/bin/php5-56STABLE-CLI"
    

If you want this substitution to affect all deployment environments, place it in deploy.rb. If it only applies to your production environment, then put it in production.rb.

Upvotes: 2

Related Questions