Bitbucket Pipelines - multiple branches with same steps

21,163

Solution 1

A comma-separated list inside braces appears to work:

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

Solution 2

This is a full example on how you can reuse some steps:

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

Read more about YAML anchors: https://confluence.atlassian.com/bitbucket/yaml-anchors-960154027.html

Solution 3

Instead of interpreting rev|staging, a far more natural way of implementing that would be using a flow style sequence as a key:

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

That would alleviates the need for quoting and for making sure spaces, or an extra (trailing) comma, make no semantic difference. Depending on the library that bitbucket uses to process this, the above might parse correctly, but not load (e.g. PyYAML cannot handle the above, but ruamel.yaml). I have not been able to verify if this preferable way actually works in bitbucket.

There are two ways that do work, one using the familiar YAML functionality of anchors and aliases to provide repeated (complex) data structures only once:

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

The other possibility is, as others have indicated, to use some a non-standard, bitbucket specific, interpretation of scalar keys with embedded comma's. I have not found clear documentation on this, but the glob patterns seem applicable, so you can use {rev,staging} as a key.

What is ugly about this is that { is the flow-style sequence indicator in YAML, so that scalar needs to be quoted:

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

The above was updated using the corrected step syntax that BlueM provided

Solution 4

As requested by Anthon in a comment to his answer, this is his perfect solution, but with the correct YAML structure as expected by Bitbucket Pipelines:

pipelines:
  branches:
    rev: &sharedsteps
      - step:
          script:
            - echo 'step'
    staging: *sharedsteps
Share:
21,163
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    Is it possible to combine multiple branches that have the same steps within bitbucket pipelines?

    ex: The teams I work on use one of two names for their review branches, either "rev" or "staging". Either way the same steps are used to publish to our review server. Right now the branches are called out separately.

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

    but could it be something like

    pipelines:
         branches:
              rev|staging:
                   steps:
                        - echo 'step'
    
  • BlueM
    BlueM about 7 years
    Cool, works indeed. (At least when using the correct Bb Pipeline syntax, which is not the case in the original post.)
  • Anthon
    Anthon almost 7 years
    @BlueM Could you add the corrected syntax example as an alternative to my answer?
  • ordonezalex
    ordonezalex almost 7 years
    I think this solution is cleaner than the others. I use it like this: '{feature/*,fix/*}' because all the feature and fix branches go through the same steps.
  • BlueM
    BlueM over 6 years
    I know. When using the validator, I encounterd various syntaxes that do work, but are reported as invalid. Obviously, the validator does not use the same codebase/language/library for parsing as the actual pipeline.
  • Snaver
    Snaver over 6 years
    Just attempted this syntax and bitbucket itself gave a validation error.
  • bert bruynooghe
    bert bruynooghe about 6 years
    Bitbucket pipelines do support YAML aliases and anchors, but the validator does not. If you want to validate the yaml, run it to through an online yaml-json converter (YAML => JSON => YAML) and present the resulting YAML to the validator.
  • Jones03
    Jones03 over 5 years
    Make sure you don't put a space behind the comma in the branches config, or it'll assume your branch name has a space in front of it. (so DONT do: '{rev, staging}')
  • Nino van Hooff
    Nino van Hooff about 5 years
    Double checked, and this didn't work for me on bitbucket.org on feb 18, 2019.
  • Qwerty
    Qwerty about 5 years
    Being a total noob in anything devops, this post quite confuses me. Where does the theory end and is this "{a,b}" the working solution?
  • Mina Luke
    Mina Luke almost 5 years
    Thanks so muc it is working now. I am using it like this '{release/**,hotfix/**}':
  • Roman Podlinov
    Roman Podlinov almost 4 years
    [rev, staging]: is invalid for BitBucket's validator.
  • crazy_abdul
    crazy_abdul over 2 years
    @Jones03 Thanks so much. Saved me hours of debugging
  • jackblk
    jackblk over 2 years
    anchors and aliases is a nice solution, readable and maintainable :)