Bitbucket Pipelines share SOME steps between branches

YarGnawh picture YarGnawh · Aug 8, 2017 · Viewed 10.6k times · Source

Is it possible to share steps between branches and still run branch specific steps? For example, the develop and release branch has the same build process, but uploaded to separate S3 buckets.

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

I saw this post (Bitbucket Pipelines - multiple branches with same steps) but it's for the same steps.

Answer

Max Malysh picture Max Malysh · Nov 21, 2018

Use YAML anchors:

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

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