Templates in Azure Pipelines: What, Why, and How

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
azure pipelines has a very useful feature that allows you to create templates with tasks you want to share and use them across different pipelines in today's video we are going to see what templates are why they are important and how to use them let's get into it [Music] hi everybody welcome back to code dave today i want to talk about azure pipelines and specifically about yaml templates templates allow us to define reusable content logic and parameters keeping our main pipeline's definition focus 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 different templates if you worked in the past with task groups for the classic pipelines yaml templates are somehow similar but way more powerful we can in fact include templates within templates and define four different kind of templates we can have stage templates to define a set of stages of related jobs job templates to define a collection of steps run by an agent step templates to define a linear sequence of operations for a job and finally variable templates as an alternative to hard-coded values or variable groups just as a side note azure pipelines currently support up to 100 template files for each pipeline and no more than 20 level of nesting which is templates within templates if you find yourself to require more than that i think it's time for you to re-architect your pipeline because you know it's getting quite complex anyway why are yaml templates so important well there are a number of reasons first as guard rails provide boundaries within a road we can use templates for the same purpose on a pipeline in fact using templates we can provide alignment to architecture security or development practices do you have some tasks or operations that every pipeline have to do well make it part of a template and enforce a policy to make sure all the pipelines use that template if you want to know how to enforce that policy you can check the video i made about environments gates and checks you can find a link up here and in the video description other reasons are consistency and speed organizing in fact the most common use operations and tasks in templates will shorten the development period for a pipeline 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 four or five 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 make sure that all your pipelines building images are consisting one another because they use the same template and of course the container image build is just an example these apply to basically any recurrent operation you can think of moreover if you have many pipelines using the same template changes become 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 change in a new version of a tool if you're not using templates you will need to go into each and every pipeline definition and make that change if instead you're using a template for that operation you just have to change the template and the job is done all the next pipelines runs we'll use the modified version finally using templates allows you to keep your pipeline definitions simpler focusing only on the application specific tasks and operations but enough talking let's see now how we can build our templates and how we can use them but before we move into that hit the like button below if you're enjoying this video or you find it insightful it will help this video to be recommended to other viewers and would mean a lot to me first of all to create template files you don't follow the same approach for pipelines you don't go to the pipeline section but you need to create directly the files into the repo usually the templates are put in a templates folder but that is totally optional and then can have either the yml or the yaml extension since writing yaml directly in a text file is not that easy we all know that yaml is quite picky with syntax etc one recommended approach is to use the pipeline editor to fill in the yaml and then take the yaml and put it in a separate file here i have four kind of templates step templates stages templates and job templates plus i have a jobs template with parameter that we will see later let's take a look first at the step template as you can see here is just the snippet of a pipeline basically in which i'm going to define the steps and in each step one or more tasks that have to be executed and this can be any kind of task that you will normally execute inside a pipeline the steps template is the lowest level of template we can have we cannot have a template just for a single task because that wouldn't make sense to see how this works let's move to the pipeline section here i have these templates one steps pipeline which uses the steps template in fact as you can see here this is a normal pipeline with the trigger with the job definition but as you can see inside the job we have the template call this is the template file i've just shown you and the interesting thing here is that if you look at the other job we do have the template but we can also see that we can actually have normal steps together with the template as well as you can see here and here so this job here will only use the steps inside the template while this other job over here will use both the steps defined in the pipeline and the step from the template let's see this in action let's run it and if we open the running one we can see that we have the script from the pipeline the execution of the template and finally the other script from the pipeline while if we open the linux one we can see that we only have the tasks defining the template because that specific job didn't have any other task let's go back and see the jobs template as you can see here the jobs template is very similar in the sense that is just a snippet of yaml from a pipeline but in this case it has the jobs keyword and it defines two jobs inside it we have the ubuntu job and the windows job once again if we want to see this in action we can go to the template to jobs pipeline and we can see here that once again we have the template keyword that links this template file to this pipeline we also have another job which is a job that belongs to the pipelines directly let's see this in action and i would expect to have three jobs the two defining the template and the one defined in the pipeline directly and in fact here we have it we have the ubuntu the windows and the job from the pipeline lastly let's take a look at the stages template once again just simple yammo but this time we have the stages keyboard and this contains the whole structure from stages jobs steps and tasks for the last time if we want to take a look at the pipeline let's see the template's three stages we can see that once again we have the template usage which uses the file i just shown you and as before we also have another stage that belongs directly to the pipeline if we run this we can see that we have the two stages this one from the pipeline is executed first because it's first in the pipeline definition and then we also have of course this stage from template which is the one defined in the template file of course these three templates types can be mixed and matched as i said before they can be nested which means that i can have for example a stages template that inside utilize a bunch of job templates and those job templates inside may use some steps templates and so on so forth cool right this was very simple wasn't it let's make it a little bit more realistic now passing it some parameters passing parameters to a template is really easy let's take a look at this this is the part we've seen before which contains the jobs but in this template we also have over here this parameters section and i have two parameters in here the job name and the vm image and each one has a default value the default value is useful in case you don't want to pass a specific parameter or for some reason that parameter is null and in that case of course the default value will be used instead if we take a look at the bottom part of the template we see how we can use parameters it's basically like a normal variable using the dollar parenthesis parentheses syntax and we just use the parameters keyword plus the name of the parameter in this case parameters.job name or parameters.vmimage what is really interesting here i can even define the name of the job with the parameter and everything in it to pass these parameters to the template of course we need to change our pipeline definition and i've done so in the templates four parameters if we see its structure we can see how to use the parameters as you can see here i use the same template twice but then i have these parameters keywords over here and in each one i'm sending a value for both parameters once to linux with the vm image ubuntu 1604 and the other one for windows with vs 2017 win 2016. as i said before since i set the default value for these parameters i could for example not specify the job name if i wanted to and the template would use the default value let's see this in action but as you can expect we will have two jobs one running on linux and one running on windows and in fact so it is and this was also easy right but what if i need to pass to that template a parameter there is an output variable from the results of our tasks well this is where it gets a little bit more interesting based on what we've seen so far to use a parameter that is an output variable from another job we will do something like this right we would define a parameter let's say that the storage account name comes from an armed deployment so we don't have it or coded it comes from another step which is in the pipeline and then in the pipeline we will set the value for this parameter and finally we will reuse the value in this way with parameters dot the name of the parameter unfortunately if we do this our pipeline will fail because output variables from other tasks are not expanded if we use this syntax so in here instead of finding my actual storage account name i will find the expression that allows me to use the output variable as the one for example that you can see on the bottom of the screen if we want to have the actual storage account name we need to use a little workaround in fact before using that parameter we need to transform it if you will or more correctly we need to assign its value to a variable when we do this operation in fact the expression that is contained in the parameter is actually evaluated is expanded and the actual value is assigned to the variable and then in the steps we actually have to use the variable name instead of the parameter if we do this everything will work it's not really very complex but you need to remember that you need to assign the parameter to a value in order for it to be evaluated if it's an expression all right i think that's enough for today this was a introduction to the azure pipelines templates and in the next video we will go deeper into this concept looking at more complex examples and scenarios if you are interested into it you may consider subscribing so you will not miss the new video let me know in the comment section below what you think about the azure pipeline templates and if you use it already azure pipeline says a very usual usual just as a side note templates no well there are a number for s guard lights guard lights asgard isguardly as guardrails changes become very easy to do easy but that's it for me thanks so much for watching i really hope you enjoyed it hit the like button below subscribe if you haven't already and i see you in the next video here at color dave [Music]
Info
Channel: CoderDave
Views: 26,388
Rating: undefined out of 5
Keywords: CoderDave, Davide Benvegnu, how to use pipelines template, templates in azure pipelines, azure pipelines yaml templates, azure pipelines templates, Introduction to YAML Templates in Azure Pipelines, templates in azure pipelines what why and how, templates in azure pipelines introduction, azure pipelines template introduction, how to use azure pipelines template, azure devops pipeline template, azure pipelines, azure devops, azure devops pipelines, azure devops yaml templates
Id: UQlRITs7veM
Channel Id: undefined
Length: 12min 59sec (779 seconds)
Published: Tue Jan 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.