首页 文章

Bitbucket Pipelines在分支机构之间共享一些步骤

提问于
浏览
10

是否可以在分支之间共享步骤并仍然运行分支特定步骤?例如,开发和发布分支具有相同的构建过程,但上载到单独的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),但是它的步骤相同 .

4 回答

  • 3

    使用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

  • 1

    虽然它尚未得到官方支持,但您现在可以预先定义步骤 .
    当我在分支机构的子集中运行相同的步骤时,我从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
    

    它并不完美,但它总比没有好

  • 10

    我觉得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
    

    最后两行将仅在指定的分支上执行 .

  • 9

相关问题