YAML アンカー

bitbucket-pipelines.yml を設定する

このページの内容

お困りですか?

アトラシアン コミュニティをご利用ください。

コミュニティに質問

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
最終更新日: 2018 年 9 月 31 日

この内容はお役に立ちましたか?

はい
いいえ
この記事についてのフィードバックを送信する
Powered by Confluence and Scroll Viewport.