August 12, 2021

Deployment Hook Error Handling in Envoyer

laravel tips

Laravel's Envoyer service allows you to break up your deployment process into multiple steps, which makes it really easy to manage deployments. Envoyer runs each step individually, checking the exit code of the last command within the step. Because each deployment step is Bash, if it's a non-zero exit code then it gets reported back as a failure.

A question I've been asked a few times is "why does Envoyer report this deployment step successful, when it actually failed?". Let's look at an example of when this may happen, and then two ways we can resolve it.

The Deployment Step Example

1php artisan migrate
2 
3echo "Hey, beautiful reader!"

In this example, let's say that php artisan migrate may fail for some reason.

The Simple Solution

The simplest solution is to split the deployment step into multiple steps:

Run Migrations

1php artisan migrate

Compliment The Reader

1echo "Hey, beautiful reader!"

But this isn't always the best solution, perhaps because we need to know the output of the response and do something else with it before reporting a failure.

The Correct Solution

The right way of handling this particular issue is to use more of Bash's power, using the set builtin. We can update our deployment script:

1set -e
2 
3php artisan migrate
4 
5echo "Hey, beautiful reader!"

set -e tells the script to exit immediately if a command (or pipeline of commands) returns a non-zero exit code.

Hope this helps!