Home > OS >  Get each key and value from selected parameter environment json
Get each key and value from selected parameter environment json

Time:01-10

I have a json file in the repo that I would like to get the values from each key based on the environment selected. At the moment the params when selected can get all the json part of the selected environment. Eg I selected Upgrade in the below example, but do not know how to call value for each one like Printing this .....

$.[env].domain 
$.[env].resources 
$.[env].resources.db-schemas 
$.[env].resources.db-schemas.[name].jvm 
$.[env].jvm 
$.[env].jvm.[type] 
$.[env].jvm.[type](.jvm-name).ip 
$.[env].jvm.[type](.jvm-name.port)

The output am getting is this :

"level": 1,
        "domain": "develop.autosample.co.uk",
        "resources": {
            "db-schemas": {
                "rule": {
                    "schema": "pegarules",
                    "database": "sas_develop",
                    "jvm": ["primary", "secondary"]
                },
          .......
        .......

But I want to be able to call each key and value of the selected json environment. The groovy script is :

#!/usr/bin/env groovy    

node{
    properties([
    parameters([

   choice(
        name: 'environment',
        choices: ['','Upgrade','BV' ],
        description: 'environment to choose'
        ),

    ])
]) 

    deleteDir()
        dir('dir-switch') {

    stage('Checkout') {
   
   // git branch: 'test-upgrade-json', url: 'https://gitlab.xxxxxxx/pipeline.git'
  // stash includes: '**', name: 'upgrade-pega'
        
checkout([$class: 'GitSCM', branches: [[name: '*/test-upgrade-json']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'jenkins-user-github', url: 'https://gitlab.xxxxx/pipeline.git']]])
                
    }

     
            stage('Get Environment') {
            sh """
                ls -lart ./*
                ls ${env.WORKSPACE}
                cp -R ./upgrade-pega/environment/env.json  /${env.WORKSPACE}/dir-switch
                ls /${env.WORKSPACE}/dir-switch
            """
        
          }  


def obj = readJSON file: './env.json'
def list = obj[params.environment];

println list

list.each { println it }
/////   print each key and value in the json to be able to used in the code pipeline

stage('JVM check content') { 

/////   print each key and value in the json to be able to used in the code pipeline
    }
  }

}
        

The json file is :

  {
    "upgrade": {
        "level": 1,
        "domain": "develop.autosample.co.uk",
        "resources": {
            "db-schemas": {
                "rule": {
                    "schema": "pegarules",
                    "database": "sas_develop",
                    "jvm": ["primary", "secondary"]
                },
                "data": {
                    "schema": "pegadata",
                    "database": "sas_develop",
                    "jvm": ["primary", "secondary"]
                },
                "report": {
                    "schema": "pegareports",
                    "database": "sas_develop",
                    "jvm": ["primary", "secondary"]
                }
            }
        },
        "jvm": {
            "load-balancer": null,
            "pega": [{
                "jenkins-name": "ent-giem-sasw02",
                "host": "ent-giem-sasw02",
                "ip": "x.x.x.x"
            }],
            "db": {
                "primary": {
                    "jenkins-name": "ent-giem-sasrd26",
                    "host": "ent-giem-sasrd26",
                    "ip": "x.x.x.x",
                    "port": 5432
                },
                "secondary": {
                    "jenkins-name": "ent-giem-sasrd98",
                    "host": "ent-giem-pgrd98",
                    "ip": "x.x.x.x",
                    "port": 5432


                  }
                }
    
            }
        }
}

Would I save this for example upgraded sample outout json into variable or file and start using jq?

CodePudding user response:

You've already got the JSON object in memory with the list variable. Now you just use it in your Steps or Jenkins file.

If I were you I'd rename somethings to make it more explanatory like obj -> envConfig, and list -> envSettings. But you can refer to things like so:

stage('JVM...') {
   sh "echo ${envSettings.jvm.pega['jenkins-name']}"
}

Beyond that I'm not sure what you are asking. Is the iteration over the properties not working (ie list.each { println( it ) }), or is it something else?

  • Related