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

Running a script before pull request is merged ?

Ritesh Thakur February 22, 2023

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.
February 23, 2023

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:

pipelines:
  pull-requests:
    dev/*:
      - step:
          name: Build for dev branch PR
          script:
            - echo "This is a dev branch PR"
- ./validation_script.sh

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

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

Ritesh Thakur February 23, 2023

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.
February 28, 2023

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
    OR
  • 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

Ritesh Thakur February 28, 2023

Thanks Patrik

Suggest an answer

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

Atlassian Community Events