Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,360,009
Community Members
 
Community Events
168
Community Groups

In Bitbucket Pipelines, why doesn't '$?' get the expected exit code from the previously run command?

paulwatson Atlassian Team Jul 21, 2017

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?

3 answers

2 accepted

3 votes
Answer accepted

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

    

3 votes
Answer accepted

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 may also use `BITBUCKET_EXIT_CODE`

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?

I have the same issue.

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

Like Paul Rohorzka likes this

Thanks @Mina Luke for sharing, I am using exactly the same approach (Forgot to share it here though) for  and it works flawlessly.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

Git push size limits are coming to Bitbucket Cloud starting April 4th, 2022

Beginning on April 4th, we will be implementing push limits. This means that your push cannot be completed if it is over 3.5 GB. If you do attempt to complete a push that is over 3.5 GB, it will fail...

3,458 views 3 10
Read article

Atlassian Community Events