Azure Pipelines YAML Templates Demystified
Davide 'CoderDave' BenvegnΓΉ
Posted on January 19, 2021
Azure Pipelines has a very useful feature that allows you to create templates with task you want to share and use them across multiple pipelines.
In today's post we are going to see what Templates are, why they are important and how we can create and use them.
Let's get into it.
What?
Templates allow us to define reusable content, logic, and parameters, keeping our main pipeline definitions focused on the application. They are also a great way of sharing common logic in a centralized way, without duplicating it in many pipelines.
Essentially, we can define reusable code in separate templates.
If you have worked in the past with Task Groups in the Classic Pipelines, YAML Templates are somehow similar but quite more powerful.
We can include templates within templates and define four types of templates:
- Stage template, to define a set of stages of related jobs
- Job template, to define a collection of steps run by an agent
- Step template, to define a linear sequence of operations for a job
- Variable template, as an alternative to hard coded values or variable groups
Just as a side note, Azure Pipelines currently support a maximum of 100 separate template files in a pipeline, and no more than 20 level of template nesting. Which I think it is more than enough for a normal use. If you need more than that in a pipeline, it's probably better if you re-architect it.
Why?
Anyway, why are YAML Templates so important? Well, there are a number of Reasons.
Guardrails
First, as guardrails provide boundaries on the roads, templates can be used for the same purpose on a pipeline. In fact we can use templates to provide alignment to architecture, security and development.
Do you have some task or operation you want every pipeline in the project to do?
Make it part of a template, and enforce a policy to make sure all the pipelines use that template. If you wanna know how to enforce that policy, check the video about the Environment Gates and Checks.
Consistency and Speed
Other reasons are consistency and speed. Organizing the most common operations in templates, the development of a pipeline will take much less time.
Let's say that you often build container images for your software, and to do so you have to add many steps to build the image, tag it properly, test it for vulnerabilities and finally push it to a container registry. That is easily a 4 or 5 steps process that you need to configure every time.
If instead you save that into a template, you will have to use a single step in your pipeline, which is the template, passing it only the parameters that change, like the image name, repo name, etc, making it much faster to write.
Also, by doing so you ensure that all the pipelines which build a container image will be consistent one another because they all use the same template.
And of course this applies to each and every operation you can think of.
Centralize editing
Moreover, if you have many pipelines using the same template changes became very easy to make.
What if you perform some operation in many pipelines and you realize that you need to change a parameter, or maybe the command changed in a new version of a tool? If you are not using templates, you would need to go into each and every pipeline definition and make that change. If instead you are using a template for that operation, you just have to change that template and all the next pipeline runs will use the modified version.
Simplicity
Finally, using templates allows you to keep your pipeline definitions simpler, focusing only on the application-specific tasks and operations.
How?
Alright, enough talking. Let's actually see how to build a template, and how to use it.
For this, I have created this video that shows step by step how to do that:
You might want to skip to the demo at around minute 4:00
(Link to the video: https://youtu.be/UQlRITs7veM)
Conclusions
Alright, I think that's enough for today. This was an introduction to Azure Pipelines Templates. In the next posts and videos about this topic we will go more in depth into it, looking at more complex examples and scenarios. If you are interested, consider following me here and subscribing to my channel so you won't miss it.
Let me know in the comment section below what you think about the Azure Pipelines Templates and if you use them already.
Like, share and follow me π for more content:
π½ YouTube
β Buy me a coffee
π Patreon
π CoderDave.io Website
π Merch
π¦π» Facebook page
π±βπ» GitHub
π²π» Twitter
π΄π» LinkedIn
π Podcast
Posted on January 19, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.