Create DRY Bitbucket Pipelines scripts by using YAML Anchors and Aliases
Published in
1 min readFeb 3, 2019
If you are using a CI you know the benefits of scripting and automation. Then why repeat yourself in your bitbucket-pipelines.yml when you can write a step once and use multiple times.
An anchor defines a chunk of steps to be used elsewhere — it is represented by a &
symbol
An alias is used to call an anchor from elsewhere — it is represented by a *
Simple example
definitions:
steps:
- step: &test
name: Test app
script:
- yarn
- yarn test - step: &build
name: Build app
script:
- yarn
- yarn build
pipelines:
pull-requests:
'**':
- step: *test branches:
develop:
- step: *test
- step: *build
master:
- step: *test
- step: *build
Customising the step
You can override values when calling an anchor with <<
like in the example below.
pipelines:
branches:
develop:
- step: *test
- step: *build
master:
- step: *test
- step:
<<: *build
name: Building Master
And we’ve done! 🙌
In the simple examples above we aren’t being all that much more efficient but with a longer real world example the benefits are far greater — I hope this helps.