使用模板扩展管道。
extends:
template: string # The template referenced by the pipeline to extend.
parameters: # Parameters used in the extend.
引用此定义的定义:管道
属性
template 字符串。
要扩展的管道引用的模板。
parameters 模板参数。
扩展中使用的参数。
Usage
该 extends 关键词允许流水线以另一个流水线(或模板)为基础。 这适用于:
- 重复使用流水线结构:创建一个多个项目可以扩展的基础流水线
- 执行标准:定义所有管道必须包含的阶段、作业或步骤
- 减少重复:在管道间共享共同配置
- 模板继承:从带有参数的模板构建管道
流水线可以扩展单个模板。 模板文件应是一个完整且有效的管道结构。 当你使用 extends时,你的流水线 YAML 会变成一个模板,引用基础模板并可选地覆盖参数。
扩展与包含
-
extends创建继承模板的流水线。 在管道的根层面使用。 -
include: 直接导入模板内容。 在管道中的特定节点使用。
例子
基础扩展示例
最简单的使用 extends 方式是让流水线从基础模板继承阶段和作业。
# File: base-pipeline.yml
# This is the base template that other pipelines extend
trigger:
- main
pool:
vmImage: 'ubuntu-latest'
stages:
- stage: Build
jobs:
- job: BuildJob
steps:
- script: echo Building...
displayName: 'Build step'
# File: azure-pipelines.yml
# This pipeline extends the base template
extends:
template: base-pipeline.yml
在本例中,流azure-pipelines.yml水线继承了触发器、池、阶段和作业。base-pipeline.yml
带参数的扩展
你可以在扩展模板时传递参数:
# File: base-pipeline.yml
parameters:
- name: buildConfiguration
type: string
default: 'Debug'
trigger:
- main
pool:
vmImage: 'ubuntu-latest'
stages:
- stage: Build
jobs:
- job: BuildJob
steps:
- script: echo Building with configuration ${{ parameters.buildConfiguration }}
displayName: 'Build step'
# File: azure-pipelines.yml
extends:
template: base-pipeline.yml
parameters:
buildConfiguration: 'Release'
多阶段流水线与延伸
你可以用 extends 它继承模板中的多个阶段。 这对希望在项目间建立一致流水线结构的团队非常有用。
# File: multi-stage-template.yml
# This template defines a complete three-stage pipeline
trigger:
- main
pool:
vmImage: 'ubuntu-latest'
stages:
- stage: Build
displayName: 'Build Stage'
jobs:
- job: BuildJob
displayName: 'Build Job'
steps:
- script: npm install
displayName: 'Install dependencies'
- script: npm run build
displayName: 'Build application'
- stage: Test
displayName: 'Test Stage'
dependsOn: Build
jobs:
- job: TestJob
displayName: 'Run Tests'
steps:
- script: npm run test
displayName: 'Run unit tests'
- script: npm run lint
displayName: 'Run linter'
- stage: Deploy
displayName: 'Deploy Stage'
dependsOn: Test
jobs:
- job: DeployJob
displayName: 'Deploy Job'
steps:
- script: npm run deploy
displayName: 'Deploy to production'
# File: azure-pipelines.yml
extends:
template: multi-stage-template.yml
在这个例子中,流水线会自动继承模板中的三个阶段(构建、测试和部署)及其依赖关系和作业定义。
带有扩展的类型安全参数
在管道运行之前,模板及其参数将转换为常量。
模板参数为输入参数提供类型安全性。
在此示例中,模板start.yml定义参数,然后用于buildSteps该参数azure-pipelines.yml。 如果使用脚本步骤传递 buildStep,则会被拒绝,管道生成会失败。
# File: start.yml
parameters:
- name: buildSteps # the name of the parameter is buildSteps
type: stepList # data type is StepList
default: [] # default value of buildSteps
stages:
- stage: secure_buildstage
pool:
vmImage: windows-latest
jobs:
- job: secure_buildjob
steps:
- script: echo This happens before code
displayName: 'Base: Pre-build'
- script: echo Building
displayName: 'Base: Build'
- ${{ each step in parameters.buildSteps }}:
- ${{ each pair in step }}:
${{ if ne(pair.value, 'CmdLine@2') }}:
${{ pair.key }}: ${{ pair.value }}
${{ if eq(pair.value, 'CmdLine@2') }}:
# Step is rejected by raising a YAML syntax error: Unexpected value 'CmdLine@2'
'${{ pair.value }}': error
- script: echo This happens after code
displayName: 'Base: Signing'
# File: azure-pipelines.yml
trigger:
- main
extends:
template: start.yml
parameters:
buildSteps:
- bash: echo Test #Passes
displayName: succeed
- bash: echo "Test"
displayName: succeed
# Step is rejected by raising a YAML syntax error: Unexpected value 'CmdLine@2'
- task: CmdLine@2
inputs:
script: echo "Script Test"
# Step is rejected by raising a YAML syntax error: Unexpected value 'CmdLine@2'
- script: echo "Script Test"