DEV Community

loading...
Cover image for Azure Pipelines YAML Templates Demystified

Azure Pipelines YAML Templates Demystified

Davide 'CoderDave' Benvegnù
DevOps Architect @ Microsoft 👨‍💻 • YouTuber @ CoderDave 🎥 • Ex MMA fighter 🥊
Updated on ・4 min read

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.

Sample Template

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:

Template Types

  • 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.

Required Template

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.

Docker params

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.

Edit

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

Buy Me A Coffee

Discussion (5)

Collapse
pacroy profile image
Chairat Onyaem (Par) • Edited

Hi Davide,
Do you know anyway we can reuse template inside another template that reside in different repositories?

  • RepoX/Pipelines1
    • RepoA/templateA
      • RepoB/templateB
Collapse
n3wt0n profile image
Davide 'CoderDave' Benvegnù Author • Edited

I've actually never tried.

Using a template from a different repo is very easy, just reference it in a resource (see this )

But I don't know if that works "recursively"

Collapse
pacroy profile image
Chairat Onyaem (Par)

Thanks for input. I end up putting all the templates into the same repository (but in separated folders) now.

Collapse
ahethaysham profile image
Ahethaysham Ahmed

Hi, Lets say that I have a dotnet project. Is it possible to identify the templates needed? How can we do that? Thanks!

Collapse
n3wt0n profile image
Davide 'CoderDave' Benvegnù Author

Well that really depends on how you manage your projects. For example, what I would do is creating a template that restore nuget packages, does build, execute tests, scan for security and publish the project. This way 5 or 6 steps can be reused in all your pipelines via just 1 template. And then your pipelines just need to take care of deployment or any other operation that is not common to all projects but specific to each one

Forem Open with the Forem app