Skip to content

Commit c17b57e

Browse files
Merge pull request #8244 from MicrosoftDocs/users/jukullam/peerreview
Update YAML templates documentation for clarity and accuracy after peer review
2 parents bda0aa1 + dfe0622 commit c17b57e

File tree

1 file changed

+44
-35
lines changed

1 file changed

+44
-35
lines changed

docs/pipelines/process/templates.md

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
title: How to use YAML templates for reusable and secure pipelines
33
description: Learn how to create reusable YAML pipeline templates to streamline and secure your CI/CD processes.
44
ms.assetid: 6f26464b-1ab8-4e5b-aad8-3f593da556cf
5-
ms.topic: conceptual
6-
ms.date: 02/26/2025
5+
ms.topic: how-to
6+
ms.date: 08/21/2025
77
monikerRange: "<=azure-devops"
88
zone_pivot_groups: template-type
99
ai-usage: ai-assisted
@@ -17,18 +17,27 @@ ai-usage: ai-assisted
1717

1818
Templates let you define reusable content, logic, and parameters in YAML pipelines. To work with templates effectively, you need to have a basic understanding of [Azure Pipelines key concepts](../get-started/key-pipelines-concepts.md) such as stages, steps, and jobs.
1919

20-
Templates can help you speed up development. For example, you can have a series of the same tasks in a template and then include the template multiple times in different stages of your YAML pipeline.
20+
There are two main types of templates:
21+
22+
- **Includes templates:** Insert reusable content into a pipeline. If a template is used to include content, it functions like an include directive in many programming languages. Content from a template is inserted into the pipeline or template that includes it.
23+
- **Extends templates:** Control and define a schema for what is allowed in a pipeline. When an extends template is used, it defines logic and structure that a pipeline must follow. This is useful for enforcing security, compliance, or organizational standards.
24+
25+
Templates can help you speed up development. For example, you can have a series of the same tasks in a template and then include the template multiple times in different stages of your YAML pipeline.
2126

2227
Templates can also help you secure your pipeline. When a template controls what is allowed in a pipeline, the template defines logic that another file must follow. For example, you might want to restrict what tasks are allowed to run. For that scenario, you can use template to prevent someone from successfully running a task that violates your organization's security policies.
23-
24-
There are two types of templates: includes and extends.
2528

