CI/CDAzure
Azure Pipelines templates: reuse stages across projects
An Azure Pipelines template defines reusable content, logic and parameters in a YAML file. A pipeline can insert it, extend it, or load it from another repository at a pinned ref.

Includes and extends templates
Azure Pipelines has two main types of template.
- Includes templates insert reusable content into a pipeline, like an include directive in many programming languages. The
templatekeyword is available instages,jobs,stepsandvariables. - Extends templates define the outer structure of the pipeline and offer specific points for targeted customizations. A pipeline can extend a single template.
Before a pipeline runs, templates and their parameters are transformed into constants.
A stage template with typed parameters
This is the stage template from Microsoft's documentation, followed by the pipeline that uses it.
# stage-template.yml
parameters:
- name: stageName
type: string
- name: jobName
type: string
- name: vmImage
type: string
- name: scriptPath
type: string
stages:
- stage: ${{ parameters.stageName }}
jobs:
- job: ${{ parameters.jobName }}
pool:
vmImage: ${{ parameters.vmImage }}
steps:
- script: ./${{ parameters.scriptPath }}# azure-pipelines.yml
trigger:
- main
stages:
- template: stage-template.yml
parameters:
stageName: 'BuildStage'
jobName: 'BuildJob'
scriptPath: 'build-script.sh' # replace with script in your repository
vmImage: 'ubuntu-latest'Step and job templates follow the same shape. The documented step template starts with steps: and is referenced under steps, and the job template starts with jobs:. When a job template is used several times, remove the job name from the template to avoid a conflict. A template path is relative to the file that does the including, or absolute within the repository when it starts with /.
What parameter types check
Parameters must include a name and a data type. The types are string, number, boolean, object (any YAML structure), step, stepList, job, jobList, deployment, deploymentList, stage and stageList. stringList is not available in templates. A values list restricts a parameter to the choices it enumerates.
The type is checked. In the documentation's example, a boolean parameter set to a string like apples makes the build fail. Declare the type every time: a scalar parameter with no specified type is treated as a string, and a non-empty string is cast to true in a Boolean context. A comparison with true then succeeds even when the value is the word false. Only literals can be used for parameter default values.
Templates from another repository
Declare the repository under resources.repositories, then refer to the template with @ and the alias. This pipeline is from Microsoft's security guidance:
# azure-pipelines.yml
resources:
repositories:
- repository: templates
type: git
name: MyProject/MyTemplates
ref: refs/tags/v1
extends:
template: template.yml@templates
parameters:
usersteps:
- script: echo This is my first step
- script: echo This is my second step- For
type: git, which is Azure Repos,nameis the project and the repository. Fortype: github,nameis the full name of the GitHub repository, including the user or organization, and a GitHub service connection is required. - If no
refis specified, the pipeline usesrefs/heads/main. Arefis a branch (refs/heads/<name>) or a tag (refs/tags/<name>), and the documentation also shows a commit SHA. Microsoft's tip is to anchor an extends template to a branch or tag, so that breaking changes do not affect existing pipelines. - Repositories are resolved only once, when the pipeline starts up, and only the template files are used. Scripts in the template repository cannot be used in the pipeline.
Extends templates and the required template check
The pipeline above extends this template, from the same page:
# template.yml
parameters:
- name: usersteps
type: stepList
default: []
steps:
- ${{ each step in parameters.usersteps }}:
- ${{ step }}The template receives the pipeline's steps as a stepList and iterates over them. A template can also rewrite user steps and allow only approved tasks to run.
Microsoft states that templates work as a security mechanism only as far as they are enforced. To enforce one, go to the service connection that you want to restrict, open Approvals and Checks, select Required template, and enter the repository type, the repository, the ref and the path to the required template. With the check in place, a pipeline fails if it does not extend from the referenced template. One service connection can have multiple required templates.
Variable templates
Variables can be defined in one YAML file and included in another.
# File: insert-vars.yml
variables:
favoriteVeggie: 'brussels sprouts'# File: azure-pipelines.yml
variables:
- template: insert-vars.yml # Template reference
steps:
- script: echo My favorite vegetable is ${{ variables.favoriteVeggie }}.A template that is included to supply variables can only define variables. It can take parameters, and the documentation shows one included for the whole pipeline and again for a single stage.
Limits to check before you split files
- The limit on included YAML files is 100, counting direct and indirect includes.
- No more than 100 levels of template nesting are allowed.
- Parsing the YAML may consume no more than 20 megabytes of memory.
- Template files need to exist on the filesystem at the start of a pipeline run. A template cannot be referenced in an artifact.
Sources
Want a second pair of eyes on yours? We do this work for a living.
More guides

Jenkins shared libraries: one pipeline for many jobs
CI/CD · 4 min read
Lay out a shared library, write one custom step, name its version and know what trusted means.

GitHub environments: approvals, secrets and deployment rules
CI/CD · 4 min read
Required reviewers, wait timers, branch rules and secrets that a job gets only after approval.

Concurrency groups: stop two deploys from running at once
CI/CD · 4 min read
One run per group at a time: cancel outdated pull request runs and never cancel a deploy.