本文介绍了Azure DEVOPS:将变量组作为参数传递给模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码库使用Azure devopYML管道。

我已在管道创建了变量组(称为‘My_VG’的管道;库;变量组)在我的管道(YML)文件中,我希望将此变量组my_vg作为参数发送到模板my_template.yml。

但是,当我在‘Variable’下使用此参数my_vg时,它不会被展开(尽管在打印时它会给我赋值)

如何在模板HERE组中获取此MY_VG的值:${参数.变量_组}},如下图所示?

(我将模板文件称为my_template.yml,它迭代环境并调用my_template.yml)Azure-Pipelines.yml

resources:
  repositories:
    - repository: templates
      type: git
      name: MY_PROJECT/GIT_REPO_FOR_TEMPLATE
stages:
  - stage: "CheckOut"
    displayName: Checkout
    jobs:
      - job: Checkout
        displayName: Checkout Application
        pool:
          name: $(my_pool_name)
        workspace:
          clean: all 
        steps:
          - checkout: self

  - template: folder_name/my_template_iterator.yml@templates
    parameters:
      agent_pool_name: $(my_pool)
      db_resource_path: $(System.DefaultWorkingDirectory)/src/main/resources/db
      envs:
        - env:
          env_name: 'dev'
          variable_group: MY_VG
          pipeline_environment_name: DEV_ENV
          is_master: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))

##迭代器:my_模板_iterator.yml

parameters:
  agent_pool_name: ''
  db_resource_path: ''
  envs: {}

stages:
  - ${{ each env in parameters.envs }}:
    - template: my_template.yml
      parameters:
        agent_pool_name: ${{ parameters.agent_pool_name }}
        db_resource_path: ${{ parameters.db_resource_path }}
        env_name: ${{ env.env_name }}
        variable_group: ${{ env.variable_group }}
        pipeline_environment_name: ${{ env.pipeline_environment_name }}
        is_master: ${{ env.is_master }}

##my_template.yml

parameters:
- name: 'variable_group'    
  type: string    
  default: 'default_variable_group'
    stages:
      - stage:
        displayName: Read Parameters
        jobs:
          - job: READ
            displayName: Reading Parameters
            steps:
              - script: |
                  echo variable_group: ${{parameters.variable_group}}
    
      - stage:
        displayName: Deployment
        variables:
           group: ${{parameters.variable_group}}
        condition: ${{parameters.is_master}}

yaml

我测试了上面的推荐答案文件。看起来很好。然而,我发现一个小错误,当您在您的my_template.yml中定义一个变量组时,您遗漏了一个-。也许这就是变量组没有为您展开的原因。

variables:
# a variable group
- group: myvariablegroup

我稍微修改了您的my_template.yml文件。我把它修好了。见下图:

parameters:
- name: 'variable_group'    
  type: string    
  default: 'default_variable_group'
- name: agent_pool_name
  default: ""
- name: env_name
  default: ""
- name: db_resource_path
  default: ""       
- name: pipeline_environment_name
  default: ""        
- name: is_master
  default: ""
  
stages:
  - stage:
    displayName: ${{parameters.env_name}}
    
    ## i changed here add '-' to group
    variables:
    - group: ${{parameters.variable_group}}

    jobs:
    - job: READ
      displayName: Reading Parameters
      steps:
      - script: |
          echo variable_group: ${{parameters.variable_group}}
      - powershell: echo "$(variableName-in-variablegroup)"

这篇关于Azure DEVOPS:将变量组作为参数传递给模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 18:19