Running a script before pull request is merged ?
Asked by Ritesh Thakur:

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" ?


Answers:
author: Patrik S
body: Hello [@Ritesh Thakur](/t5/user/viewprofilepage/user-id/5154056) ,

Welcome to Atlassian Community!

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

The [pull-request pipeline](https://support.atlassian.com/bitbucket-cloud/docs/pipeline-start-conditions/#Pull-Requests) 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](https://www.atlassian.com/software/bitbucket/pricing), 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](https://support.atlassian.com/bitbucket-cloud/docs/suggest-or-require-checks-before-a-merge/#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 :

* <https://support.atlassian.com/bitbucket-cloud/docs/pipeline-start-conditions/#Pull-Requests>

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

Thank you, [@Ritesh Thakur](/t5/user/viewprofilepage/user-id/5154056) !

Patrik S

comments: author: Ritesh Thakur
body: 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 ?


author: Patrik S
body: Hello [@Ritesh Thakur](/t5/user/viewprofilepage/user-id/5154056) ,

I'm afraid the only way to achieve this would be to indeed using [pull-request pipeline](https://support.atlassian.com/bitbucket-cloud/docs/pipeline-start-conditions/#Pull-Requests).

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](/t5/user/viewprofilepage/user-id/5154056) !

Patrik S


author: Ritesh Thakur
body: Thanks Patrik


Tags: bitbucket-cloud

cloud