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

Run A Step if Above Steps Failed

Shrey April 12, 2023

Hi Folks,

Need your help in one thing. I have a BB pipeline which consist of 4 steps . If Any of 3 steps failed there should be option for me to run 4th step directly.
Scenario : If any of step gets failed the 4th step should run with a manual trigger.

How Can I Achieve that. Any help is highly appreciated .

1 answer

1 accepted

2 votes
Answer accepted
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 13, 2023

Hello @Shrey ,

Thank you for reaching out to Atlassian Community!

I'm afraid that currently, Bitbucket cloud pipelines will not continue to the next steps when a step failed. Whenever a step fails, the pipeline execution will be stopped and none of the next steps will be executed. We do have a feature request to implement an option to allow a step to fail and continue to the following steps : 

We encourage you to add your vote there, so it gives that feature traction, and also add yourself as a watcher to be notified of any update on that feature implementation.

While that option is not available, you might be interested in using the after-script option in your step. The after-script section will always be executed at the end of a step, regardless if the step succeeded or failed. You could add some logic to the after-script section for it to run only when the step fails, like in the below example : 

pipelines:
  default:
    - step:
        name: Step 1
        script:
          - echo "test step"
        after-script:
          - if [[ BITBUCKET_EXIT_CODE -eq 0 ]]; then exit 0; else echo "Step failed" ; fi
- echo "only execute this if step failed"

In this example, in the after-script section, we are evaluating the value of the variable BITBUCKET_EXIT_CODE that will contain the exit code of the last command executed in the step. If this exit code is zero (meaning the step's script commands were executed successfully) the after-script section will be forced exit. On the other hand, if BITBUCKET_EXIT_CODE is different than zero (meaning one of the step commands has failed) the after-script section will be executed up to its end, so you can include any commands there that you would like to be executed only on step-failure.

For more details about the after-script section you can check our official documentation below : 

Thank you, @Shrey !

Patrik S

pablo.hendrickx
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!
November 10, 2023
after-script:

- if [[ BITBUCKET_EXIT_CODE -eq 0 ]]; then exit 0; else echo "Test failed!"; fi
The example code snippet is missing the ending "; fi" statement for it to work.
After adding it, it works like a charm.
Thanks !
Like Patrik S likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events