【问题标题】:Is there a way to parametrize/dynamically set variable group names in Azure DevOps Pipelines YAML?有没有办法在 Azure DevOps Pipelines YAML 中参数化/动态设置变量组名称?
【发布时间】:2020-05-26 07:00:30
【问题描述】:

我有一个嵌套的 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

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

---
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

但是,这似乎不起作用。无奈之下,我尝试了多种方法:

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

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

jobs:
  - job: Job1
    variables:
      - group: ${{ parameters.variablegroup }}

但是,这也不起作用。

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

根据thisthisthisthis,这似乎是不可能的。

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

【问题讨论】:

    标签: azure azure-devops azure-pipelines azure-pipelines-release-pipeline


    【解决方案1】:

    您将组名作为参数传递给模板的直觉是正确的。我能够得到这个工作:

    模板文件传递-variable-groups.yml

    parameters:
    - name: deploymentVariableLibraries
      type: object
      default:
        dev: ''
        qa: ''
        prod: ''
    
    jobs:
    - job: TestDev
      variables:
        - group: ${{parameters.deploymentVariableLibraries.dev}}
      steps:
        - script: echo "$(whichEnvironment)"
    - job: TestQa
      variables:
        - group: ${{parameters.deploymentVariableLibraries.qa}}
      steps:
        - script: echo "$(whichEnvironment)"
    - job: TestProd
      variables:
        - group: ${{parameters.deploymentVariableLibraries.prod}}
      steps:
        - script: echo "$(whichEnvironment)"
    

    管道

    trigger: none
    
    resources:
      repositories:
        - repository: templates
          type: git
          name: c4ePipelineExamples-Templates
    
    jobs:
    - template: passing-variable-groups.yml@templates
      parameters:
        deploymentVariableLibraries:
          dev: 'test-dev'
          qa: 'test-qa'
          prod: 'test-prod'
    

    我每个作业的输出分别是DEV、QA、PROD(每个测试变量库中whichEnvironment的值)。

    【讨论】:

      【解决方案2】:

      您是否尝试过注入变量mapping as a parameter

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

      # sometemplate.yml
      parameters:
        variables: {}
      
      jobs:
      - job: build
        variables: ${{ parameters.variables }}
      
      # 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"
      

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

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

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

      【讨论】:

      • 同意在可能的情况下使用编译时语法,以及在此时无法知道值的运行时 - 这样做的好处之一是允许您下载扩展的YAML 并具有尽可能多的值,这简化了验证和调试。
      【解决方案3】:

      以下对我有用,无需任何模板

      variables:
      - group: variables-${{variables['Build.SourceBranchName']}}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-30
        • 1970-01-01
        • 1970-01-01
        • 2021-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多