Is there a way to fail a build when one of the build hooks throws an error?

In my case I want to use npm clean-install instead of npm install to ensure that my package-lock and my package.json have not diverged.

Yes. To do this you need to start your build hook with set -e. That means :

  -e  Exit immediately if a command exits with a non-zero status.

To be a bit more clear, if you don’t use set -e, the build will exit with the exit code of the last command.

For example:

ls /folder/that/doesnt/exist
exit 0

Will exit with exit code β€œ0”, for success. If you use set -e, the build will exit after the ls with the exit code that ls failed with.

Another option that is usually useful is set -o pipefail which will fail the build if a piped command fails.

For example:

set -e
ls /folder/that/doesnt/exist | echo foo
exit 0

Will return with exit code 0.

set -eo pipefail
ls /folder/that/doesnt/exist | echo foo
exit 0

Will return with a failure.

1 Like