Introduction

As DevOps practices mature, CI/CD pipelines often grow in size and complexity. Multiple applications may share similar build, test, and deployment steps, leading to duplicated YAML code across repositories. Maintaining these duplicated pipelines becomes difficult, especially when updates need to be applied consistently across multiple projects.

Azure DevOps YAML Pipeline Templates solve this problem by allowing you to extract reusable pipeline logic into template files. Templates help standardize CI/CD processes, reduce duplication, improve maintainability, and promote consistency across teams.

In this article, you'll learn what Azure DevOps YAML Pipeline Templates are, how to create and use them, and the best practices for building reusable CI/CD pipelines.

What Are YAML Pipeline Templates?

A YAML pipeline template is a reusable YAML file that contains one or more pipeline components.

Templates can define reusable:

Instead of repeating the same pipeline configuration across multiple projects, you reference the template wherever it is needed.

Why Use Pipeline Templates?

Without templates, teams often copy and paste YAML code between repositories.

This approach leads to:

Pipeline templates provide several benefits:

They are especially valuable for organizations managing many repositories.

Types of Pipeline Templates

Azure DevOps supports several types of templates.

Step Templates

Reusable collections of pipeline steps.

Job Templates

Reusable build or deployment jobs.

Stage Templates

Reusable stages containing multiple jobs.

Variable Templates

Reusable sets of variables shared across pipelines.

Choosing the appropriate template type depends on the level of reuse required.

Project Structure

A typical repository might be organized as follows:

Code
Project
│
├── azure-pipelines.yml
├── templates
│   ├── build.yml
│   ├── test.yml
│   ├── deploy.yml
│   └── variables.yml
└── src

Keeping templates in a dedicated folder improves organization and discoverability.

Create a Step Template

Create a reusable build template.

YAML
steps:
- task: DotNetCoreCLI@2
  inputs:
    command: restore

- task: DotNetCoreCLI@2
  inputs:
    command: build
    arguments: --configuration Release

This template performs package restoration and builds the application.

Use the Step Template

Reference the template in your main pipeline.

YAML
trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:
- template: templates/build.yml

Azure DevOps expands the template during pipeline execution.

Create a Job Template

Job templates encapsulate an entire job.

YAML
jobs:
- job: Build
  pool:
    vmImage: ubuntu-latest

  steps:
  - script: dotnet build

This template can be reused by multiple pipelines.

Use the Job Template

Reference the job template.

YAML
jobs:
- template: templates/build.yml

The job becomes part of the pipeline during execution.

Create a Stage Template

Larger organizations often standardize deployment stages.

Example:

YAML
stages:
- stage: Deploy

  jobs:
  - job: DeployApp

    steps:
    - script: echo Deploying application...

Stage templates are useful for implementing consistent deployment processes across environments.

Pass Parameters to Templates

Templates become more flexible when they accept parameters.

YAML
parameters:
- name: buildConfiguration
  type: string
  default: Release

steps:
- script: dotnet build --configuration ${{ parameters.buildConfiguration }}

This template supports different build configurations without modification.

Use Template Parameters

Pass parameter values when referencing the template.

YAML
steps:
- template: templates/build.yml

  parameters:
    buildConfiguration: Debug

Parameters make templates adaptable while avoiding duplication.

Use Variable Templates

Store shared variables in a separate YAML file.

YAML
variables:
  buildConfiguration: Release
  vmImage: ubuntu-latest

Reference the variables.

YAML
variables:
- template: templates/variables.yml

Variable templates centralize commonly used configuration values.

Organize Shared Templates

Many organizations maintain a dedicated repository for pipeline templates.

Benefits include:

Multiple projects can reference the same template repository, reducing duplication across teams.

Validate Templates

Before sharing templates across projects:

Testing helps prevent widespread pipeline failures.

Best Practices

When working with Azure DevOps YAML Pipeline Templates:

These practices improve maintainability and reduce configuration drift.

Common Mistakes to Avoid

Avoid these common issues when designing pipeline templates:

Keeping templates modular and well documented makes them easier to maintain.

Pipeline Templates vs Copy-and-Paste Pipelines

The following comparison highlights the benefits of templates.

FeatureCopy-and-Paste PipelinesYAML Templates
ReusabilityLowHigh
MaintenanceDifficultEasy
ConsistencyLimitedExcellent
Code DuplicationHighLow
ScalabilityLimitedExcellent

Templates help organizations build scalable and maintainable CI/CD pipelines.

Conclusion

Azure DevOps YAML Pipeline Templates are a powerful way to standardize and simplify CI/CD workflows. By extracting reusable steps, jobs, stages, and variables into dedicated template files, teams can reduce duplication, improve maintainability, and ensure consistent build and deployment processes across multiple projects.

By following best practices such as using parameters, organizing templates effectively, centralizing shared variables, and versioning reusable components, you can build Azure DevOps pipelines that are easier to maintain, scale, and evolve as your applications grow.