Azure Pipelines YAML:意外值“变量”

Dra*_*ex_ 8 azure azure-devops azure-pipelines azure-pipelines-yaml

我使用 Azure Pipelines 作为 Azure DevOps 的一部分。我试图在我的模板文件中定义变量,因为我需要多次使用相同的值。

这是我的stage-template.yml

parameters:
 - name: param1
   type: string
 - name: param2
   type: string

variables:
  var1: path/${{ parameters.param2 }}/to-my-file.yml

stages:
 - stage: Deploy${{ parameters.param2 }}
   displayName: Deploy ${{ parameters.param1 }}
   jobs:  
    ...
Run Code Online (Sandbox Code Playgroud)

尝试使用此管道时,我收到一条错误消息:

/stage-template.yml(第 7 行,第 1 列):意外值“变量”

为什么这不起作用?我做错了什么?

riQ*_*iQQ 19

您不能将variables其包含在作为阶段、作业或步骤模板包含的模板中(即包含在管道中的stages,jobs或元素下方)。steps您只能variables在变量模板中使用。

遗憾的是,文档对此并不清楚。

包括舞台模板

# pipeline-using-stage-template.yml

stages:
- stage: stage1
[...]
# stage template reference, no 'variables' element allowed in stage-template.yml
- template: stage-template.yml 
Run Code Online (Sandbox Code Playgroud)

包括变量模板

# pipeline-using-var-template.yml

variables:
# variable template reference, only variables allowed inside the template
- template: variables.yml

steps:
- script: echo A step.
Run Code Online (Sandbox Code Playgroud)

如果您使用模板在管道中包含变量,则包含的模板只能用于定义变量。

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops#variable-reuse