Drush 8 Exit Code Workaround

drupal snippets bash

A bug in older versions of drush stop errors bubbling up to shell exit codes. This is problematic if you have a shell script with a sequence of commands that depend on the previous one completing successfully. It also makes detecting failed CI builds, deployments and cronjobs near impossible.

Unfortunately I’m stuck using drush8 as Lagoon does not support dynamic drush aliases in newer versions.

The bash function below will terminate a script with exit-code 1 if the output piped to it includes exception or error.

So it can be used in a script like this - drush updatedb -y | fail_on_exceptions_errors

You must have -o pipefail enabled in your shell for this to work.

#!/usr/bin/env bash
set -euo pipefail

fail_on_exceptions_errors() {
  tee /dev/tty | egrep -i "(exception|error)" && exit 1 || true

drush updatedb -y | fail_on_exceptions_errors