I would like to read some key-value pairs from a YAML/JSON object and iterate a couple stages over these pairs. I tried the below approach but it does work as it says fromJson
is invalid. Is there any other way to achieve this?
pool:
vmImage: 'ubuntu-latest'
steps:
- task: AzurePowerShell@5
displayName: 'Read pairs from YAML with Azure PowerShell'
inputs:
azureSubscription: 'YourAzureSubscriptionServiceConnectionName'
ScriptType: 'InlineScript'
Inline: |
# Read the YAML file
$pairsFilePath = "$(Build.SourcesDirectory)/pairs.yml"
$yamlContent = Get-Content -Path $pairsFilePath -Raw | ConvertFrom-Yaml
# Convert the pairs to JSON and set as an output variable
$pairsJson = $yamlContent.pairs | ConvertTo-Json -Compress
Write-Host "##vso[task.setVariable variable=pairsJson;isOutput=true]$pairsJson"
azurePowerShellVersion: 'LatestVersion'
name: ReadPairs
- ${{ each pair in fromJson(dependencies.ReadPairs.outputs['ReadPairs.pairsJson']) }}:
- template: stages/test.yml
parameters:
targetEnvironment: 'Dev'
key: ${{ pair.key }}
value: ${{ pair.value }}
pairs:
- key: "test1"
value: "abc"
- key: "test2"
value: "def"
- key: "test3"
value: "ghi"
I would like to read some key-value pairs from a YAML/JSON object and iterate a couple stages over these pairs. I tried the below approach but it does work as it says fromJson
is invalid. Is there any other way to achieve this?
pool:
vmImage: 'ubuntu-latest'
steps:
- task: AzurePowerShell@5
displayName: 'Read pairs from YAML with Azure PowerShell'
inputs:
azureSubscription: 'YourAzureSubscriptionServiceConnectionName'
ScriptType: 'InlineScript'
Inline: |
# Read the YAML file
$pairsFilePath = "$(Build.SourcesDirectory)/pairs.yml"
$yamlContent = Get-Content -Path $pairsFilePath -Raw | ConvertFrom-Yaml
# Convert the pairs to JSON and set as an output variable
$pairsJson = $yamlContent.pairs | ConvertTo-Json -Compress
Write-Host "##vso[task.setVariable variable=pairsJson;isOutput=true]$pairsJson"
azurePowerShellVersion: 'LatestVersion'
name: ReadPairs
- ${{ each pair in fromJson(dependencies.ReadPairs.outputs['ReadPairs.pairsJson']) }}:
- template: stages/test.yml
parameters:
targetEnvironment: 'Dev'
key: ${{ pair.key }}
value: ${{ pair.value }}
pairs:
- key: "test1"
value: "abc"
- key: "test2"
value: "def"
- key: "test3"
value: "ghi"
Share
Improve this question
edited Jan 20 at 9:08
jonrsharpe
122k30 gold badges266 silver badges473 bronze badges
asked Jan 20 at 9:03
user3310115user3310115
1,4604 gold badges26 silver badges59 bronze badges
8
|
Show 3 more comments
1 Answer
Reset to default 1Agreed with Rui Jarimba's comment, using the Template parameters is convenient in the yaml.
For example:
trigger:
- none
parameters:
- name: myObject
type: object
default:
test1: 'value1'
test2: 'value2'
test3: 'value3'
steps:
- ${{ each object in parameters.myObject }}:
- template: /test.yml
parameters:
targetEnvironment: 'Dev'
key: ${{ object.key }}
value: ${{ object.value }}
If you want to add more values in the parameter, you can edit the yaml file or modify it when run the pipeline.
fromJson
in Azure DevOps - a list of available functions can be found here. – Rui Jarimba Commented Jan 20 at 11:18