MSDN Tutorials

Learn, Build, Innovate

Understanding YAML Pipelines in Azure DevOps

Welcome to this comprehensive tutorial on crafting powerful CI/CD pipelines using YAML in Azure DevOps. YAML pipelines offer a code-based approach to defining your build and release processes, providing version control, reusability, and a clear audit trail.

What are YAML Pipelines?

Instead of configuring your pipelines through a graphical user interface, YAML pipelines allow you to define your entire CI/CD workflow in a single YAML file. This file is typically stored in your repository alongside your application code, enabling you to version your pipeline definitions just like your code.

Why Use YAML Pipelines?

  • Version Control: Track changes to your pipeline definitions over time.
  • Code Review: Subject pipeline changes to the same review process as your application code.
  • Reusability: Create templates and reusable components for common pipeline tasks.
  • Branching and Merging: Manage pipeline variations and evolution with Git branching.
  • Flexibility: Define complex workflows with stages, jobs, steps, and more.

Your First YAML Pipeline

Let's start with a basic example. Create a file named azure-pipelines.yml in the root of your repository.

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

steps:
- script: echo Hello, world!
  displayName: 'Run a one-line script'

- script: |
    echo Add other tasks to build, test, and deploy your project.
    echo Make sure to refer to Azure DevOps Pipelines documentation for details.
  displayName: 'Run a multi-line script'

Key Concepts

  • trigger: Defines when the pipeline should run (e.g., on commits to specific branches).
  • pool: Specifies the agent pool where the job will run.
  • vmImage: Selects the virtual machine image for the agent (e.g., 'ubuntu-latest', 'windows-latest', 'macos-latest').
  • steps: A sequence of tasks or scripts to be executed.
  • script: A built-in task to run inline scripts.
  • displayName: A user-friendly name for a step that appears in the pipeline logs.

Advanced Features

YAML pipelines support a rich set of features:

  • Stages, Jobs, and Steps: Organize your pipeline into logical units.
  • Templates: Create reusable pipeline components.
  • Variables: Define and use variables for configuration.
  • Conditions: Control the execution of steps based on certain criteria.
  • Service Connections: Securely connect to external services.
  • Artifacts: Publish and consume build outputs.

Resources

"The best way to predict the future is to create it." - Peter Drucker