azure-devopsazure-pipelinesdevops

Can't pass parameter to an Azure Devops template


Trying to transmit a dynamically created variable from a stage to a stage template. Doesn't work at all.

What I am doing

I run the main.yml file:

# main.yml

stages:
  - stage: Stage1
    jobs:
      - job: Job1
        steps:
          - task: powershell@2
            inputs:
              filePath: Tests/LoadData.ps1

          - script: echo $(foo)
            displayName: read foo

          - bash: echo "##vso[task.setvariable variable=bar;isOutput=true]$(foo)"
            name: step1
            displayName: create public variable

  - stage: Stage2
    variables:
      foo2: $[ stageDependencies.Stage1.Job1.outputs['step1.bar'] ]
    jobs:
      - job:
        steps:
          - script: echo $(foo2)
            displayName: read foo2

Variable is comming from a PowerShell:

write-host "Run LoadData"
write-host "##vso[task.setvariable variable=foo]'one;two'"

This work fine. Logs showing expected string.

Then I add this to main:

  - template: myTemplate.yml
    parameters:
      foo3: $[ stageDependencies.Stage1.Job1.outputs['step1.bar'] ]

With the temple file:

# myTemplate.yml
parameters:
  foo3: ""

    stages:
      - stage: Stage3
        dependsOn: [Stage1]
        jobs:
          - job:
            steps:
              - script: echo ${{ parameters.foo3 }}

This time it doesn't work. I got this error message:

Script contents: echo $[stageDependencies.Stage1.Job1.outputs['step1.bar'] ]

========================== Starting Command Output ===========================

/usr/bin/bash --noprofile --norc /home/vsts/work/_temp/5ecfbe2d-20cd-43bf-b6b4-1a4f088d5e17.sh

/home/vsts/work/_temp/5ecfbe2d-20cd-43bf-b6b4-1a4f088d5e17.sh: line 1: stageDependencies.Stage1.Job1.outputs['step1.bar'] : syntax error: invalid arithmetic operator (error token is ".Stage1.Job1.outputs['step1.bar'] ") ##[error]Bash exited with code '1'.

It seems the variable is not expanded in main.

What I need

What can I do to make this script working? Why the variable is not expanded when transmitted to template?

Thank you


Solution

  • I think I found a solution after a lot of try and it's easy.

    I modify main like this:

    stages:
      - stage: Stage1
        jobs:
          - job: Job1
            steps:
              - task: powershell@2
                inputs:
                  filePath: Tests/LoadData.ps1
    
              - script: echo $(foo)
                displayName: read foo
    
              - bash: echo "##vso[task.setvariable variable=bar;isOutput=true]$(foo)"
                name: step1
                displayName: create public variable
    
      - template: myTemplate.yml
    

    And change template like:

    stages:
      - stage: Stage3
        variables:
          foo3: $[ stageDependencies.Stage1.Job1.outputs['step1.bar'] ]
        dependsOn: [Stage1]
        jobs:
          - job:
            steps:
              - script: echo $(foo3)
    

    This time it's working!