All guides

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.

A whiteboard with handwritten release notes and sticky notes

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 template keyword is available in stages, jobs, steps and variables.
  • 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.

YAML

# 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 }}

YAML

# 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:

YAML

# 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, name is the project and the repository. For type: github, name is the full name of the GitHub repository, including the user or organization, and a GitHub service connection is required.
  • If no ref is specified, the pipeline uses refs/heads/main. A ref is 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:

YAML

# 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.

YAML

# File: insert-vars.yml
variables:
  favoriteVeggie: 'brussels sprouts'

YAML

# 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.

Book a call

More guides