A Complete Guide to Azure DevOps Pipelines with YAML Templates
YAML Basics: Syntax and Structure
YAML is structured around key-value pairs, lists, and nested structures, allowing you to define complex configurations in a clear and concise manner.
Key-Value Pairs
key: value
Lists
- item1 - item2 - item3
Nested Structures
parent: child1: value1 child2: value2
Comments
# This is a comment key: value
YAML Structure in Azure DevOps Pipeline
Azure DevOps pipelines use YAML to define the configuration for your CI/CD workflows. The structure of a YAML pipeline typically consists of the following sections.
Trigger
The trigger section defines the events or conditions that will trigger the execution of your pipeline. Triggers can be based on specific branches, tags, or even schedules.trigger: branches: include: - main - feature/*
Stages
Stages represent the major phases of your CI/CD process. Each stage can contain one or more jobs. Stages are executed sequentially, allowing you to define a logical progression of tasks.
stages:
- stage: Build
jobs:
- job: BuildJob
# Job configuration goes here
- stage: Test
jobs:
- job: TestJob
# Job configuration goes here
Jobs
Jobs represent the individual units of work within a stage. Each job can consist of one or more steps, defining the tasks to be executed. Jobs can run in parallel or sequentially, depending on your requirements.
jobs:
- job: BuildJob
steps:
- script: echo "Building..."
# Step configuration goes here
- job: TestJob
steps:
- script: echo "Running tests..."
# Step configuration goes here
Steps
Steps define the tasks that need to be executed within a job. Each step represents a specific action or command. Steps can include scripts, commands, or calls to predefined tasks or extensions
steps: - script: echo "Step 1" displayName: 'Run Script 1' - task: PublishPipelineArtifact@1 inputs: targetPath: '$(Pipeline.Workspace)' artifact: 'my-artifact'
Configuring the YAML Templates
YAML templates provide a convenient way to define and reuse pipeline configurations.
Next, let's configure the YAML templates for the build and test stages. Create two YAML template files named build.yml
and test.yml
in the templates/
directory and populate them with the following contents:
build.yml
jobs:
- job: BuildJob
pool:
vmImage: 'ubuntu-latest'
steps:
- script: |
echo 'Building...'
# Add build commands here
test.yml
jobs:
- job: TestJob
pool:
vmImage: 'ubuntu-latest'
steps:
- script: |
echo 'Running tests...'
# Add test commands here
Create azure-pipelines.yml
trigger:
branches:
include:
- main
stages:
- template: templates/build.yml
- template: templates/test.yml
Configuring Triggers and Conditions
Triggers and conditions allow you to control when your pipeline should run.
Branch Triggers
Branch triggers enable you to specify which branches in your repository should trigger the pipeline. By defining branch filters, you can configure the pipeline to run whenever changes are pushed to specific branches. Here's an example of a branch trigger configuration:
trigger:
branches:
include:
- main
- feature/*
Scheduled Triggers
Scheduled triggers enable you to schedule pipeline runs at specific times or intervals. This is useful for running periodic tasks, such as nightly builds or scheduled deployments. Here's an example of a scheduled trigger configuration:
trigger:
schedules:
- cron: "0 0 * * *"
Using Conditions in Azure DevOps Pipelines
Conditions allow you to control when a specific job or step should execute within a pipeline. Conditions are evaluated based on expressions that can include variables, comparison operators, and logical operators.
Job Conditions:
jobs:
- job: BuildJob
condition: eq(variables['BuildType'], 'Release')
steps:
- script: echo "Building..."
Step Conditions
jobs:
- job: BuildJob
steps:
- script: echo "Building..."
condition: succeeded()