Reuse Azure Pipelines YAML with templates

DevOps, Hands On30 minutesFree tierWritten 23 November 2026By Cloud Hands On

In this task you'll share stages and steps across Azure Pipelines with YAML templates.

Azure Pipelines reuses YAML with templates. A step, job or stage template is a file you insert with template: and fill with parameters (typed, with defaults). An extends template goes further: the pipeline must extend a central template, which decides the stages and can refuse steps it doesn't allow, so a platform team can enforce scanning and approvals.

Templates can live in another repository, referenced with resources: repositories and pinned to a tag. Template expressions in ${{ }} are filled in when the pipeline is compiled, before it runs, so you can loop over parameters to make one stage per environment.

Example. Tollmere Logistics' 18 services each had their own 200-line pipeline. They now extend one service-pipeline.yml template, pinned to a release tag, that always runs tests, a security scan and the same three deployment stages.

Before you start

You need: An Azure DevOps organisation and project (free for up to five users); a Microsoft-hosted parallel job needs a free grant request, or use a self-hosted agent. Time: 30 minutes.

Where to look

  1. Azure DevOps
  2. Project
  3. Repos
  4. Pipelines

Steps

  1. In your Azure DevOps repository, create templates/build-steps.yml with a pythonVersion parameter (see Commands).
  2. Create templates/deploy-stage.yml that takes an env parameter and makes one stage.
  3. In azure-pipelines.yml, insert the build steps, then loop over a list of environments to create one deploy stage each.
  4. Run the pipeline (or, without a parallel job, use Run pipeline, Advanced options to view the expanded YAML) and see one stage per environment.The expanded YAML shows what the templates turned into.
  5. Move the templates to a shared repository, reference it in resources: repositories with a tag, and make the pipeline extends: it.

Commands

# templates/build-steps.yml
parameters:
- name: pythonVersion
  type: string
  default: '3.13'
steps:
- task: UsePythonVersion@0
  inputs: { versionSpec: '${{ parameters.pythonVersion }}' }
- script: pip install -r requirements-dev.txt && pytest -q

# templates/deploy-stage.yml
parameters:
- name: env
  type: string
stages:
- stage: deploy_${{ parameters.env }}
  jobs:
  - deployment: deploy
    environment: ${{ parameters.env }}
    strategy:
      runOnce:
        deploy:
          steps:
          - script: echo Deploying to ${{ parameters.env }}

# azure-pipelines.yml
parameters:
- name: environments
  type: object
  default: [test, prod]
stages:
- stage: build
  jobs:
  - job: build
    steps:
    - template: templates/build-steps.yml
- ${{ each e in parameters.environments }}:
  - template: templates/deploy-stage.yml
    parameters:
      env: ${{ e }}

Watch out for

Cost
Azure DevOps is free for five users. Microsoft-hosted minutes need a free grant (public projects) or a paid parallel job; a self-hosted agent is free.
Security
Use extends templates and required template checks on environments so no pipeline can skip the security steps.
Common mistakes
Pointing at a shared template's main branch (one change breaks every pipeline), and mixing up ${{ }} compile-time and $( ) run-time variables.

Clean up

Delete the test pipeline from Pipelines, ..., Delete.

Words to know

Template
A YAML file of steps, jobs, stages or variables inserted into a pipeline.
Parameters
Typed inputs to a template, checked when the pipeline compiles.
extends
Makes a pipeline build on a central template that controls its shape.
Template expression
${{ }} syntax filled in at compile time.

Tick off each step and track your progress in the guide.

Open this task in DevOps, Hands On

Keep going