Missed Team ’24? Catch up on announcements here.

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

How to fail the Merge if my Pipeline fails that runs after merging the PR to a specific branch

salluri December 16, 2022

Hi Team,

 

I am trying to create a pipeline that runs on merge to a target branch. The issue is the pipeline starts deploying only once the PR is merged into the target branch. If the pipeline that is triggered after the merge is failed how can i stop or fail the merge to happen?

Is there a way i can achieve this through any post deploy steps in my pipeline to make sure if the deploy command is failed it stops or fails the merge to happen on the target branch?

 

Thanks

1 answer

1 accepted

0 votes
Answer accepted
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 20, 2022

Hi @salluri,

There is no pipeline definition that runs on the merge of pull requests only, so I assume that you have a pipeline that runs on a branch definition, is that the case? Pipelines that run on branches don't have any variables available regarding pull requests and it would be quite difficult to achieve what you are looking for.

You would need to write a script using our API in order to find out which PR it is, based on the merge commit, and even then, you cannot prevent the merge from happening, since it will have already happened. You could possibly reset the last commit and force push back to the Bitbucket repo, but this way you risk losing other commits on the destination branch that were made while the build was running.

What you could do instead is use pull request pipelines, that run when a pull request is created and when the source branch of the pull request gets updated. These pipelines have the following two variables available:

  • $BITBUCKET_PR_ID : the ID of the pull request
  • $BITBUCKET_PR_DESTINATION_BRANCH : the name of the destination branch of the pull request
  • $BITBUCKET_BRANCH is also available, which points to the source branch of the pull request

Using these variables, you can do the following at the beginning of your step:

script:
- git checkout $BITBUCKET_PR_DESTINATION_BRANCH
- git merge $BITBUCKET_BRANCH
- <rest of your pipeline>

This way, the build will run in the merge commit (the merge will not occur in the Bitbucket repo, but in the clone of the repo in the Docker container where the pipeline is running). If the build is successful, you can then manually merge the PR on Bitbucket.

Kind regards,
Theodora

Suggest an answer

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

Atlassian Community Events