Today in the Era of DevOps and automation, we expect everything to be as quick as the click of a button and whenever the term ‘DevOps’ comes to our mind a bunch of CI-CD tools pop up automatically, Azure DevOps is on such tool of capabilities.
None of us wants to use the conventional ways to of adding manual steps to our DevOps implementation these days, which is where Azure DevOps provides us with the option of Azure pipelines which we opted for our project implementation but still we don’t want to write the same pipeline code for every application, that’s where the Azure pipeline template steps in, we will discuss how I setup and implement Azure pipeline template in my projects.
The concept is pretty straight forward as we know azure pipelines uses ‘YAML’ for pipelines we will be breaking our entire pipeline in a bunch of reusable components and every component will be a independent yaml file. Select a repository to keep all the template files, the repository could be a github repository or Azure git repository, we will discuss about both.
First we will discuss about the github approach:
- setup a github repository
- Once the repository is set generate a personal access token by navigating to the ‘developer settings’ in your github account.
- Now we will push all your template files to the repository, while pushing we have to be careful about the section where we want to use the template i.e. the ‘Stage’ level or the ‘step’ level, you can refer the below link to get more info about the azure pipeline hierarchy:
- If you would like to use the template at the step level the code would look something like this
- If you would like to use the template at the stage level the code would look something like this , a stage level template will comprise of all the steps in a single yaml file
- Once done we are all set with our github repository and we can move ahead to configure our DevOps project.
- Navigate to your ‘Project Settings’, there setup a new github service connection and use the token created in the above step
- Now we will move to our pipeline script, where we have to mention the template repository in order for Azure to identify the templates being referenced.
- we will put this chunk of code in the ‘resources’ section, as it is a github repository we will keep the type as ‘github’
- Now we can proceed towards consuming our pipeline template, use the syntax below:
- template: <template_file_name>@<endpoint_name>
- Who doesn’t like a ‘One Stop Shop’ for their work, well Azure is one such resource, it does provide it’s integrated git repositories. In this section we will discuss how to setup our template repository in Azure itself.
- This part is pretty straight forward as we are setting up the repository in azure itself so, we don’t have to explicitly notify azure about it’s authenticity.
- Setup a new git repository in your Azure DevOps project and place all your template files there and you are good to go.
- Now we just have to refer this template repository in our pipeline script the same way we did for our github repository, the only differences would be that we will only be using ‘git’ in the ‘type’ parameter and we won’t be specifying any endpoint because our entire setup is in Azure.
- You can access the templates in the same manner that we did above.
There you have it, your setup is ready, now just rev up your templates and get your pipelines going.
There are still a lot of things which needs to be explored in Azure pipeline, if you guys have any feedback or suggestion, please let us know in the comment.
Till that time, happy reading!!!
Opstree is an End to End DevOps solution provider