Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure devops: pass variable group as parameter to Template

I am using Azure devops yml pipeline at code base.

I have created Variable Group at pipeline (pipeline > Library > variable group > called 'MY_VG') In my pipeline (yml) file i want to send this variable group MY_VG to template my_template.yml as parameter.

But this parameter MY_VG is not being expanded when i use it under 'Variable' (though while printing it gives me value)

How to access the value of this MY_VG in the template here group: ${{parameters.variable_group}} shown below?

(I am calling a template file my_template_iterator.yml which iterates the environments and call the 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'))

## Iterator: my_template_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}}
like image 803
Sanjesh M Avatar asked Sep 08 '26 19:09

Sanjesh M


1 Answers

I tested with above your yaml files. It seems fine. However i found a small mistake that you missed a - when you define a variable group in my_template.yml of yours. Maybe that is the reason the variable group didnot expand for you.

variables:
# a variable group
- group: myvariablegroup

I modified your my_template.yml file a little bit. And i got it working. See below:

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)"
like image 189
Levi Lu-MSFT Avatar answered Sep 11 '26 22:09

Levi Lu-MSFT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!