Azure DevOps Build & Release with YAML Templates ๐
In this piece, we explore a technique for YAML pipeline modularization in Azure DevOps using YAML templates.
Whether you are on a development team or working on a solo project, there are a few reasons why you might consider using templates for your Azure DevOps pipelines.
Organizing your pipeline into multiple file components in your version control system allows for ease of substitution & reuse of each job
, stage
or step
.
In terms of team collaboration โ templates allow for ease of code reviews for individual pipeline components, efficiency gains through modularity as your pipelines mature, and even better security.
Getting Started
This walkthrough assumes experience working with Azure Pipelines and at least familiarity with Azure Pipeline YAML syntax.
In this example, I will be building and releasing the PartsUnlimited
template website from the ADO Demo Generator through the use of a pipeline built using multiple YAML files.
Here are the requirements one should take note of in order to complete these steps:
- Azure DevOps Account โ visit dev.azure.com and sign in with your account.
- PartsUnlimted Project โ visit the Azure DevOps Demo Generator and sign in with your ADO account. Follow the prompts to generate a
PartsUnlimited
project into your ADO organization - Azure Subscription โ to release our website via an Azure App Service you need a linked subscription from portal.azure.com.
- Azure Service Connection โ to use Azure resources you will also need a valid service connection.
- YAML for ADO Pipelines โ In the example below we will be using pure YAML for our ADO build & release pipeline. Make sure you are at least familiar with this process. Pipeline basics & YAML schema for reference.
1 | Creating the YAML files
Before we create our Pipeline, we need to create the following YAML files in our project directory:
azure-pipelines.yaml
- create inroot
directorybuild.yaml
- create in./pipeline-templates
directoryrelease.yaml
- create in./pipeline-templates
directory
The Main Pipeline File ๐ง
We will use the azure-pipelines.yaml
file as our top-level YAML file.
This is the file we will directly link to our Pipeline. In this file, we specify the template files to use for the individual pipeline stages. See line 11 & 12.
We use the build.yaml
template file for our build stage, and the release.yaml
template file for our release stage :-)
Note that these files exist in a directory in our repository and that we could add additional stages using other custom templates as necessary (e.g. QA)
Also note, that in our release stage, we need to pass parameters from our Pipeline variables. In this case, I have specified these variables through the ADO pipeline UI. See line 13 to 18.
The Build Stage ๐ฆ
Here we examine the build.yaml
template file.
This is the file that defines the build stage of our pipeline. In this file, we specify the various build parameters involved in packaging our project ahead of the release stage.
Notice the various tasks, associated here, including VSTest@2 task on line 39 which will run a series of test cases.
The Release Stage ๐
Here we examine the release.yaml
template file.
This is the file that defines the release stage of our pipeline. This is where we create our deployment to an Azure App Service. Notice how we define the default parameters in case they are not explicitly passed from the top-level file.
Also note that on line 28 you will need to substitute the azureResourceManagerConnection
value for an appropriate service connection, and add your subscriptionId
to line 29 below. On line 47 replace the azureSubscription
value with your service connection name.
2 | Creating the Pipeline
Select Main YAML file ๐
In Azure DevOps, create a new pipeline with YAML and when you get to the Configure step, make sure to choose the Existing Azure Pipelines YAML file. Select azure-pipelines.yaml
from the Path dropdown as shown below.
The Result ๐ฏ
After executing your pipeline, you should see the two stages Build
& Deploy
.
If you have correctly configured your pipeline environment variables, service connection, and Azure subscription you should be able to deploy this example site successfully.
From here you should be able to visit the URL associated with your website to see the deployed site.
Conclusion
Using templates can be helpful in defining comprehensive pipelines that would provide more functional value when modularized. Feel free to take this concept and apply it to your own development efforts. With YAML you can create a range of complex processes made simple through modularization.
Further Reading
Pipeline Templates
Building GitHub Repositories
Release Approvals and Gates Overview
Gates in YAML Pipelines
Join FAUN: Website ๐ป|Podcast ๐๏ธ|Twitter ๐ฆ|Facebook ๐ฅ|Instagram ๐ท|Facebook Group ๐ฃ๏ธ|Linkedin Group ๐ฌ| Slack ๐ฑ|Cloud Native News ๐ฐ|More.
If this post was helpful, please click the clap ๐ button below a few times to show your support for the author ๐