Transfer variable to template

127 Views Asked by At

I am trying to transfer a variable to a template. I have tried: ${{ variables.portalPath }} and $(portalPath)

But no values are transferred. If I dont use a variable but just a string it works:

pipeline:

trigger:
- none

variables:
  - name: portalPath value:"sdfsdf"

extends:
  template: azure-pipelines-build-react-portal-template.yml
  parameters:
    portalPath: "I WOULD LIKE TO REPLACE THIS WITH VARIABLE"

Template:

parameters:
 - name: portalPath 
   type: string

steps:
  - script: echo ${{ parameters.portalPath }}
2

There are 2 best solutions below

0
On BEST ANSWER

Run-time variables are not yet available at the time the value pass to portalPath is interpreted. The template parameter would then receive a run-time variable $(portalPath).

Instead ofpassing the variable in run-time syntax but use the expression syntax ${{ variables.environment }}:

Template.yml

parameters:

- name: portalPath # don't pass run-time variables

steps:
  - script: echo ${{ parameters.portalPath }}

azure-pipelines.yml

- stage: QA

  variables: 

    PortalPath : sdfsdf

  jobs:

  - template: azure-pipelines-build-react-portal-template.yml

    parameters:

      environment: ${{ variables.environment }} # use expression syntax

If this is still not work. A workaround here is introducing simply a new variable in your in the beginning of your job (with powershell or bash):

- powershell: Write-Host "##vso[task.setvariable variable=PortalPath ]${{ parameters.portalPath }}"

From that point on you can use ${PortalPath }in your further steps.

You could also take a look at below similar question:

2
On

The example pipeline doesn't declare the variable value properly.

Not sure if this is just a typo when entering the question as the pipeline should fail upfront with your current variable declaration of:

variables:
  - name: portalPath value:"sdfsdf"

This template works for me:

variables:
  - name: portalPath
    value: "sdfsdf"

extends:
  template: my-template.yml
  parameters:
    portalPath: $(portalPath)