YAML anchors

Configure bitbucket-pipelines.yml

On this page

Still need help?

The Atlassian Community is here for you.

Ask the community

If you have repeated sections in your bitbucket-pipelines.yml file, you might like to use YAML anchors. They can reduce effort and make updating in bulk, easier.

Anchors and aliases

There are 2 parts to this:

  • The anchor '&' which defines a chunk of configuration
  • The alias '*' used to refer to that chunk elsewhere

So in the example below we use &build-test to define a step entity, which has several lines for reuse, and the alias *build-test to reuse it.

definitions: 
  steps:
    - step: &build-test
        name: Build and test
        script:
          - mvn package
        artifacts:
          - target/**

pipelines:
  branches:
    develop:
      - step: *build-test
    master:
      - step: *build-test

YAML anchors and aliases cannot contain the ' [ ', ' ] ', ' { ', ' } ', and ' , ' characters.

Override values

But what if you want essentially the same block of code with one small change?

You can use overrides with the characters '<<:' to add more values, or override existing ones.

Building on the example above we could override the name value for a step:

pipelines:
  branches:
    develop:
      - step: *build-test
    master:
      - step: 
          <<: *build-test
          name: Testing on Master

In the pipelines result page, we'd see the name of the step as Build and test for pipelines that ran on the develop branch, and Testing on master for pipelines that ran on the master branch.

Put it all together

Using the techniques above, you can reduce the amount of effort needed to create your configuration, and maintain it when changes occur.

As with all YAML it's vital to make sure your indentation is correct, which is tricky when using anchors, so take care!


Here's a slightly longer example showing how you might use overrides and multiple anchors together:

definitions: 
  steps:
    - step: &build-test
        name: Build and test
        script:
          - mvn package
        artifacts:
          - target/**
    - step: &deploy
        name: Deploy
        deployment: test
        script:
          - ./deploy.sh target/my-app.jar

pipelines:
  branches:
    develop:
      - step: *build-test
      - step: *deploy
    master:
      - step: *build-test
      - step:
          <<: *deploy
          deployment: production
          trigger: manual
Last modified on Oct 31, 2018

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.