How to programmatically cancel a Netlify build

Updated

Sometimes you've got a build script for a Netlify site, and want to cancel the build and deploy if a certain condition is true a version of the website with an error doesn't get published.

To cancel the build in a Node.js script, just run:

process.exit(1)

This causes the build to fail, and will show a message like this in your Netlify build logs:

If you want to add additional information on why the build failed, log a message before calling process.exit:

console.warn('This is what went wrong.')
process.exit(1)

This page is referenced in: Coding Blog