简体   繁体   中英

Azure Pipeline dynamic parameters to template file from YAML pipeline

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 : 在此处输入图片说明

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

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)' 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.

After lot of attempts, I got this question in stackoverflow and came to know that there is no way to achieve this in Azure Pipeline. Pathetic. Waiting from Azure DevOps to give a way for this problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM