【问题标题】:Bitbucket Pipelines share SOME steps between branchesBitbucket Pipelines 在分支之间共享一些步骤
【发布时间】:2018-01-15 10:20:56
【问题描述】:

是否可以在分支之间共享步骤并仍然运行分支特定的步骤?例如,develop 和 release 分支具有相同的构建过程,但上传到不同的 S3 存储桶。

pipelines:
  default:
    - step:
        script:
          - cd source
          - npm install
          - npm build
  develop:
    - step:
        script:
          - s3cmd put --config s3cmd.cfg ./build s3://develop

  staging:
    - step:
        script:
          - s3cmd put --config s3cmd.cfg ./build s3://staging

我看到了这篇文章 (Bitbucket Pipelines - multiple branches with same steps),但它的步骤相同。

【问题讨论】:

    标签: bitbucket bitbucket-pipelines


    【解决方案1】:

    使用 YAML 锚点:

    definitions:
      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
            name: Deploy to production
            deployment: production
            trigger: manual
    

    文档:https://confluence.atlassian.com/bitbucket/yaml-anchors-960154027.html

    【讨论】:

    【解决方案2】:

    虽然尚未正式支持,但您现在可以预先定义步骤。
    当我让 an issue 跨分支子集运行相同的步骤时,我从 bitbucket 工作人员那里得到了这个提示。

     definitions:
      step: &Build
        name: Build
        script:
          - npm install
          - npm build
    
    pipelines:
      default:
        - step: *Build
      branches:
        master:
          - step: *Build
          - step:
              name: deploy
              # do some deploy from master only
    

    虽然不完美,但总比没有好

    【讨论】:

    • 这里有两个注意事项:1)定义块必须在管道块之前(yamllint允许两者但不允许bitbucket)2)共享许多步骤,给每个步骤一个唯一的名称:step-build: &amp;build然后- step: &amp;step-build
    【解决方案3】:

    我认为 Bitbucket 做不到。您可以使用一个管道并检查分支名称:

    pipelines:
      default:
        - step:
            script:
              - cd source
              - npm install
              - npm build 
              - if [[ $BITBUCKET_BRANCH = develop ]]; then s3cmd put --config s3cmd.cfg ./build s3://develop; fi
              - if [[ $BITBUCKET_BRANCH = staging ]]; then s3cmd put --config s3cmd.cfg ./build s3://staging; fi
    

    最后两行将只在指定的分支上执行。

    【讨论】:

      【解决方案4】:

      您可以使用YAML Anchors 定义和重复使用步骤。

      • 锚定&amp; 来定义一大块配置
      • 别名 * 在别处引用该块

      源分支保存在默认的variable 中,称为BITBUCKET_BRANCH

      您还需要将构建结果(在本例中为 build/ 文件夹)从一个步骤传递到下一步,这是通过 artifacts 完成的。

      结合所有三个将为您提供以下配置:

      definitions:
        steps:
          - step: &build
              name: Build
              script:
                - cd source
                - npm install
                - npm build
              artifacts: # defining the artifacts to be passed to each future step.
                - ./build
      
          - step: &s3-transfer
              name: Transfer to S3
              script:
                - s3cmd put --config s3cmd.cfg ./build s3://${BITBUCKET_BRANCH}
      
      pipelines:
        default:
          - step: *build
        
        develop:
          - step: *build
          - step: *s3-transfer
      
        staging:
          - step: *build
          - step: *s3-transfer
      

      您现在还可以一次性使用referenced post 中提到的全局模式以及developstaging 分支的步骤:

        "{develop,staging}":
          - step: *build
          - step: *s3-transfer
      

      【讨论】:

        【解决方案5】:
        猜你喜欢
        • 2019-05-01
        • 2017-07-07
        • 1970-01-01
        • 2018-09-02
        • 1970-01-01
        • 2019-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多