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" ?
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
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 ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 :
Thank you, @Ritesh Thakur !
Patrik S
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.