Azure devops pipeline access Json file inputs and perform for(each) loop

Viewed 905

I am using, Linux agent, I need to iterate over json input objects for each project.

I have below json file and It may have more than 200 charts, I need perform build, lint, templates and push to repository, I can do this using shell/python, but I thought to use Azure pipelines yaml code.

{
    "helm": {
        "charts": [ 
            {
                "project1": {
                    "secretName" : "mysecret1",
                    "setValues": ["a", "b", "c"]
                }
            },
            {
                "project2": {
                    "secretName" : "mysecret2",
                    "setValues": ["x", "y", "z"]
                }
            }
        ]
    }
}

azure-pipelines.yaml:

trigger:
- '*'

variables:
  buildConfiguration: 'Release'
  releaseBranchName: 'dev'

stages:
- stage: 'Build'
  pool:
    name: 'linux'
  displayName: 'Build helm Projects'
  
  
  jobs: 
  - job: 'buildProjects'
    displayName: 'Building all the helm projects'
    steps:

    - task: HelmInstaller@0
      displayName: install helm
      inputs:
        helmVersion: 'latest'
        installKubectl: false

    - script: chmod -R 755 $(Build.SourcesDirectory)/
      displayName: 'Set Directory permissions'

    - task: PythonScript@0
      inputs:
        scriptSource: inline
        script: |
          import argparse, json, sys
          parser = argparse.ArgumentParser()
          parser.add_argument("--filePath", help="Provide the json file path")
          args = parser.parse_args()
          with open(args.filePath, 'r') as f:
            data = json.load(f)
            data = json.dumps(data)
          print('##vso[task.setvariable variable=helmConfigs;]%s' % (data))
        arguments: --filePath $(Build.SourcesDirectory)/build/helm/helmConfig.json
        failOnStderr: true
      displayName: setting up helm configs
      
    - template: helmBuild.yml
      parameters: 
        helmCharts: $(HELMCONFIGS)

Json input is saved to HELMCONFIGS variable in azure pipelines, As per Azure documents, it string type and we cannot convert to any other type like array.

helmBuild.yml file:

parameters:
  - name: helmCharts
    type: object
    default: {}

steps :
- ${{ each helm in parameters.helmCharts }}:
  - ${{ each charts in helm}}:
    - ${{ each chart in charts }}:
      - task: AzureKeyVault@1
        inputs:
          azureSubscription: 'A-B-C'
          KeyVaultName: chart.secretName
          SecretsFilter: '*'
          RunAsPreJob: true
    
    

I am not able to access chart.secretName, How to access to secretNames input?

0 Answers
Related