Reputation: 1624
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?
Upvotes: 11
Views: 4558
Reputation: 61
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
Upvotes: 6
Reputation: 114548
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.
Upvotes: 6