I'm currently migrating an existing CI/CD pipeline build in Azure DevOps from the ‘classic’ build approach to YAML files. Yesterday I talked about the concept of templates as a replacement for Task groups in the 'classic' pipeline.
I showed how to create a template and use it inside your YAML pipeline. What I didn’t mention is that next to ‘including’ a template it is also possible to ‘inherit’ from a template.
Let’s find out how to do this.
Extending a template
To ‘inherit’ from a template, we should let a pipeline extend from an extendible template.
To have an extendible template, it must be created on the level of the stages:
parameters: | |
- name: yesNo | |
type: boolean | |
stages: | |
- stage: development | |
pool: | |
vmImage: windows-latest | |
jobs: | |
- deployment: | |
environment: development | |
strategy: | |
runOnce: | |
deploy: | |
steps: | |
- script: echo ${{ parameters.yesNo }} | |
- stage: acceptance | |
dependsOn: development | |
pool: | |
vmImage: windows-latest | |
jobs: | |
- deployment: | |
environment: acceptance | |
strategy: | |
runOnce: | |
deploy: | |
steps: | |
- script: echo ${{ parameters.yesNo }} |
To create a pipeline that extends a template, we need to provide the relative path to the template file and also pass all (required) parameter:
resources: | |
pipelines: | |
- pipeline: example_ci # Name of the pipeline resource(up to you to choose) | |
source: Example-CI # Name of the pipeline (case sensitive!) | |
trigger: true | |
pool: | |
vmImage: ubuntu-latest | |
extends: | |
template: templates/template.yml | |
parameters: | |
yesNo: false |