Azure Pipelines provides a YAML-based configuration format to define CI/CD workflows. YAML (YAML Ain’t Markup Language) uses a simple, human-readable syntax that makes setting up pipelines straightforward and flexible. In this article, we’ll cover the key concepts of pipeline YAML, how to structure pipeline files, and essential components like triggers, jobs, and steps to create a functional CI/CD pipeline.

Understanding the Structure of a Pipeline YAML File

An Azure Pipeline YAML file typically contains the following structure:

  • trigger: Specifies which branches or pull requests trigger the pipeline.
  • stages: Defines the main phases, like build, test, or deploy.
  • jobs: A job is a set of steps that run on a specific agent, executing tasks like builds or tests.
  • steps: Individual tasks within jobs, such as running scripts, installing dependencies, or building code.

Basic Example of a Pipeline YAML

Let’s start with a simple example of an Azure Pipeline YAML file:


trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

steps:
  - task: UseNode@2
    inputs:
      versionSpec: '14.x'
    displayName: 'Install Node.js'

  - script: npm install
    displayName: 'Install Dependencies'

  - script: npm test
    displayName: 'Run Tests'

  - script: npm run build
    displayName: 'Build Application'

This example triggers the pipeline on changes to the main branch, using an Ubuntu agent, and includes steps to install Node.js, dependencies, run tests, and build the application.

Key Components of Pipeline YAML

Here’s a closer look at the core components you’ll encounter in a pipeline YAML file:

  • Trigger: Defines when the pipeline runs. Use trigger to specify branches or use pr for pull request triggers.
  • Pool: Specifies the agent pool, like vmImage: 'ubuntu-latest', to define where the pipeline runs.
  • Stages: Logical divisions of the pipeline (e.g., build, test, deploy). Each stage can have multiple jobs.
  • Jobs: Each job contains a series of steps that run sequentially within a stage.
  • Steps: The individual tasks within a job, including tasks and scripts.

Defining Triggers in YAML

Triggers specify the conditions under which a pipeline runs:

  • Branch Trigger: Runs the pipeline on specific branches. For example:
    
    trigger:
      branches:
        include:
          - main
          - develop
      
  • Pull Request Trigger: Triggers the pipeline on pull requests:
    
    pr:
      branches:
        include:
          - main
      

Using Stages, Jobs, and Steps

In more complex pipelines, you may want to break down workflows into stages, jobs, and steps:


stages:
  - stage: Build
    jobs:
      - job: BuildJob
        steps:
          - script: npm install
            displayName: 'Install Dependencies'
          - script: npm run build
            displayName: 'Build Application'

  - stage: Deploy
    jobs:
      - job: DeployJob
        steps:
          - script: echo "Deploying application"
            displayName: 'Deploy Application'

This configuration has two stages: Build and Deploy. Each stage contains jobs that run specific tasks, organized by steps.

Adding Conditions and Variables

Pipeline YAML allows you to define conditions and variables for greater flexibility:

  • Variables: Use variables for values that may change, like environment-specific settings:
    
    variables:
      nodeVersion: '14.x'
      
  • Conditions: Apply conditions to steps or jobs, for example:
    
      - script: npm test
        condition: succeeded()
        displayName: 'Run Tests'
      

Reusable Templates in YAML

Azure Pipelines support reusable YAML templates, enabling you to standardize pipelines across projects:


resources:
  repositories:
    - repository: templates
      type: github
      name: YourOrg/azure-pipeline-templates

jobs:
  - template: templates/build-template.yml

This setup allows you to reference templates stored in other repositories, reducing duplication and making your pipelines easier to manage.

Best Practices for Writing Pipeline YAML

To ensure readability and maintainability, follow these best practices:

  • Use Indentation Consistently: YAML is whitespace-sensitive, so consistent indentation is crucial.
  • Organize by Stages: Break down workflows into stages for logical flow.
  • Minimize Hard-Coded Values: Use variables to avoid hardcoding paths or credentials.
  • Comment Your YAML: Adding comments improves readability and helps other team members understand each step.

Conclusion

Understanding the basics of Azure Pipeline YAML is essential for setting up efficient CI/CD workflows. By defining triggers, stages, jobs, and steps, you can create flexible and automated pipelines that adapt to your project’s needs. As you gain experience, you’ll be able to customize pipeline YAML configurations further, making Azure Pipelines a powerful tool for your development lifecycle.