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?
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
script:
- set +e # turn off error-trapping
- failing-tests
- echo $?
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I figured it out finally as follows:
set +e # turn off error-trapping
# do something here that might throw
git merge-base --is-ancestor $(git rev-parse master) HEAD
RETURN_CODE=$? ## capture the exit code
if [ $RETURN_CODE -eq 1 ]; then
echo "The current branch is not sibling of master"
exit 1
else
echo "The current branch is sibling of master"
fi
set -e # turn on error-trapping
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks @Mina Luke for sharing, I am using exactly the same approach (Forgot to share it here though) for and it works flawlessly.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.