Azure DevOps Build & Release with YAML Templates ๐Ÿš€

Kyler Mintah
FAUNโ€Šโ€”โ€ŠDeveloper Community ๐Ÿพ
4 min readMay 23, 2021

--

In this piece, we explore a technique for YAML pipeline modularization in Azure DevOps using YAML templates.

image credit: Microsoft Docs

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 in root directory
  • build.yaml - create in ./pipeline-templates directory
  • release.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.

azure-pipelines.yaml

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.

pipeline-templates/build.yaml

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.

pipeline-templates/release.yaml

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.

Creating pipeline from YAML file in Azure DevOps

The Result ๐ŸŽฏ

After executing your pipeline, you should see the two stages Build & Deploy.

PartsUnlimted Pipeline Execution Result

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 ๐Ÿ‘‡

--

--