Trigger pipelines on pull request to destination branch
Platform Notice: Cloud - This article applies to Atlassian products on the cloud platform.
Summary
Bitbucket allows you to run pipelines on pull requests. How do I specify a pipeline to run when a pull request is opened with a specific destination branch?
Cause and Use Case
Bitbucket Pull Request triggers only work on source branches. In other words, the pull request name pattern that you define in pipelines only applies to source branches. This makes it difficult to specify a pull request that will act on a destination branch. One use case is to run a pipeline when there is a pull request opened to merge to master only.
Solution
We can make use of an IF condition along with a built in Pipeline variable - BITBUCKET_PR_DESTINATION_BRANCH to achieve the result. Here is a sample pipeline configuration -
pipelines:
pull-requests:
'**': # this trigger will make the pipeline run for all Pull Requests
- step:
script:
- if [ "${BITBUCKET_PR_DESTINATION_BRANCH}" = "master" ]; then printf 'Commands to be triggered for master branch only'; fi
The above configuration will trigger for all pull requests since our pattern is ** for Pull Requests. However, the script checks if the destination branch is master. Only then are further commands executed. This way, we can trigger tests and commands to be run for pull requests with specific destination branches.
Please note that variable BITBUCKET_PR_DESTINATION_BRANCH
will only be available on the PR builds as documented here.
If the above instructions do not assist with your use case, please raise a support ticket or raise a community support ticket for further assistance with this.