如何仅在拉取请求到主分支时运行管道

问题描述 投票:2回答:1

Bitbucket管道允许定义对pull-requests的检查,并具有允许检查源分支的glob过滤器。

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

如何根据目标分支进行过滤?有些测试只有在合并到master时才需要完成。

git bitbucket bitbucket-pipelines
1个回答
5
投票

遗憾的是,拉请求管道机制正在基于源分支而不是在目标分支上工作。

这是由他们的跟踪器在团队成员之一添加pull-request功能的问题解释的:

pull请求下的分支模式定义了源分支。这样您就可以根据修复运行不同的管道。例如,您可能对功能分支与修补程序分支具有不同的测试集。请注意,这只是讨论在开发过程中针对PR运行的测试。

资料来源:Geoff Crain's comment

实际上this exact feature还有另一个问题。

但团队的答案是:

我可以肯定地知道为什么这会有用,特别是在合并到master / main分支时。

然而,鉴于我们目前的优先事项,我们不太可能在短期内支持这一点。与此同时,我会打开这张票来衡量其他用户对同样事情的兴趣。

资料来源:Aneita Yang's comment

也就是说,你可以通过这种方式获得所需的行为:

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'

或者,如果您已经对所有拉取请求进行了一些测试,就像我理解的那样:

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

或者再次,它可以自己外部化到脚本:

到位桶,pipelines.yaml

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

AM /测试

#!/usr/bin/env bash

printf 'these are the all PR tests'

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

另请参见:管道文档页面中的变量:https://confluence.atlassian.com/bitbucket/variables-in-pipelines-794502608.html

© www.soinside.com 2019 - 2024. All rights reserved.