How to create Azure Pipeline templates?
Introduction
In Azure Pipeline, we can create YAML-based templates. Templates are used to avoid creating the same multiple tasks and you can call them in the multiple pipelines. Like modules/functions, you can use them as many times to avoid code repetition.
Templates can be created at the task, job, or stage level. Check this link below to understand the pipeline definition structure.
https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/pipeline?view=azure-pipelines
If you check the above definition structure, we can define template parameters for task, job, and stage. We can leverage this feature to create templates.
We will discuss each of the templates.
Task level template:
For example, I want scripts in the different tasks that can provide me with the health of the Windows Services and the CPU Utilization and I want those scripts should be used in the other pipelines as well.
So we will create here step template and add those two tasks to the scripts. Creating a step template is very easy as we just have to define the steps in the YAML file as shown below.
# Path: templates/WindowsHealthCheck.yml
steps:
- powershell: |
Get-Service | where{($_.StartType -eq 'Automatic') -and ($_.Status -eq 'Running')}
displayName: List of Automatic and running services
- powershell: |
Get-Process | Sort-Object -Property CPU -Descending | Select -First 5
Now you can use this template by just providing a path of the template as shown below.
# StepTemplate.yml
trigger: none
pool:
vmImage: windows-latest
stages:
- stage: stage1
jobs:
- job: job1
steps:
- template: templates/windowsHealtchCheck.yml
- job: job2
steps:
- template: templates/windowsHealtchCheck.yml
- stage: stage2
jobs:
- job: job1
steps:
- template: templates/windowsHealtchCheck.yml
As shown above, we have used step templates at the different job levels of the same stage and the different stages as well. If you have multiple templates, you can use them as many you want.
Job Leve Template:
Similar to the steps template, Job templates can be created.
# Path: templates/WindowsHealthCheck_jobs.yml
jobs:
- job: serviceJob
steps:
- powershell: |
Get-Service | where{($_.StartType -eq 'Automatic') -and ($_.Status -eq 'Running')}
displayName: List of Automatic and running services
- job: memoryJob
steps:
- powershell: |
Get-Process | Sort-Object -Property CPU -Descending | Select -First 5
And you can call this template from the pipeline.
# Path: JobTemplate.yml
trigger: none
pool:
vmImage: windows-latest
stages:
- stage: stage1
jobs:
- template: templates/WindowsHealtchCheck_jobs.yml
- stage: stage2
jobs:
- template: templates/WindowsHealtchCheck_jobs.yml
Stage Template:
# Path: templates/WindowsHealthCheck_stage.yml
stages:
stage: stage1
jobs:
- job: serviceJob
steps:
- powershell: |
Get-Service | where{($_.StartType -eq 'Automatic') -and ($_.Status -eq 'Running')}
displayName: List of Automatic and running services
- job: memoryJob
steps:
- powershell: |
Get-Process | Sort-Object -Property CPU -Descending | Select -First 5
To call this template from the pipeline.
# Path: StepTemplate.yml
trigger: none
pool:
vmImage: windows-latest
stages:
- template: templates/windowsHealtchCheck_stage.yml
if you have multiple templates at the different levels, you can call them as many times, for example,
# Path: StepTemplate.yml
trigger: none
pool:
vmImage: windows-latest
stages:
- template: templates/windowsHealtchCheck_stage.yml
- template: templates/windowsHealtchCheck_stage2.yml
You can find the above codes in my GitHub repo below.
https://github.com/chiragce17/MyPublicRepo/tree/main/AzurePipeline
Conclusion:
Templates are very helpful, you can call them in as many pipelines to avoid repeating of code and for clean/less pipeline code. You can also pass parameters to the template, that we will cover in the future article.
Pingback: How to pass parameters to Azure yaml templates?