Exit CodeBuild prematurely with success code
Asked Answered
M

2

10

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?

Maurene answered 25/5, 2021 at 20:58 Comment(0)
P
5

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.

Peag answered 9/7, 2021 at 13:50 Comment(0)
R
5

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

Robi answered 27/7, 2021 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.