Azure 管道 YAML : Unexpected value 'variables'

标签 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:  
    ...

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

/stage-template.yml (Line: 7, Col: 1): Unexpected value 'variables'

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

最佳答案

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

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

包括舞台模板

# pipeline-using-stage-template.yml

stages:
- stage: stage1
[...]
# stage template reference, no 'variables' element allowed in stage-template.yml
- template: stage-template.yml 

包括变量模板

# pipeline-using-var-template.yml

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

steps:
- script: echo A step.

If you are using a template to include variables in a pipeline, the included template can only be used to define variables.

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

关于Azure 管道 YAML : Unexpected value 'variables' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60912733/

相关文章:

Azure API管理: Discriminate operations by both path and query parameters

powershell - 如何使用 DSC 在 Azure VM 上安装存储在 Azure 文件共享中的 exe 文件?

azure - 如何跟踪 Azure DevOps 上触发管道的原因?

Azure DevOps - Maven Pipeline 发布 Artifact

azure - 如何向 Terraform 中的 Azure Devops 项目授予 "Storage Blob Data Contributor"权限?

asp.net-mvc - 当将位图流上传到Azure存储时,它存储零/空图像

Azure - 托管磁盘,如何创建快照

azure - YAML 模板中出现意外值 'resources'

github - 使用 Azure DevOps 构建公共(public) GitHub 存储库

azure-pipelines - 在 ADF V2 中 - 如何将日期 ("yyyyMMdd")) 动态附加到 S3 数据集的文件名