Deployment Hook Error Handling in Envoyer

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

php artisan migrate

echo "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

php artisan migrate

Compliment The Reader

echo "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:

set -e

php artisan migrate

echo "Hey, beautiful reader!"

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