40

是否可以在 bitbucket 管道中组合具有相同步骤的多个分支?

例如:我工作的团队使用两个名称之一作为他们的审查分支,“rev”或“staging”。无论哪种方式,都使用相同的步骤发布到我们的评论服务器。现在分支被单独调用。

pipelines:
     branches:
          rev:
               steps:
                    - echo 'step'
          staging:
               steps:
                    - echo 'step'

但它可能是这样的

pipelines:
     branches:
          rev|staging:
               steps:
                    - echo 'step'
4

5 回答 5

85

大括号内的逗号分隔列表似乎有效:

pipelines:
  branches:
    '{rev,staging}':
      - step:
        script:
          - echo 'step'
于 2017-07-02T21:49:58.233 回答
32

这是有关如何重用某些步骤的完整示例:

image: yourimage:latest

definitions:
  services: ... # Service definitions go there
  steps:
    - step: &Test-step
        name: Run tests
        script:
          - npm install
          - npm run test
    - step: &Deploy-step
        name: Deploy to staging
        deployment: staging
        script:
          - npm install
          - npm run build
          - fab deploy
pipelines:
  default:
    - step: *Test-step
    - step: *Deploy-step
  branches:
      master:
        - step: *Test-step
        - step:
            <<: *Deploy-step
            deployment: production
            trigger: manual

阅读有关 YAML 锚点的更多信息: https ://confluence.atlassian.com/bitbucket/yaml-anchors-960154027.html

于 2018-11-21T21:05:56.707 回答
11

代替解释rev|staging,一种更自然的实现方式是使用流样式序列作为键:

pipelines:
  branches:
    [rev, staging]:
    - step:
      script:
      - echo 'step'

这将减轻对引用和确保空格或额外(尾随)逗号没有语义差异的需求。根据 bitbucket 用于处理此问题的库,上述内容可能会正确解析,但不会加载(例如,PyYAML 无法处理上述内容,但是ruamel.yaml)。我无法验证这种优选方式是否真的适用于 bitbucket

有两种工作方式,一种使用熟悉的 YAML 锚点和别名功能来提供重复(复杂)数据结构一次:

pipelines:
  branches:
    rev: &sharedsteps
    - step:
      script:
      - echo 'step'
    staging: *sharedsteps

正如其他人所指出的,另一种可能性是使用一些非标准的、特定于位桶的、带有嵌入逗号的标量键的解释。我还没有找到关于此的明确文档,但glob 模式似乎适用,因此您可以 {rev,staging}用作键。

丑陋{的地方在于 YAML 中的流式序列指示符,因此需要引用标量:

pipelines:
  branches:
    "{rev,staging}":
    - step:
      script:
      - echo 'step'

以上内容已使用 BlueM 提供的更正步骤语法进行了更新

于 2017-02-17T19:28:38.587 回答
4

正如 Anthon 在对其回答的评论中所要求的,这是他的完美解决方案,但具有 Bitbucket Pipelines 所期望的正确 YAML 结构:

pipelines:
  branches:
    rev: &sharedsteps
      - step:
          script:
            - echo 'step'
    staging: *sharedsteps
于 2017-05-30T21:24:48.397 回答
-1

使用 Bitbucket 5.8,为了能够手动触发管道,我必须使用这种格式:

pipelines:
  branches:
    rev,staging:
      - step:
        script:
          - echo 'step'

所以基本上只是需要相同管道的逗号分隔分支列表

于 2018-03-07T15:15:37.160 回答