使用同一 Azure DevOps 项目中不同存储库中的变量引用模板

问题描述 投票:0回答:1

我已经构建了一个使用项目中的几个存储库的管道。 所有这些都在资源块中声明,并通过标签引用:

ref: refs/tags/$(variableName)

这正是我想要实现的目标。

我面临的问题与单个存储库有关,我想从该存储库将一堆模板加载到我的主管道中。因此,每当我调用模板时,我都会使用其中的存储库名称:

- template: templateName.yml@myRepoForTemplates

当事情看起来如上所述时,我收到以下错误:

/azure-pipelines.yml: Could not get the latest source version for repository myRepoForTemplates hosted on Azure Repos using ref refs/tags/$(variableName)
.

当我更改将变量引用为“模板参数”的方式时,如下所示:

ref: refs/tags/${{ variables.variableName }}
,错误也变了:

/azure-pipelines.yml: Could not get the latest source version for repository myRepoForTemplates hosted on Azure Repos using ref refs/tags/
.

错误消息的变化迫使我认为当使用存储库进行模板调用时,该变量被视为文字。有趣的是,如果我不在存储库声明中使用变量并直接在那里提供标签,一切都会正常工作。

这是 DevOps 错误吗?有什么办法可以克服吗?

我将非常感谢您的帮助。 干杯

编辑:@Bright Ran-MSFT,您可以在下面找到 yaml 代码:
yml-config/variables.yml

variables:
  serviceConnectionName: '#myConn#'
  subscription-id: '#mySubId'
  destroyPlan: 'false'
  cicd-templates-version: '0.0.1'
  tf-modules-version: '0.0.1'
  tf-templates-version: '0.0.1'
  tf-bin-version: '0.0.1'

azure-pipelines.yml

trigger:
  - none

variables:
- template: yml-config/variables.yml

resources:
  repositories:
  - repository: tf-templates
    type: git
    name: myProject/tf-templates
    ref: refs/tags/$(tf-templates-version)

  - repository: tf-modules
    type: git
    name: myProject/tf-modules
    ref: refs/tags/$(tf-modules-version)

  - repository: cicd-templates
    type: git
    name: myProject/cicd-templates
    ref: refs/tags/$(cicd-templates-version)

stages:
- template: yaml/tf-backend-setup.yml@cicd-templates
  parameters:
    serviceConnectionName: $(serviceConnectionName)
    subId: $(subscription-id)

- ${{ if eq(variables.destroyPlan, 'false') }}:
  - template: yaml/tf-execution.yml@cicd-templates
    parameters:
      serviceConnectionName: $(serviceConnectionName)
      subId: $(subscription-id)
      tfBinVersion: $(tf-bin-version)
      env: 'base'
      destroyPlan: $(destroyPlan)

- template: yaml/tf-execution.yml@cicd-templates
  parameters:
    serviceConnectionName: $(serviceConnectionName)
    subId: $(subscription-id)
    tfBinVersion: $(tf-bin-version)
    env: 'prod'
    destroyPlan: $(destroyPlan)

- template: yaml/tf-execution.yml@cicd-templates
  parameters:
    serviceConnectionName: $(serviceConnectionName)
    subId: $(subscription-id)
    tfBinVersion: $(tf-bin-version)
    env: 'dev'
    destroyPlan: $(destroyPlan)

- ${{ if eq(variables.destroyPlan, 'true') }}:
  - template: yaml/tf-execution.yml@cicd-templates
    parameters:
      serviceConnectionName: $(serviceConnectionName)
      subId: $(subscription-id)
      tfBinVersion: $(tf-bin-version)
      env: 'base'
      destroyPlan: $(destroyPlan)

包含 TF 代码的两个存储库工作正常,并根据各自变量的值进行检查。

只要我不使用

path@repo
语法(即
- template: yaml/tf-execution.yml@cicd-templates
)调用模板,第三个存储库也可以正常工作。
如果我引入
path@repo
调用,我会收到上述错误,几乎就像 ref 值被不同地对待一样。

预先感谢您的帮助!

azure azure-devops azure-pipelines azure-repos
1个回答
0
投票

存储库资源在编译时扩展,只有静态变量和参数可以在编译时评估。

所以,对于你的情况:

  1. 如果你想通过管道变量设置

    ref
    键的值,你需要设置一个静态变量,如下所示。通过这种方式,管道将始终引用静态变量指定的标签,直到您将其值更新为 YAML 文件中的新标签。

    # Define the static variable and explicitly set its value.
    variables:
      tagName: v1.2
    
    resources:
        repositories:
        - repository: myRepoForTemplates
          type: git
          name: string
          ref: refs/tags/$(tagName)
    
  2. 如果您想在触发管道运行时动态设置

    tagName
    的值,您可以将其定义为如下参数。这样,当您手动触发管道时,您可以指定参数的值。否则,管道将使用默认值。

    # Define the parameter with default value.
    parameters:
    - name: tagName
      type: string
      default: v1.2
    
    resources:
        repositories:
        - repository: myRepoForTemplates
          type: git
          name: string
          ref: refs/tags/${{ parameters.tagName }}
    


© www.soinside.com 2019 - 2024. All rights reserved.