DevOps | Scripts | Automation

Azure DevOps

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.

Now you can use this template by just providing a path of the template as shown below.

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.

And you can call this template from the pipeline.

Stage Template:

To call this template from the pipeline.

if you have multiple templates at the different levels, you can call them as many times, for example,

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.

Loading

One thought on “How to create Azure Pipeline templates?

Comments are closed.