Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run pipeline only on pull request to master branch

Bitbucket pipelines allows to define checks on pull-requests and has glob filter that allows checking source branch.

pipelines:
  pull-requests:
    '**': #this runs as default for any branch not elsewhere defined
      - step:
          script
            - ...
    feature/*: #any branch with a feature prefix
      - step:
          script:
            - ...

How to filter based on target branch? There are some tests that need to be done only when merging into master.

like image 205
Margus Pala Avatar asked Mar 06 '19 09:03

Margus Pala


People also ask

How do you trigger a pipeline on a merge request?

Push a new commit to the source branch for a merge request. Select Run pipeline from the Pipelines tab in a merge request. This option is only available when merge request pipelines are configured for the pipeline and the source branch has at least one commit.

What is pipeline pull request?

Pull request pipeline runs set compliance status checks on a pull request for the specified application repository. Attempts to merge a pull request into the master branch might be blocked because of failed compliance status checks.

What is a detached pipeline in GitLab?

In a basic configuration, GitLab runs a pipeline each time changes are pushed to a branch. If you want the pipeline to run jobs only on commits associated with a merge request, you can use pipelines for merge requests. In the UI, these pipelines are labeled as detached .


1 Answers

Sadly indeed, the pull-request pipeline mechanism is working based on the source branch, not on the target branch.

This is explained on the issue from their tracker adding the pull-request feature by one of the team member:

The branch pattern under pull requests defines the source branch. This is so that you can run a different pipeline depending on the fix. For example you may have a different set of tests for feature branches vs hotfix branches. Note that this is only talking about the tests that run against the PR during development.

Source: Geoff Crain's comment

There is actually another issue open for this exact feature.

But the answer from the team is:

I can definitely see why this would be useful, especially when merging to the master/main branch.

Given our current priorities, however, this is unlikely something that we'll support in the short term. In the meantime though, I'll open this ticket to gauge the interest of other users in seeing the same thing.

Source: Aneita Yang's comment

That said, you could somehow have the required behavior with this kind of hack:

pipelines:
  pull-requests:
    '**': #this runs as default for any branch not elsewhere defined
      - step:
          script
            - if [ "${BITBUCKET_PR_DESTINATION_BRANCH}" != "master" ]; then printf 'not a target branch we want to check'; exit; fi
            - printf 'running useful tests'

Or, if you already are doing some tests on all pull request, like I understand it:

pipelines:
  pull-requests:
    '**': #this runs as default for any branch not elsewhere defined
      - step:
          script
            - printf 'these are the all PR tests'
            - if [ "${BITBUCKET_PR_DESTINATION_BRANCH}" = "master" ]; then printf 'those are the extra checks on master'; fi

Or yet again, it could be externalized to a script on its own:

bitbucket-pipelines.yaml

pipelines:
  pull-requests:
    '**': #this runs as default for any branch not elsewhere defined
      - step:
          script
            - ./bin/tests "${BITBUCKET_PR_DESTINATION_BRANCH}"

bin/tests

#!/usr/bin/env bash

printf 'these are the all PR tests'

if [ "${1}" = "master" ]
then 
    printf 'those are the extra checks on master'
fi

See also: Variables in pipelines documentation page: https://confluence.atlassian.com/bitbucket/variables-in-pipelines-794502608.html

like image 65
β.εηοιτ.βε Avatar answered Sep 22 '22 08:09

β.εηοιτ.βε