How to run composer commands on DeployHQ
DeployHQ is a great service for automating the deployment of code from source code repositories onto SSH, SFTP or cloud servers.
When deploying applications which use Composer for dependency management, you often want to run Composer commands after deployment to install or update those dependencies.
Fortunately, Deploy makes it easy to run SSH commands before and/or after deployment.
For example, to run composer install after deployment, you can set up the following:
1. Log in to Deploy and view your deployment for the application
2. Go to Settings > SSH Commands
2. We want to run composer after files have been deployed to the server, so click the “Add Command” button at the button below the “After changes have been uploaded” section
3. Give your command a descriptive name, then enter the following in the “Command” text box to run composer after deployment:
composer install -d %path%
Note that we need to specify the -d option (aka –working-dir), to specify the directory where the composer command should be executed from, otherwise it will incorrectly attempt to execute the command in the home directory of the SSH user used for the deployment, rather than from the path we are deploying to.
The %path% part of the command is a variable supplied by Deploy, which in this case matches the deployment path specified in the settings.
UPDATE: Note that if you are deploying to a production server, you should consider using the following options:
composer install --no-dev --optimize-autoloader --working-dir %path%
… where –no-dev instructs composer to skip installing packages listed in require-dev and –optimize-autoloader converts PSR-0 autoloading to a classmap to get a faster autoloader.
In this example, I want composer install to run every time we deploy, to make sure we have the latest updates according to the composer.lock file.
4. Click on Create Command and then run the deployment. It does mean that deployments take a lot longer to execute (composer can be quite slow, especially when there are a lot of dependencies to check!), and you must remember to carefully check the deployment logs to verify things worked as expected.