Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How can i fail a pipeline if there is a failure in the build.sh script in the Build step ?

Rajat Gupta April 17, 2019

I am triggering a shell script within a step in the bitbucket-pipelines.

If for some reason there is error or failure in the shell script the status of the bitbucket-pipeline is still pass.

Any way or modification i need to do to fail the pipeline build if there is any kind of error in the shell script.

2 answers

0 votes
Marcus Menezes
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 22, 2022

Hi, 

In my case, this doesn't work. The solution I found is to return "exit 1" from the script. 

Example: 

 

On pipeline : 

- step:
name: "Test"

script:
- ./my_script.sh

 

On my_script.sh

sh ./my_second_script.sh 

if [[ $? -ne 0 ]]; then
exit 1 #it will fail the pipeline
fi

 

On my_second_script I am returning "exit 1" when I want to fail. 

0 votes
Daniel Santos
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 22, 2019

Hi @Rajat Gupta

Is it a bash shell script?
I'm afraid you need to set a special option to make sure the script itself will return an error in case any of its commands fail.

Please take a look at this article (bash - When to use set -e) and let me know if setting the environment options below, at the beginning of your script, will solve the issue:

set -eu
set -o pipefail
Jason Nichols
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 16, 2020
set -o pipefail

returns

set: Illegal option -o pipefail

Not sure if this is still relevant.

carlos_ijalba August 26, 2024

The command set -o pipefail first appeared in Bash 4.0.

It was introduced to provide a more consistent and predictable exit status for pipelines. Prior to Bash 4.0, the exit status of a pipeline was determined only by the last command in the pipeline, even if earlier commands had failed.

With set -o pipefail, the pipeline's exit status becomes the exit status of the first command that fails.

On which system where you trying it? it works on all modern Linux and MacOS that I have tried it on.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events