In a script I have:
script: - set +e # turn off error-trapping - failing-tests - echo $?
I expect an exit code of 1, but get 0. What's going on?
Pipelines executes the printf command before every command in a script section of the bitbucket-pipelines.yml file in order to print those commands to the logs. This means, for example, that:
script: - mvn clean build
actually behave like this:
script: - printf "+ mvn clean build\n" - mvn clean build
One consequence of this is that an attempt to use '$?' to pass the exit code of a failing command to the next command (when error-trapping is turned off) will fail because the next command actually receives the exit code of the printf command.
For example:
will behave like this:
script: - printf "set +e\n" - set +e # turn off error-trapping - printf "failing-tests\n" - failing-tests - printf "echo $?\n" - echo $? # with exit code of 0, because the preceding printf command succeeded
If you want to catch the exit code of a failing command, a workaround is to combine commands on the same line (to prevent the printf command from intercepting the exit code), like this:
script: - set +e - failing-tests; echo $? # with exit code of 1 in this case
Do it like this instead:
- step: name: notify image: cfcommunity/slack-notification-resource script: - | set +e faiing-tests echo $? - some other stuff - yet more stuff
Thanks!
I am using: `set +e` to prevent `failing tests` from stopping Pipelines, so I can add some debugging commands, printout logs after the failing tests command.
However I would still like Pipeline to fail in case of failing test. With: `set +e` Pipeline passes and last command in my Pipeline
How can I make Bitbucket Pipeline fail or not, given the result of my tests?
You may also use `BITBUCKET_EXIT_CODE`
I have the same issue.
I figured it out finally as follows:
set +e # turn off error-trapping# do something here that might throwgit merge-base --is-ancestor $(git rev-parse master) HEADRETURN_CODE=$? ## capture the exit codeif [ $RETURN_CODE -eq 1 ]; then echo "The current branch is not sibling of master" exit 1else echo "The current branch is sibling of master"fiset -e # turn on error-trapping
Thanks @Mina Luke for sharing, I am using exactly the same approach (Forgot to share it here though) for and it works flawlessly.
It looks like you're new here. Sign in or register to get started.