Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit CodeBuild prematurely with success code

I have a AWS CodeBuild template in a buildspec.yml file with the following line in the build section:

- if [ "${STAGE}" = "alpha" ]; then echo "Quitting alpha build." && exit 0; fi

My expectation is that the build should exit the build if the $STAGE variable is set to alpha with a successful finish. However, it goes to the next line.

If I change the exit code to 1, the build fails at that point as expected.

Any ideas how to end a build prematurely like this, and is there something obviously wrong with this line?

like image 644
Optimus Avatar asked Mar 13 '26 10:03

Optimus


2 Answers

Imagine your line of code being run in a subshell. The return value is the code of the last command. Whether test ([) returns non-zero or not, the if returns zero, unless you exit with something else. In other words, there is no way to distinguish between $STAGE not being alpha and exit 0. So if you want to terminate prematurely, you must exit with a non-zero code.

like image 105
Mad Physicist Avatar answered Mar 16 '26 00:03

Mad Physicist


So if you want to terminate prematurely, you must exit with a non-zero code.

thanks for that answer, its true exit 0 has NO effects in a buildspec.yml

I had the same situation and was searching hours but this finding here shows also there is a need for stopping a CodeBuild with BUILD State: SUCCEEDED otherwise you only prematurely can get BUILD State: FAILED when exit 1

but unfortunately the FAILED state disturbs the following CodeBuilds in the AWS CodePipeline then, so hopefully AWS can offer a "prematurely SUCCEEDED exit" in future, would be helpful in my case too

like image 25
mt_nczepa Avatar answered Mar 15 '26 22:03

mt_nczepa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!