OpsCanary
azuredevopsPractitioner

Mastering YAML Templates in Azure Pipelines for Reusable Processes

5 min read Microsoft LearnJul 26, 2026Reviewed for accuracy
Share
PractitionerHands-on experience recommended

YAML templates in Azure Pipelines exist to streamline your CI/CD processes. They allow you to define reusable content, logic, and parameters, which can significantly reduce redundancy and enhance maintainability. When you include a template, it acts like an include directive in programming, injecting predefined content into your pipeline. Extending templates goes a step further by enforcing a structure and logic that your pipeline must adhere to, which is crucial for maintaining security and compliance.

The configuration parameters you can define include buildSteps, which is a list of steps to execute, and name, which provides defaults for unspecified parameters. For example, in a template file, you might define buildSteps as a step list with a default value of an empty array. This allows you to customize the steps in your pipeline while keeping the core logic intact. The use of templates can help manage complexity, but be cautious: Azure imposes limits on template nesting and file inclusion to prevent runaway growth in pipeline size and complexity.

In production, leveraging YAML templates effectively can lead to cleaner, more maintainable pipelines. However, remember that template files must exist on your filesystem at the start of a pipeline run; you can't reference them in artifacts. Additionally, keep an eye on the limits imposed by Azure to avoid performance issues. Templates can make your pipelines more powerful, but they can also introduce complexity if not managed properly.

Key takeaways

  • Define reusable content using includes templates to reduce redundancy.
  • Enforce pipeline structure with extends templates for better security.
  • Utilize parameters like buildSteps to customize your pipeline execution.
  • Be aware of Azure's limits on template nesting and file inclusion to avoid performance issues.
  • Ensure template files are present at runtime to prevent pipeline failures.

Why it matters

Using YAML templates can significantly enhance the maintainability and security of your CI/CD processes, allowing teams to scale their operations without sacrificing quality or consistency.

Code examples

YAML
1# File: start-extends-template.yml
2parameters:
3- name: buildSteps # the name of the parameter is buildSteps
4  type: stepList # data type is StepList
5  default: [] # default value of buildSteps
6stages:
7- stage: secure_buildstage
8  pool:
9    vmImage: windows-latest
10  jobs:
11  - job: secure_buildjob
12    steps:
13    - script: echo This happens before code 
14      displayName: 'Base: Pre-build'
15    - script: echo Building
16      displayName: 'Base: Build'
17
18    - ${{ each step in parameters.buildSteps }}:
19      - ${{ each pair in step }}:
20          ${{ if ne(pair.value, 'CmdLine@2') }}:
21            ${{ pair.key }}: ${{ pair.value }}       
22          ${{ if eq(pair.value, 'CmdLine@2') }}: 
23            # Step is rejected by raising a YAML syntax error: Unexpected value 'CmdLine@2'
24            '${{ pair.value }}': error         
25
26    - script: echo This happens after code
27      displayName: 'Base: Signing'
YAML
1# File: azure-pipelines.yml
2trigger:
3- main
4
5extends:
6  template: start-extends-template.yml
7  parameters:
8    buildSteps:  
9      - bash: echo Test #Passes
10        displayName: succeed
11      - bash: echo "Test"
12        displayName: succeed
13      # Step is rejected by raising a YAML syntax error: Unexpected value 'CmdLine@2'
14      - task: CmdLine@2
15        inputs:
16          script: echo "Script Test"
17      # Step is rejected by raising a YAML syntax error: Unexpected value 'CmdLine@2'
18      - script: echo "Script Test"
YAML
1# File: azure-pipelines.yml
2
3jobs:
4- job: Linux
5  pool:
6    vmImage: 'ubuntu-latest'
7  steps:
8  - template: templates/insert-npm-steps.yml  # Template reference
9- job: Windows
10  pool:
11    vmImage: 'windows-latest'
12  steps:
13  - template: templates/insert-npm-steps.yml  # Template reference

When NOT to use this

The official docs don't call out specific anti-patterns here. Use your judgment based on your scale and requirements.

Want the complete reference?

Read official docs

Test what you just learned

Quiz questions written from this article

Take the quiz →
DigitalOceanSponsor

Simple, affordable cloud — VMs, Kubernetes, and managed databases in minutes. Trusted by 600,000+ developers. Spin up a Droplet in 60 seconds.

Try DigitalOcean →

Get the daily digest

One email. 5 articles. Every morning.

No spam. Unsubscribe anytime.