I'm using Go's custom command to execute a shell script. The problem is, if the script fails for any reason, the build still succeeds. How do I fail the build if something goes wrong in my shell script?
Fail build in Go when custom command fails
1k views Asked by Jorn At
2
There are 2 answers
0
On
The easiest way to go would be to add either set -e at the top of the script or change the shebang to #!/bin/bash -e. -e will cause bash to exit immediately (with an error) after any[0] command fails. As an aside I suggest adding -u as well to catch undefined variable use.
[0] It's a little more complicated than that -- it actually only exits if a command fails if that command's result is not tested in the script. See the bash man page for (extensive) details that may differ depending on your specific platform.
See also: "Aborting a shell script if any command returns a non-zero value?" at https://stackoverflow.com/a/4346420/11638666
Arrange the logic in your script so that if the script fails it exits with an exit code >= 1
alternatively if you want to fail fast
For each shell cmd you can see the exit code with...