How to pass variables to the template parameter in azure yaml?

Viewed 1931

I have template

dockerbuild.yml

steps:
  - task: Bash@3
    displayName: Build an image to container registry
    inputs:
      script : echo $(PATH)

then a variable file

var.build.yml
- name: PATH
  value: 'docker/path'
- name: PATH1
  value: 'docker/oldpath'
- name: PATH2
  value: 'docker/newpath'

azurepipeline1.yml

resources:
  repositories:
  - repository: templates
    type: git
    name: components/pipeline_templates
trigger:
- none

pool:
  name: PoolA

variables:
- template: variabletemplates/var.build.yml@templates

jobs:
  steps:
      - template: CI-CD/dockerbuild.yml@templates  # Template reference  
        parameters:
          PATH: ${{ variables.PATH }}
      - template: CI-CD/dockerbuild.yml@templates  # Template reference  
        parameters:
          PATH: ${{ variables.PATH1 }}
      - template: CI-CD/dockerbuild.yml@templates  # Template reference  
        parameters:
          PATH: ${{ variables.PATH2 }}

The above code works

But if I change the parameter to

    parameters:
      PATH: ${{ variables.PATH1 }}
       OR
    parameters:
      PATH: ${{ variables.PATH2 }}

In the output it shows

 Output:
   docker/path
    $(PATH1)
    $(PATH2)

I need to reuse the template with many times within the pipeline with different paths. Please help in resolving the issue

1 Answers

Your template needs a parameters block.

parameters:
  - name: PATH
    type: string

steps:
  - task: Bash@3
    displayName: Build an image to container registry
    inputs:
      script : echo ${{ parameters.PATH }}
Related