SSH Commands
Run commands on your servers before and after each deployment: migrations, cache clears, restarts.
SSH commands run on your servers as part of a deployment: before the files are uploaded, for example to turn on a maintenance page, or after, to run migrations, clear caches or restart a service.
Add a command
In a project, open SSH Commands and choose New SSH Command.
| Name | A label shown in the list and in the deployment log, such as Run migrations. |
| Commands | What to run, as you would type it in a terminal on the server. |
| Servers | All the project's servers, or only the ones you tick. |
| Execute | Before deployment or After deployment. |
Commands run over SSH as the server's user, in the order of the list. Drag them to change the order.
# after deployment: a Laravel application
php artisan migrate --force
php artisan config:cache
# after deployment: reload PHP-FPM so it picks up the new code
sudo systemctl reload php8.4-fpm
Variables
Donkey replaces these placeholders in a command before running it:
%branch% | The branch being deployed |
%commitrange% | The start and end commit, separated with a hyphen |
%start_commit_ref%, %end_commit_ref% | The start and end commit |
%start_tag_ref%, %end_tag_ref% | The tags of the start and end commit |
%project% | The project's name |
%projecturl%, %deploymenturl% | The project's and the deployment's address in Donkey |
%path% | The server's remote path |
%username% | The user that logs in to the server |
# record which commit is live
echo "%end_commit_ref%" > %path%/REVISION
Anything else between % signs, such as date +%Y-%m-%d, is left as it is.
When a command fails
A command fails when it exits with a code other than 0. What happens next is up to you:
- Fail deployments if SSH commands fail, at the top of the SSH Commands page, marks the deployment as failed. Leave it off to carry on and only see the error in the log.
- The deploy form has the same option for a single deployment, plus Only transfer files, don't run SSH commands to skip the commands that time.
- Automatic deployments always run the commands and follow the project's setting.
The output of each command is in the deployment's log, under the before or after step.
Tips
- Commands that need a password prompt or a terminal will hang. Use
--force,-yor asudoersentry for the deploying user. - Use full paths, or
%path%, rather than relying on the login shell's directory or environment. - Keep long jobs out of SSH commands; they hold the deployment open until they finish.
From the repository
The before and after sections of a Donkeyfile replace these commands for deployments of commits that have one, along with whether a failure fails the deployment.
Still need help?
Tell us what you are trying to do and we will reply by e-mail.