How to add tasks to Capistrano
I wrote in post Capistrano – PHP, deploy without stressful how to install and make first deploy. In this post I want show you how to add tasks to capistrano.
When we send new code version to test or production server we need often to run some script to upload new database schema or update vendor folder with composer. That all can make Capistrano automatically. You need to add some script to config file:
namespace :deploy do desc "Build"; after :updated, :build do on roles(:web) do within release_path do execute 'composer', 'install --no-dev --quiet' execute 'php', 'vendor/bin/phinx', 'migrate', '-e', 'testing', raise_on_non_zero_exit: false end end end end
Capistrano after update execute two scripts: composer install and phinx to migrate database. If you need you can add another tasks for example delete cache or run tests scripts.
You can run hooks before update. More about hooks:
PHP is on my hosting not install in the standard place and I can’t run composer:
cap aborted! SSHKit::Runner::ExecuteError: Exception while executing as bsosna: composer exit status: 127 composer stdout: Nothing written composer stderr: /usr/bin/env: php: No such file or directory
The solution is simple. If you have this error you must add php path to composer command:
SSHKit.config.command_map[:composer] = "/usr/local/YOUR_PHP_PATH/bin/php /usr/local/bin/composer"