有没有办法在 Azure DevOps Pipelines YAML 中参数化/动态设置变量组名称?

sun*_*ots 6 azure azure-devops azure-pipelines azure-pipelines-release-pipeline

我有一个嵌套的 Azure DevOps YAML 管道:

---
name: Some Release Pipeline

trigger: none

variables:
  - group: "DEV VARIABLE GROUP" # This is the environment variable library

stages:
  - stage: Stage1
    displayName: "Stage 1"
    dependsOn: []
    jobs:
      - template: /pipelines/pipeline_templates/sometemplate.yml
Run Code Online (Sandbox Code Playgroud)

我想做的是在任何环境中重用这个发布管道。理想情况下,我会设置一个管道变量“group-name”,然后将其分配给该组。像这样的东西:

---
name: Some Release Pipeline

trigger: none

variables:
 - group: "$(group-name)" # This is the environment variable library

stages:
 - stage: Stage1
    displayName: "Stage 1"
    dependsOn: []
    jobs:
      - template: /pipelines/pipeline_templates/sometemplate.yml
Run Code Online (Sandbox Code Playgroud)

但是,这似乎不起作用。绝望地,我尝试了多种方法:

  • 使用 ${{ group-name }}
  • 我尝试使用以下方法将组名作为参数传递:
    jobs:
      - template: /pipelines/pipeline_templates/sometemplate.yml
        parameters:
          variablegroup: $(group-name)
Run Code Online (Sandbox Code Playgroud)

然后在作业中的 sometemplate.yml 中设置它。例如:

jobs:
  - job: Job1
    variables:
      - group: ${{ parameters.variablegroup }}
Run Code Online (Sandbox Code Playgroud)

然而,这也不起作用。

  • 我已经尝试按照此处的建议使用插入( {{ insert }}) 。但是,要么我不知道如何正确使用插入,要么这不起作用,因为我总是遇到某种形式的验证错误。

根据这个这个这个这个似乎是不可能的。

我想知道是否有人已经找到了解决方案(除了调用 DevOps REST API 的一个非常混乱的解决方法)?

Jos*_*ust 0

您是否尝试过将变量映射作为参数注入。

参数不限于标量字符串。只要参数展开的地方需要映射,参数就可以是映射。同样,序列可以传递到需要序列的地方。

# sometemplate.yml
parameters:
  variables: {}

jobs:
- job: build
  variables: ${{ parameters.variables }}
Run Code Online (Sandbox Code Playgroud)
# somepipeline.yml
name: Some Release Pipeline

trigger: none

stages:
 - stage: Stage1
    displayName: "Stage 1"
    dependsOn: []
    jobs:
      - template: sometemplate.yml
        parameters:
          variables: 
            group: "DEV VARIABLE GROUP"
Run Code Online (Sandbox Code Playgroud)

我已经使用参数将任务集作为模板中其他任务的操作之前/之后注入,但我还没有使用它来注入映射。我认为这个特定的实现会变得棘手的领域是需要使用name/value语法。我想如果您将组映射传递到作业中,则定义变量的其他任何内容都需要使用扩展语法。

如果您同时使用变量和变量组,则必须对各个(非分组)变量使用名称/值语法:

此外,您可能需要小心何时使用运行时$()与“编译”或扩展时${{ }}语法。使用变量时,您可能希望尽可能使用扩展时间参考。