Advanced Azure Pipelines Using YAML

Advanced Azure Pipelines Using YAML

In previous posts, we built a pipeline by creating jobs, tasks, and triggers. In this post, you'll learn how to customize an Azure pipeline using YAML. This includes creating condition statements with variable groups to set complex conditions, offering more flexibility compared to the classic online version. For example, YAML allows you to deploy mobile applications simultaneously to both the Google Play Console and the App Store Connect.

By the end of this post, you will have learned how to create both build and release pipelines using YAML. Additionally, we will also know how to clone, export, and import YAML configurations from the classic editor in the Azure DevOps portal.

This post will cover the following topics:        

- Creating a build pipeline using YAML

- Creating a release pipeline using YAML

- Cloning, exporting, and importing a YAML pipeline

- Complex YAML configurations


Let's begin by creating a pipeline using YAML syntax.

Creating a Build Pipeline Using YAML

In this section, We'll learn how to build a pipeline using YAML, view the YAML configuration on the Azure DevOps portal, and save the YAML file in Azure Repos. To create a build pipeline using YAML, follow these steps:

1. Log in to the Azure DevOps portal, select your organization, navigate to the Pipelines page, and then click on "New pipeline."

New Pipeline

2. Click on Azure Repos Git, which is a source code repository for the demo:

Azure Repos Git

3. Click on the PacktAzureDevOps repository:

Select a repository

4. If you already have a YAML file, select "Existing Azure Pipelines YAML file." However, since we are creating a new one, click on "Starter pipeline."

Selecting a starter pipeline

5. Click on Save, and you can review a new pipeline in YAML format:

Save pipeline YAML

6. Enter a commit message to help you remember the changes made in the file, and select the "Commit directly to the main branch" option. This will save your file to the main branch.

Commit YAML to Azure Repos Git

If we would like to save your file in a new branch, then select Create a new branch for this commit. Click on Save.

7. After you click Save, you will be taken back to the main dashboard result of the build pipeline:

A build pipeline dashboard

8. Click on Run pipeline:

Run a build pipeline

After that, you can see a summary of the build pipeline results:

build pipeline result

9. You can edit the pipeline by clicking on the ellipses (…) next to the Run new button shown in the following screenshot and then Edit pipeline:

Edit Pipeline

10. You can now view the basic structure of the YAML file. Let's break down each part of the example YAML file:

A. The main branch of Azure Repos stores the YAML file.

B. This is the repository name.

C. This is the YAML filename.

D. The build pipeline will trigger any changes to the main branch.

E. The build pipeline will run on the Ubuntu operating system.

F. A script task containing a single line.

G. A script task containing multiple lines.

These components are illustrated in the following screenshot:

YAML File Structure

11. We can see the result after running the pipeline based on the YAML file in the previous screenshot by clicking on a job:

Display job result details

12. Click on Run a one-line script, which will display the Hello, world! Text. This is an example of when you would like to display the message inside the task of the Azure pipeline:

Job steps with results

Creating a Release Pipeline Using YAML

In this section, we'll learn how to create a release pipeline using YAML, including how to define stages, jobs, and tasks. Follow these steps:

1. Edit the existing pipeline by clicking on the ellipsis (…) next to it, then select "Edit pipeline."

Editing the current File

2. Replace all contents of the existing azure-pipelines.yml file, as shown in the following screenshot:

Pipeline by using two stages

There are two stages, as shown in the preceding screenshot:

  • The first stage will display the Build stage job
  • The second stage will display the Release stage

3. we can validate the syntax of the YAML file by clicking on … next to Save and clicking Validate:

YAML File Validation

4. We will see the following message if the YAML file is valid:'

Validation Complete

If the YAML file is invalid, We will see the following error message explaining which line has a problem:

Invalid YAML File

5. Click Save | Run pipeline. We can see the result of a pipeline contains two stages:

Stage Details

6. We can rerun a specific stage by expanding it and clicking on the Rerun stage:

Rerun Stage

Cloning, Exporting, and Importing a YAML Pipeline

In this section, We'll learn how to clone, export, and import a YAML pipeline from the Azure DevOps portal. These actions help you save time by duplicating and adjusting existing templates. If you need to create a new Azure pipeline, you can do so by cloning an existing one. Here are the steps to perform these tasks:

- Clone: You can quickly clone a pipeline by copying and pasting the YAML file.

Export and import: The following steps show you can export an entire YAML file from a pipeline:

A. We can export a YAML pipeline by clicking on Edit:

Edit Pipeline

B. Click on … | Download full YAML to download a file:

Download the complete YAML

C. Open a downloaded file and copy and paste it into the new pipeline you created.

Complex YAML Configurations

YAML syntax supports various complex configurations that allow for the modularization and reuse of YAML files. Examples include template reuse and the implementation of template expressions. We'll explore these features in the following sections.

YAML Template Reuse

