Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root


1 badge earned


Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!


Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.


Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!


Running a script before pull request is merged ?


For my master and dev branch, I was wondering if there is a way in Bitbucket which allows me to run some sanity checks before code is actually merged by pull requests ? Sanity checks like versioning for example. Ideally I would like to put a script which will automate versioning process. All the posts I can find on Atlassian community or Google point me towards "Running a script AFTER pull request is merged". Can someone point me towards some material wherein I can learn about "Running a script BEFORE pull request is merged" ?

1 answer

1 accepted

0 votes
Answer accepted
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
Feb 23, 2023 • edited

Hello @Ritesh Thakur ,

Welcome to Atlassian Community!

As per your description, I would suggest using pull-request-triggered pipelines. 

The pull-request pipeline will start as soon as a Pull Request is created for the matching branch, and will also be triggered on updates to the Pull Request, such as a new commit being pushed to the source branch of the PR. Within this pull-request pipeline, you could then run any commands you want, such as validation scripts, versioning checks, etc. This all runs before a pull request is merged.

If on the Premium plan, you can also enforce a merge check that will prevent users from merging the pull request if they don't have that number of successful builds for the most recent commit (see Merge checks)

Following is an example of bitbucket-pipelines.yml the file that is triggered for PRs that have dev/* and master branches as the source branch:

      - step:
          name: Build for dev branch PR
            - echo "This is a dev branch PR"
- ./

      - step:
          name: Build for master branch pull request
            - echo "This is a master branch PR"
- ./

For more details and examples of pull-request-triggered pipelines you can refer to the following documentation : 

Hope that helps! Let me know in case you have any questions.

Thank you, @Ritesh Thakur !

Patrik S

Thanks for your quick response Pratik. 

I don't want to trigger an action when Pull Request is created, rather I want to do the same when "Merge" (after approvals) is triggered. Also, is there a way that I can save one of my scripts, which will be triggered for pull requests for all the repositories ?

Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
Feb 28, 2023 • edited

Hello @Ritesh Thakur ,

I'm afraid the only way to achieve this would be to indeed using pull-request pipeline.

It's important to note that pull-request pipelines do not run only on pull-request creation, but also in every new commit that is pushed to the pull-request source branch. So if you have your PR pipeline with your validation script, this script will run on every commit that is pushed to the pull request. If for a given commit you don't want to trigger a pipeline, you can include [skip ci] as part of the commit message.

Another benefit of PR pipelines is that during the build it merges the destination branch into the source branch to mimic what the code will look like once the Pull Request is actually merged. This helps you to verify if the code in Pull Request will break anything once merged into the destination branch.

As for having the script in multiple repositories, I would suggest two options : 

  • Commit the script to every repository that will run it, so the script will be available during the build to be executed
  • Upload the script in the Downloads section - or any other host provider- and download the script (using curl for example) during your build and execute it.

Thank you, @Ritesh Thakur !

Patrik S

Suggest an answer

Log in or Sign up to answer
AUG Leaders

Atlassian Community Events