Sanjeev
Sanjeev

Reputation: 495

How to pass variable template to extend template as a parameter in azure pipelines

i have a variables declared in yaml templates like

# File: vars.yml
variables:
- name: firstvar
  value:'first var value'
- name: secondvar
  value:'second var value'

and in pipeline i'm using extend template

trigger:
- none

extends:
  template: resource-template.yml
  paramters:
    uservariablestemplate: <i want to pass above variable yaml template here>

because my extend template using other variables so this user defined variables should be used in there

    # File: resource-template.yml
   parameters:
   - name: uservariablestemplate
     type: string
     default: none

    resources:
      repositories:
      - repository: samplerepo
        type: git
        name: myproject/myrepo
    
    variables:
    - template: centralvariables.yml@samplerepo
    - template: <i want to use variable template from user here>
    
    steps:
     - script: echo "Testing resource template"

Any help would be appreciated. or any other workaround to achieve this.

Upvotes: 4

Views: 6646

Answers (1)

Levi Lu-MSFT
Levi Lu-MSFT

Reputation: 30313

You can just pass the variable template file name to the parameters. For below example:

trigger:
- none

extends:
  template: resource-template.yml
  paramters:
    uservariablestemplate: vars.yml

Then in the template resource-template.yml, retrieve the variable template using ${{parameters.uservariablestemplate}}. See below example:

   parameters:
   - name: uservariablestemplate
     type: string
     default: none

   resources:
     repositories:
     - repository: samplerepo
       type: git
       name: myproject/myrepo
    
   variables:
   - template: centralvariables.yml@samplerepo
   - template: ${{parameters.uservariablestemplate}}
    
   steps:
    - script: echo "Testing resource template"

If the variable template file resides in another repo. You need to define the repo in the repo resources section. For below example:

   parameters:
   - name: uservariablestemplate
     type: string
     default: none

   resources:
     repositories:
     - repository: samplerepo
       type: git
       name: myproject/myrepo
     
     - repository: variableRepo
       type: git
       name: myVariableRepo
    
   variables:
   - template: centralvariables.yml@samplerepo
   - template: ${{parameters.uservariablestemplate}}@variableRepo
    
   steps:
    - script: echo "Testing resource template"

Update:

To dynamically set the repo name, you can try using runtime parameters. See below: Define a runtime parameter repo:

trigger: none

parameters:
- name: repo
  type: string
  default: none

extends:
  template: resource-template.yml
  parameters:
    uservariablestemplate: vars.yml

Then you can refer to the repo by ${{parameters.repo}} in the following yaml.

When you run your pipeline. You will be able to set the repo name:

enter image description here

Upvotes: 4

Related Questions