For large projects with multiple applications developed by the same team, defining common templates to reuse is beneficial instead of writing everything from scratch for each application's CI/CD needs.

Azure Pipelines supports referencing templates to reuse steps, jobs, and stages, which reduces YAML duplication since all applications or deployment processes within the project are the same. Additionally, you can include parameters in the templates to pass values that customize the behavior of the referenced template.

Let's consider a scenario where an Azure pipeline builds the same application twice with two different build configurations in the .NET language. The file in the following screenshot defines one parameter, buildConfiguration, and three steps: installing the NuGet tool (NuGetToolInstaller@1), restoring NuGet dependencies (NuGetCommand@2), and building the solution with the Visual Studio build utility (VSBuild@1).

Dotnet-build-steps template with parameters

You can see in the following example how the same dotnet-build-steps.yml file can now be used to build the same application in two different agents, Linux-latest and windows-latest, with the same steps and having the possibility to add other tasks before and after:

Azure pipeline with template references

This feature offers great flexibility by reducing duplicate code in your YAML pipelines and promoting standardization across pipelines, which helps minimize errors.

More complex configurations can involve storing all templates in a separate repository, managed by another team responsible for assembling these building blocks. This approach supports the teams handling CI/CD pipelines, as illustrated in the following screenshot:

Another Repositry Response

YAML Template Expressions

Expressions in Azure Pipelines provide a custom syntax that allows you to dynamically resolve values during runtime, acting as control logic in your templates. While there are many types of expressions, it is important to know that you can:

- Evaluate literals and variables.

- Use built-in functions such as coalesce, contains, eq, format, and many others to evaluate logical conditions or transform values.

- Use built-in functions to evaluate the job status.

- Use conditions to conditionally insert variable values or tasks.

- Loop through parameters with each keyword.

- Evaluate dependencies on previous jobs or stages, such as status or output variables.

The following screenshot shows an example of how expressions can be used to build and test an application using two different sets of tools (the msbuild or dotnet CLI tools), selecting the tool based on a parameter:

Template Expression
This post taught you how to create build and release pipelines using YAML. This method is more powerful for developers than using the classic editor on the Azure DevOps portal, as it allows them to save YAML files in their Azure Repos, facilitating review of each revision’s pipeline. YAML-based pipelines enable more efficient, transparent, and developer-centric CI/CD processes.         

You also learned about handling complex scenarios, reducing YAML duplication, reusing templates, and adding dynamic behavior through expressions. Additionally, you explored the advantages and limitations of YAML-based pipelines.

In the next chapter, you will delve deeper into implementing a build and release pipeline using YAML, and learn how to reuse build tasks using Node.js, NPM, .NET, and Docker to build a comprehensive pipeline.

Microsoft Azure DevOps Microsoft Azure Microsoft Learn























要查看或添加评论,请登录

Ankit Ranjan (DevOps Engineer)的更多文章

  • What is Azure Pipelines?

    What is Azure Pipelines?

    Azure Pipelines Benefits of Azure Pipelines Azure Pipelines offers a fast, reliable, and secure way to automate the…

  • Installing Docker on Windows 11 using WSL 2: A Step-by-Step Guide

    Installing Docker on Windows 11 using WSL 2: A Step-by-Step Guide

    Docker has become a crucial tool for developers, enabling seamless and portable application deployment. Prerequisites…

  • Safeguard Your Azure Route Server with Azure DDoS Protection

    Safeguard Your Azure Route Server with Azure DDoS Protection

    This guide walks you through securing your Azure Route Server by integrating it with Azure DDoS Protection in a virtual…

  • Protect your public load balancer with Azure DDoS Protection

    Protect your public load balancer with Azure DDoS Protection

    Azure DDoS Protection provides advanced mitigation features like adaptive tuning, attack alert notifications, and…

  • Create a public load balancer with an IP-based backend

    Create a public load balancer with an IP-based backend

    This Edition will teach us how to create a public load balancer with an IP-based backend pool. Traditionally, an Azure…

  • Load Balancer and its Different Types

    Load Balancer and its Different Types

    Global Load Balancer The Azure Standard Load Balancer enables cross-region load balancing, providing geo-redundant high…

  • Inbound NAT Rule

    Inbound NAT Rule

    Inbound NAT rules enable connections to virtual machines (VMs) in an Azure virtual network using a public IP address…

  • Azure Availability Set

    Azure Availability Set

    As part of a high-availability deployment, virtual machines are typically organized into multiple availability sets to…

    8 条评论
  • Azure Load Balancer (Part 2)

    Azure Load Balancer (Part 2)

    What is Azure Front Door Azure Front Door helps you deliver content, files, apps, and APIs with better availability…

  • Azure Load Balancer (Part-1)

    Azure Load Balancer (Part-1)

    Load balancing involves the efficient distribution of incoming network traffic across multiple backend servers or…

社区洞察

其他会员也浏览了