简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM