5

I am currently working with Azure Devops Build Pipelines, and am trying to call a template file to do some tasks from my build yaml.

I am facing some difficulties to pass parameters to the template file. Let assume that this is my template file (simplified) which works fine :

parameters:
 iterations: []

steps:
- ${{ each i in parameters.iterations }}:
- task: PowerShell@2
  displayName: "Get key values ${{i}}"
  name: getKeyValues_${{i}}
  inputs:
    targetType: 'inline'
    script: |
      $item = "${{i}}"
      Write-Host "item : $($item)"
      $keyVal = $item -split "_"
      Write-Host $keyVal
      Write-Host "key: $($keyVal[0]) | value: $($keyVal[1])"
      echo "##vso[task.setvariable variable=key;isOutput=true]$($keyVal[0])"
      echo "##vso[task.setvariable variable=value;isOutput=true]$($keyVal[1])"

So I want my iterations parameter contain something like this :

iterations: ["1_60", "2_40"]

Inside my Yaml pipeline I have the following code(also simplified) :

Not working scenario

- task: PowerShell@2
   displayName: Calculate iterations for $(copies) copies
   name: calculateIterations
   inputs:
   targetType: 'inline'
   script: |
       # Do some stuf here to get the arrow below from int value = 100 
       $iterations = ["1_60, "2_40"]
       echo "##vso[task.setvariable variable=iterations;isOutput=true]$($iterations)"

- template: container-template.yml 
   parameters:
     iterations: $(calculateIterations.iterations)

Working scenario

- task: PowerShell@2
   displayName: Calculate iterations for $(copies) copies
   name: calculateIterations
   inputs:
   targetType: 'inline'
   script: |
       # Do some stuf here to get the arrow below from int value = 100 
       $iterations = ["1_60, "2_40"]
       echo "##vso[task.setvariable variable=iterations;isOutput=true]$($iterations)"

- template: container-template.yml 
   parameters:
     iterations: ["1_60, "2_40"]

As you can see, the problem is that I am unable to use the output variable of my script to pass it as parameter to my template. When I run the not working scenario I have the following error : enter image description here

I have found this post, but no solutions yet...

Roman Lushkin
  • 131
  • 1
  • 2
  • 12

1 Answers1

4

As what 4c74356b41 said, this is the dilemma at present. In another word, the Not working scenario you mentioned does not support to achieve until now.

Now, we must let the template know the clear text at compile time. Because during this compile time, we have difficulty to do 2 or more things in one step at same time, especially contain compiling variable value, passing to corresponding template dynamic arguments and etc.


Expected a sequence or mapping. Actual value '$(calculateIterations.iterations)'

More detailed, during compile time (after you click Run but before pipeline start truely):

1) Firstly we map the value that come from YAML pipeline, to make sure the - ${{ each i in parameters.iterations }} has clear value to start.

2) After it is done, then parse exact value on name: getKeyValues_${{i}} in script order.

In your scenario, it cannot even satisfy the first step since what you passed is a variable, and we do not has parse value process here. That's why you saw error said Expected a sequence or mapping. Actual value '$(calculateIterations.iterations)'.

Another expression of this error message is: we(template) are looking forward to exact value(s) to map our dynamic parameters, but what you give is a unrecognized content $(calculateIterations.iterations). Sorry, we cannot start to run.

Mengdi Liang
  • 17,577
  • 2
  • 28
  • 35
  • Thank you for this complete explanation. I suspected that my problem was linked to something like you have described, but was not sure. – Roman Lushkin Mar 11 '20 at 07:49
  • There's a uservoice feature request here: https://developercommunity.visualstudio.com/t/dynamically-populate-runtime-parameters/1020402?space=21&q=dynamic+parameters – woter324 Apr 30 '21 at 22:36
  • Well... I struggled half a day trying to get this to work... It kind of limit the use of templates. This is a huge limitation. Considering where we are with Html generation, other templating framework and stuff, yaml looks like stone age. – Yepeekai Apr 01 '22 at 18:32