26-
- **Includes templates** let you insert reusable content with a template. If a template is used to include content, it functions like an include directive in many programming languages. Content from template is inserted into the pipeline or template that includes it.
27-
- **Extends templates** let you control what is allowed in a pipeline. When an extends template controls what is allowed in a pipeline, the template defines logic that a pipeline must follow. For example, an extends template can be used in the context of extending a pipeline to perform stages or jobs.
29+
To take full advantage of templates, you should also use [template expressions](template-expressions.md) and [template parameters](template-parameters.md).
30+
31+
## Prerequisites
32+
33+
| **Product** | **Requirements** |
34+
|---|---|
35+
| **Azure DevOps** | - An [Azure DevOps project](../../organizations/projects/create-project.md).<br> - An ability to run pipelines on Microsoft-hosted agents. You can either purchase a [parallel job](../licensing/concurrent-jobs.md) or you can request a free tier. <br> - Basic knowledge of YAML and Azure Pipelines. For more information, see [Create your first pipeline](../create-first-pipeline.md). <br> - **Permissions:**<br> &nbsp;&nbsp;&nbsp;&nbsp; - To create a pipeline: you must be in the **Contributors** group and the group needs to have *Create build pipeline* permission set to Allow. Members of the [Project Administrators group](../../organizations/security/permissions.md) can manage pipelines. <br> &nbsp;&nbsp;&nbsp;&nbsp;- To create service connections: You must have the *Administrator* or *Creator* role for [service connections](../library/add-resource-protection.md).
36+
| **GitHub** | - A [GitHub](https://github.com) account. <br> - A [GitHub service connection](../library/service-endpoints.md) to authorize Azure Pipelines.|
37+
| **Azure** | An [Azure subscription](https://azure.microsoft.com/free/). |
2838

29-
To take full advantage of templates, you should also use [template expressions](template-expressions.md) and [template parameters](template-parameters.md).
3039

31-
### Imposed limits on template updates
40+
## Imposed limits on template updates
3241

3342
Templates and template expressions can cause explosive growth to the size and complexity of a pipeline.
3443
To help prevent runaway growth, Azure Pipelines imposes the following limits:
@@ -45,12 +54,12 @@ To help prevent runaway growth, Azure Pipelines imposes the following limits:
4554

4655
::: moniker range=">=azure-devops-2020"
4756

48-
To increase security, you can enforce that a pipeline extends from a particular template. The file `start.yml` defines the parameter `buildSteps`, which is then used in the pipeline `azure-pipelines.yml`.
49-
In `start.yml`, if a `buildStep` gets passed with a script step, then it's rejected and the pipeline build fails.
57+
To increase security, you can enforce that a pipeline extends from a particular template. The file `start-extends-template.yml` defines the parameter `buildSteps`, which is then used in the pipeline `azure-pipelines.yml`.
58+
In `start-extends-template.yml`, if a `buildStep` gets passed with a script step, then it's rejected and the pipeline build fails.
5059
When extending from a template, you can increase security by adding a [required template approval](../security/templates.md#set-required-templates).
5160

5261
```yaml
53-
# File: start.yml
62+
# File: start-extends-template.yml
5463
parameters:
5564
- name: buildSteps # the name of the parameter is buildSteps
5665
type: stepList # data type is StepList
@@ -85,7 +94,7 @@ trigger:
8594
- main
8695

8796
extends:
88-
template: start.yml
97+
template: start-extends-template.yml
8998
parameters:
9099
buildSteps:
91100
- bash: echo Test #Passes
@@ -110,11 +119,11 @@ trigger:
110119
- none
111120
112121
extends:
113-
template: resource-template.yml
122+
template: resource-extends-template.yml
114123
```
115124

116125
```yaml
117-
# File: resource-template.yml
126+
# File: resource-extends-template.yml
118127
resources:
119128
pipelines:
120129
- pipeline: my-pipeline
@@ -134,13 +143,13 @@ steps:
134143

135144
::: moniker range=">=azure-devops-2020"
136145

137-
You can copy content from one YAML and reuse it in a different YAML. Copying content from one YAML to another saves you from having to manually include the same logic in multiple places. The `include-npm-steps.yml` file template contains steps that are reused in `azure-pipelines.yml`.
146+
You can insert content from one YAML and reuse it in a different YAML. Inserting content from one YAML to another saves you from having to manually include the same logic in multiple places. The `insert-npm-steps.yml` file template contains steps that are reused in `azure-pipelines.yml`.
138147

139148
> [!NOTE]
140149
> Template files need to exist on your filesystem at the start of a pipeline run. You can't reference templates in an artifact.
141150

142151
```yaml
143-
# File: templates/include-npm-steps.yml
152+
# File: templates/insert-npm-steps.yml
144153
145154
steps:
146155
- script: npm install
@@ -156,12 +165,12 @@ jobs:
156165
pool:
157166
vmImage: 'ubuntu-latest'
158167
steps:
159-
- template: templates/include-npm-steps.yml # Template reference
168+
- template: templates/insert-npm-steps.yml # Template reference
160169
- job: Windows
161170
pool:
162171
vmImage: 'windows-latest'
163172
steps:
164-
- template: templates/include-npm-steps.yml # Template reference
173+
- template: templates/insert-npm-steps.yml # Template reference
165174
```
166175

167176
#### Reuse steps across multiple jobs
@@ -170,7 +179,7 @@ You can insert a template to reuse one or more steps across several jobs.
170179
In addition to the steps from the template, each job can define more steps.
171180

172181
```yaml
173-
# File: templates/npm-steps.yml
182+
# File: templates/insert-npm-steps.yml
174183
steps:
175184
- script: npm install
176185
- script: npm test
@@ -184,20 +193,20 @@ jobs:
184193
pool:
185194
vmImage: 'ubuntu-latest'
186195
steps:
187-
- template: templates/npm-steps.yml # Template reference
196+
- template: templates/insert-npm-steps.yml # Template reference
188197
189198
- job: macOS
190199
pool:
191200
vmImage: 'macOS-latest'
192201
steps:
193-
- template: templates/npm-steps.yml # Template reference
202+
- template: templates/insert-npm-steps.yml # Template reference
194203
195204
- job: Windows
196205
pool:
197206
vmImage: 'windows-latest'
198207
steps:
199208
- script: echo This script runs before the template's steps, only on Windows.
200-
- template: templates/npm-steps.yml # Template reference
209+
- template: templates/insert-npm-steps.yml # Template reference
201210
- script: echo This step runs after the template's steps.
202211
```
203212

@@ -206,7 +215,7 @@ jobs:
206215
Much like steps, jobs can be reused with templates.
207216

208217
```yaml
209-
# File: templates/jobs.yml
218+
# File: templates/insert-jobs.yml
210219
jobs:
211220
- job: Ubuntu
212221
pool:
@@ -225,13 +234,13 @@ jobs:
225234
# File: azure-pipelines.yml
226235
227236
jobs:
228-
- template: templates/jobs.yml # Template reference
237+
- template: templates/insert-jobs.yml # Template reference
229238
```
230239

231240
When working with multiple jobs, remember to remove the name of the job in the template file, so as to avoid conflict
232241

233242
```yaml
234-
# File: templates/jobs.yml
243+
# File: templates/insert-multiple-jobs.yml
235244
jobs:
236245
- job:
237246
pool:
@@ -250,17 +259,17 @@ jobs:
250259
# File: azure-pipelines.yml
251260
252261
jobs:
253-
- template: templates/jobs.yml # Template reference
254-
- template: templates/jobs.yml # Template reference
255-
- template: templates/jobs.yml # Template reference
262+
- template: templates/insert-multiple-job.yml # Template reference
263+
- template: templates/insert-multiple-jobs.yml # Template reference
264+
- template: templates/insert-multiple-jobs.yml # Template reference
256265
```
257266

258267
#### Reuse stages across multiple templates
259268

260269
Stages can also be reused with templates.
261270

262271
```yaml
263-
# File: templates/stages1.yml
272+
# File: templates/insert-stage1.yml
264273
stages:
265274
- stage: Angular
266275
jobs:
@@ -270,7 +279,7 @@ stages:
270279
```
271280

272281
```yaml
273-
# File: templates/stages2.yml
282+
# File: templates/insert-stage2.yml
274283
stages:
275284
- stage: Build
276285
jobs:
@@ -295,8 +304,8 @@ stages:
295304
- task: Npm@1
296305
inputs:
297306
command: 'install'
298-
- template: templates/stages1.yml # Template reference
299-
- template: templates/stages2.yml # Template reference
307+
- template: templates/insert-stage1.yml # Template reference
308+
- template: templates/insert-stage2.yml # Template reference
300309
```
301310

302311
#### Add parameters to job, stage, and step templates
@@ -466,7 +475,7 @@ Variables can be defined in one YAML and included in another template. This coul
466475
In this example, the variable `favoriteVeggie` is included in `azure-pipelines.yml`.
467476

468477
```yaml
469-
# File: vars.yml
478+
# File: insert-vars.yml
470479
variables:
471480
favoriteVeggie: 'brussels sprouts'
472481
```
@@ -475,7 +484,7 @@ variables:
475484
# File: azure-pipelines.yml
476485
477486
variables:
478-
- template: vars.yml # Template reference
487+
- template: insert-vars.yml # Template reference
479488
480489
steps:
481490
- script: echo My favorite vegetable is ${{ variables.favoriteVeggie }}.

0 commit comments

Comments
 (0)