YAML Pipelines: Automate Your CI/CD with Code

Azure Pipelines allows you to define your continuous integration and continuous delivery (CI/CD) workflows as code using YAML. This approach offers several advantages, including version control, reusability, and a more declarative way to manage your build and release processes.

What is a YAML Pipeline?

A YAML pipeline is a configuration file written in YAML format that describes the stages, jobs, and steps involved in your CI/CD process. Azure DevOps reads this file to understand how to build, test, and deploy your application.

Key Concepts in YAML Pipelines

  • Pipeline: The overall workflow for your CI/CD process.
  • Stages: Major divisions of your pipeline, often representing different environments (e.g., Build, Test, Production).
  • Jobs: A collection of steps that run on an agent. Jobs within a stage can run in parallel or sequentially.
  • Steps: The smallest unit of work in a pipeline. A step can be a script, a task, or a command.
  • Agents: The compute resource that runs your pipeline jobs. These can be Microsoft-hosted agents or self-hosted agents.
  • Triggers: Events that initiate a pipeline run, such as code commits or scheduled times.

Getting Started with a Basic YAML Pipeline

Let's look at a simple example of a YAML pipeline that checks out code from a repository and runs a script.

pipeline: '' trigger: - main # Trigger on commits to the main branch pool: vmImage: 'ubuntu-latest' # Use a Microsoft-hosted Ubuntu agent steps: - script: echo Hello, world! displayName: 'Run a one-line script' - script: | echo Add other tasks to build, test, and deploy your app. echo This is a multi-line script. displayName: 'Run a multi-line script'

Understanding the YAML Structure

  • pipeline: '': Defines the name of your pipeline.
  • trigger:: Specifies when the pipeline should run. Here, it's set to run on commits to the main branch.
  • pool:: Defines the agent pool to use. vmImage: 'ubuntu-latest' specifies a Microsoft-hosted Ubuntu agent.
  • steps:: A list of tasks or scripts to execute.
    • script:: Executes a script.
    • displayName:: Provides a human-readable name for the step.

More Advanced Concepts

YAML pipelines support a wide range of features, including:

  • Service Connections: Securely connect to external services for deployment.
  • Variable Groups: Manage and reuse variables across pipelines.
  • Templates: Create reusable pipeline components to promote consistency and reduce duplication.
  • Environments and Approvals: Define deployment targets and control when deployments occur.
  • Complex Workflows: Build sophisticated multi-stage and multi-job pipelines.

To learn more about specific YAML syntax and advanced configurations, please refer to the Templates and Variables sections.