AZ-400: Designing and Implementing Microsoft DevOps Solutions

Design and Implement Pipelines

Exploring YAML Pipelines

In this guide, we’ll dive into YAML pipelines for Azure Pipelines. YAML (YAML Ain’t Markup Language) is a human-friendly data serialization format used to define CI/CD workflows. You’ll discover why YAML pipelines are crucial for continuous integration and delivery, how they differ from classic builds, and best practices to streamline your DevOps process.

What Is YAML?

YAML (YAML Ain’t Markup Language) is a lightweight, human-readable format for configuration files and data exchange. Since 2001, YAML has powered many DevOps tools—Kubernetes resources, for instance, are declared in YAML. Its syntax emphasizes readability and simplicity, making it ideal for defining complex pipeline logic.

Simple Azure Pipelines Example

trigger:
  - main

pool:
  vmImage: 'ubuntu-latest'

steps:
  - task: NodeTool@0
    inputs:
      versionSpec: '14.x'
    displayName: 'Install Node.js'

  - script: |
      npm install
      npm run build
    displayName: 'Install dependencies and build'

Benefits of YAML Pipelines

  • Version Control: Keep your pipeline definitions alongside application code.
  • Reusability: Share pipeline snippets across multiple repositories.
  • Flexibility: Customize each stage, job, and step to fit team needs.
  • Collaboration: Your entire team can review and update the pipeline file.

Basic YAML Syntax

Before building pipelines, master these core YAML rules:

  • Indentation: Use spaces only (no tabs) to denote hierarchy.
  • Key-Value Pairs: Written as key: value.
  • Lists: Each item starts with - .
  • Mappings: Nest key-value pairs under parent keys.
  • Multiline Strings:
    • Literal block (|): preserves newlines.
    • Folded block (>): folds newlines into spaces.

Note

Always validate your YAML with an online linter or yamllint to catch indentation and formatting errors before committing.

Example: Dictionaries, Lists, and Variables

stages:
  - stage: Build
    displayName: "Build Stage"
    jobs:
      - job: BuildJob
        displayName: "Build Job"
        pool:
          vmImage: "ubuntu-latest"
        steps:
          - script: |
              echo "Building the project..."
              echo "Environment: ${{ variables.environment }}"
              echo "Packages:"
              for pkg in "${{ variables.packages }}"; do
                echo "- $pkg"
              done

Core Pipeline Concepts

  • Stages: Logical sections (e.g., Build, Test, Deploy).
  • Jobs: Units of work that run in parallel or sequentially.
  • Steps: Individual tasks or scripts executed in a job.

The image illustrates a YAML pipeline structure, showing the flow from a trigger through stages, jobs, and steps, with tasks like publishing build artifacts and deploying Azure App Services.

Minimal Pipeline Example

trigger:
  - main

pool:
  vmImage: 'ubuntu-latest'

stages:
  - stage: Build
    jobs:
      - job: BuildJob
        steps:
          - script: echo "Hello, world!"
          - task: PublishBuildArtifacts@1

Creating Your First YAML Pipeline

  1. Select your code repository (GitHub, Azure Repos, Bitbucket, etc.).
  2. In Azure DevOps, choose PipelinesCreate Pipeline.
  3. Let the wizard scaffold a starter YAML file.
  4. Review, customize, and commit the YAML—your pipeline runs on every push.

The image is a guide for creating a YAML pipeline, showing steps to select a repository from a list. It includes a sidebar with steps and a dark interface for selecting a repository.

Automating with Triggers

Azure Pipelines supports multiple trigger types:

Trigger TypeDescription
CI TriggersBuild when code is pushed to a branch
PR TriggersValidate pull requests before merge
Scheduled TriggersRun pipelines at defined intervals
trigger:
  - main

pr:
  - develop

schedules:
  - cron: "0 2 * * *"
    displayName: Daily midnight build
    branches:
      include:
        - main

Continuous Deployment

Automate deployments by defining environments and stages:

  1. Create stages for Testing, Staging, and Production.
  2. Add deployment jobs targeting each environment.
  3. Chain stages so a successful build or approval triggers the next stage.

This end-to-end flow ensures rapid, reliable delivery from code to users.

Advanced Features

Leverage these techniques for more sophisticated pipelines:

  • Multi-stage Pipelines: Combine build, test, and deploy in one YAML.
  • Templates: Share and reuse common steps across multiple pipelines.
  • Conditions & Dependencies: Control execution with dependsOn, condition, and runtime expressions.

The image shows a diagram of an advanced YAML pipeline with stages labeled Development, Stage, and Production, along with features like multi-stage pipelines, using templates for reuse, and conditional insertions and dependencies.

Best Practices

  • Keep YAML files tidy with clear comments and consistent indentation.
  • Store secrets securely using Azure Key Vault or pipeline variables.
  • Test pipeline changes in a feature branch before merging to main.

Warning

Never commit plain-text credentials or secrets in your YAML. Always use secured variables and secret scopes.

The image outlines best practices for YAML pipelines, focusing on readability, securing sensitive data, and testing strategies. It features three colored sections with icons and text.

Watch Video

Watch video content

Previous
Exploring Classic Pipelines