最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Read from JSON or YAML and iterate over stages in Azure DevOps - Stack Overflow

programmeradmin3浏览0评论

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
  • There's no such function as fromJson in Azure DevOps - a list of available functions can be found here. – Rui Jarimba Commented Jan 20 at 11:18
  • What you're trying to achieve is not possible using a single pipeline: templates are generated at compile time, while reading a custom file will be processed at runtime. Have you considered using template parameters instead of parsing a custom file? – Rui Jarimba Commented Jan 20 at 11:23
  • @RuiJarimba - How is reading from a template parameters file different from reading from a normal file? – user3310115 Commented Jan 20 at 14:21
  • I mentioned template parameters, not template parameters files. The difference is that template parameters are part of the Azure Pipelines YAML schema and can be processed at compile time, i.e. when stages/jobs/etc are dynamically generated. In other words, you cannot generate stages after the pipeline runs. – Rui Jarimba Commented Jan 20 at 14:31
  • Please provide more details/context regarding these key-value pairs - where are theese coming from and how dynamic are these? If you have always the same number of key-value pairs maybe consider using variable templates? – Rui Jarimba Commented Jan 20 at 14:33
 |  Show 3 more comments

1 Answer 1

Reset to default 1

Agreed 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.

发布评论

评论列表(0)

  1. 暂无评论