Sanjesh M
Sanjesh M

Reputation: 457

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

Upvotes: 5

Views: 23609

Answers (2)

Levi Lu-MSFT
Levi Lu-MSFT

Reputation: 30373

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

Upvotes: 11

Krzysztof Madej
Krzysztof Madej

Reputation: 40909

Ok. I'm not sure if this doable in a way how you are trying. But I tried another approach which seems to be working.

First let's define template for simple stage:

parameters:
- name: env_name
  type: string
  default: 'dev'
- name: variable_group
  type: string
  default: 'MY_VG'
- name: pipeline_environment_name
  type: string
  default: 'DEV_ENV'

stages:
- stage: ${{ parameters.env_name }}
  jobs:
  - job: angularinstall
    steps:
    - script: echo "${{ parameters.env_name }}"
    - script: echo "${{ parameters.variable_group }}"
    - script: echo "${{ parameters.pipeline_environment_name }}"

then template for main build:

parameters:
- name: stages # the name of the parameter is stageList
  type: stageList # data type is stageList
  default: [] # default value of stageList

stages: ${{ parameters.stages }}

and then we can combine them together in main build file:

extends:
  template: template.yaml
  parameters:
    stages:  
    - template: stageTemplate.yaml
      parameters:
        env_name: 'dev'
        variable_group: 'MY_VG'
        pipeline_environment_name: 'DEV_ENV'
    - template: stageTemplate.yaml
      parameters:
        env_name: 'stage'
        variable_group: 'MY_STAGE'
        pipeline_environment_name: 'STAGE_ENV'

And for that I got:

enter image description here

Does it sth what bring you closer to your solution?

Upvotes: 0

Related Questions