【问题标题】:Bitbucket Pipelines - multiple branches with same stepsBitbucket Pipelines - 具有相同步骤的多个分支
【发布时间】:2017-07-07 08:47:26
【问题描述】:

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

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

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

但是会不会是这样的

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

【问题讨论】:

    标签: git yaml bitbucket bitbucket-pipelines


    【解决方案1】:

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

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

    【讨论】:

    • 我认为这个解决方案比其他解决方案更干净。我这样使用它:'{feature/*,fix/*}',因为所有feature 和fix 分支都经过相同的步骤。
    • 确保您没有在分支配置中的逗号后面放置空格,否则它会假定您的分支名称前面有空格。 (所以不要这样做:'{rev, staging}')
    • 非常感谢它现在正在工作。我这样使用它'{release/**,hotfix/**}':
    • @Jones03 非常感谢。节省了我数小时的调试时间
    【解决方案2】:

    这是一个关于如何重用一些步骤的完整示例:

    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

    【讨论】:

      【解决方案3】:

      代替解释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 patterns 似乎适用,因此您可以使用{rev,staging} 作为键。

      难看的是{是YAML中流式的序列指示符,所以标量需要加引号:

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

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

      【讨论】:

      • 酷,确实有效。 (至少在使用正确的 Bb Pipeline 语法时,原始帖子中并非如此。)
      • @BlueM 您能否添加更正的语法示例作为我的答案的替代方案?
      • 作为一个在任何 devops 方面的菜鸟,这篇文章让我很困惑。理论在哪里结束,"{a,b}" 是可行的解决方案吗?
      • [rev, staging]: 对 BitBucket 的验证器无效。
      • 锚和别名是一个很好的解决方案,可读性和可维护性:)
      【解决方案4】:

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

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

      【讨论】:

      • 我知道。在使用验证器时,我遇到了各种 do 有效但被报告为无效的语法。显然,验证器不使用与实际管道相同的代码库/语言/库进行解析。
      • 刚刚尝试了这种语法,bitbucket 本身给出了验证错误。
      • Bitbucket 管道确实支持 YAML 别名和锚点,但验证器不支持。如果要验证 yaml,请通过在线 yaml-json 转换器 (YAML => JSON => YAML) 运行它,并将生成的 YAML 呈现给验证器。
      【解决方案5】:

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

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

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

      【讨论】:

      • 双重检查,这对我在 2019 年 2 月 18 日的 bitbucket.org 上不起作用。
      猜你喜欢
      • 2018-01-15
      • 2018-11-06
      • 2018-09-02
      • 2015-10-06
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多