azure - 为什么我的变量没有被 Azure 管道中的嵌套模板继承?

标签 azure azure-devops azure-pipelines

我有一个 Azure 管道,它首先声明一些变量,包括从模板 YAML 文件中提取的一些变量。以下是仅关注关键部分的管道摘要:

variables:
- template: "variables/my-variables.yml"

第一阶段直接编码到管道 YAML 中,并导致发布工件:

artifact: ${{ variables.artifactName }}-provisioning

其中 artifactName 是在 variables/my-variables.yml 中定义的变量。

下一阶段使用外部文件中模板中的作业:

- stage: Next
  displayName: "Stage using template yml"
  jobs:
  - template: templates/jobs/external.yml

在该外部文件中,有几个作业本身是从外部文件引用的:

jobs:
- deployment: PublishArmTemplate
  strategy:
    runOnce:
      deploy:
        steps:
        - template: "../job.yml"
          parameters:
            ArtifactName: ${{ variables.artifactName }}

请注意,variables.artifactName 作为参数传递给作业的 ArtifactName 参数。

该作业 YAML 文件本身有一个步骤,该步骤取决于我上面发布的工件:

parameters:
- name: 'ArtifactName'
  type: string

steps:
  - download: current
    artifact: ${{ parameters.ArtifactName }}-provisioning

但是,当管道运行时,我收到此错误:

##[error]Artifact -provisioning was not found for build 322650.

换句话说,我定义的变量和管道本身的作用域尚未传递到该作业步骤(parameters.ArtifactName 为空)。

我根据 Microsoft 文档的理解是,管道范围内的变量应该可用于所有阶段和作业。使用模板时这不适用吗?或者我还需要做些什么才能使其正常工作?

最佳答案

这些变量不可用于管道的嵌套组件。根据文档(我也经历过这种行为):

Within a template expression, you have access to the parameters context that contains the values of parameters passed in. Additionally, you have access to the variables context that contains all the variables specified in the YAML file plus many of the predefined variables (noted on each variable in that topic).

“YAML 文件”实际上是指在其中进行引用的当前文件。

在嵌套模板中,您需要执行以下任一操作

  1. 在正确的范围内包含相同的变量模板,或者
  2. 将声明的管道中的变量值作为参数一直传递到每​​个嵌套模板。

所以,你可以这样做:

- stage: Next
  displayName: "Stage using template yml"
  jobs:
  - template: templates/jobs/external.yml
    parameters:
      artifactName: ${{ variables.artifactName }}

然后

parameters:
- name: artifactName
  type: string

jobs:
- deployment: PublishArmTemplate
  strategy:
    runOnce:
      deploy:
        steps:
        - template: "../job.yml"
          parameters:
            ArtifactName: ${{ parameters.artifactName }}

关于azure - 为什么我的变量没有被 Azure 管道中的嵌套模板继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66461200/

相关文章:

c# - VSTS - Visual Studio 测试任务失败并显示错误 No Test is available

azure - 测试 Azure SQL DW 并发性显示 Azure 内部未同时运行过程

使用企业库在 azure 中记录异常

kubernetes - Azure DevOps > Helm > Azure Kubernetes 部署 - 删除部署时删除 Azure 文件共享

azure-devops - Azure DevOps Rest Api 是否返回了正确数量的拉取请求?

tfs - 我们可以并行构建多个门控签到吗?

azure - 将 VS2017 Azure DevOps 管道迁移到 VS2019

Azure Application Insights - 如何根据指标之间的比较创建警报?

azure - 如何将对象从 azure 数据工厂查找传递到笔记本,以便我可以在 python 脚本中使用 object/json

azure - Visual Studio Team Services 未部署某些文件