You can loop through and print the strings in an array passed as a parameter to an Azure Devops Yaml Pipeline Template. You can also loop through a string array passed as part of a complex object, or coalesced with another array - but not both.
Below I have 2 working examples and then the one that crashes.
Working example #1
azure-deploy.yml:
extends:
template: azure-deploy-template.yml
parameters:
complexObject:
- drink: 'water'
foods:
- 'apple'
- 'banana'
azure-deploy-template.yml:
parameters:
- name: complexObject
type: object
jobs:
- job: TestJob
pool:
vmImage: 'ubuntu-latest'
steps:
- ${{ each o in parameters.complexObject }}:
- ${{ each f in o.foods }}:
- script: echo '${{convertToJson(f)}}'
- script: echo '${{f}}'
outputs:
"apple"
apple
"banana"
banana
Working example #2:
azure-deploy.yml:
extends:
template: azure-deploy-template.yml
parameters:
animals:
- 'cat'
- 'dog'
complexObject:
- drink: 'water'
foods:
- 'apple'
- 'banana'
azure-deploy-template.yml
parameters:
- name: animals
type: object
- name: complexObject
type: object
- name: foods
type: object
jobs:
- job: TestJob
pool:
vmImage: 'ubuntu-latest'
steps:
- ${{ each o in parameters.complexObject }}:
- ${{ each f in coalesce(parameters.foods, parameters.animals) }}:
- script: echo '${{convertToJson(f)}}'
- script: echo '${{f}}'
outputs:
Same as #1
Problem case
azure-deploy.yml
extends:
template: azure-deploy-template.yml
parameters:
animals:
- 'cat'
- 'dog'
complexObject:
- drink: 'water'
foods:
- 'apple'
- 'banana'
azure-deploy-template.yml
parameters:
- name: animals
type: object
- name: complexObject
type: object
jobs:
- job: TestJob
pool:
vmImage: 'ubuntu-latest'
steps:
- ${{ each o in parameters.complexObject }}:
- ${{ each f in coalesce(o.foods, parameters.animals) }}:
- script: echo '${{convertToJson(f)}}'
- script: echo '${{f}}'
This fails with the error: Unable to convert from Object to String
If I remove the last line in azure-deploy-template.yml
, just the json outputs.
Output:
{
"lit": "apple",
"style": "",
"line": "13",
"col": "5",
"type": "0"
}
{
"lit": "banana",
"style": "",
"line": "13",
"col": "5",
"type": "0"
}
Why is the string array being interpreted as this strange complex object?