Bitbucket 管道在分支之间共享一些步骤

Bitbucket Pipelines share SOME steps between branches

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

我看到了这个 post () 但它的步骤相同。

显然它正在制作中。希望尽快上市。

https://bitbucket.org/site/master/issues/12750/allow-multiple-steps?_ga=2.262592203.639241276.1502122373-95544429.1500927287

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

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

虽然官方暂不支持,但您现在可以预定义步骤。
当我 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

虽然不完美,但聊胜于无

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

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

  • 锚点&定义一个配置块
  • 别名 * 以在其他地方引用该块

并且源代码分支保存在一个名为 BITBUCKET_BRANCH

的默认 variable

您还需要将构建结果(在本例中为 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

您现在还可以使用 中提到的 glob 模式以及 developstaging 分支的步骤:

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