Home > Enterprise >  Looping the list of variables of json into stage in Jenkins pipeline
Looping the list of variables of json into stage in Jenkins pipeline

Time:01-19

I have been able to get the list of the value for a particular key which is array in the provided json to be in the shell script :

The "echo ${list.jvm.pega['jenkins-name']}" output these values :

 ["ent-giem-sasw02","ent-giem-sasw03","ent-giem-sasw04"]

How would I be able to loop each of the list in the in the array and pass it to the node under stage. Expected solution is to loop each of the list as

 stage('delete_pegarules_schema_for_each_node') {
   
///loop 3 times based on the ${list.jvm.pega['jenkins-name'] output list
 
  node("${list.jvm.pega['jenkins-name']") {
       sh """
         echo -e "-------------------------------System Information of current node running ----------------------------"
      echo -e "Hostname:\t\t"`hostname`

                """               
              }  
            }

The 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)  }
    
stage('JVM check content') {   

sh "echo ${list.jvm.pega['jenkins-name']}" 
   
          }

   stage('delete_pegarules_schema_for_each_node') {
    
///loop 

   node("Expecting the each of the loop in the list of ${list.jvm.pega['jenkins-name']}") {
            sh """
              echo -e "-------------------------------System Information of current node running ----------------------------"
  echo -e "Hostname:\t\t"`hostname` 

            """
        
          }  
        }


     }

}

The json file :

 {
    "upgrade": {
        "level": 1,
        "domain": "develop.autosample.co.uk",
        "resources": {
            "db-schemas": {
                "rule": {
                    "schema": "pegarules",
                    "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"
            },
            
            {
                "jenkins-name": "ent-giem-sasw03",
                "host": "ent-giem-sasw03",
                "ip": "x.x.x.x"
            },
            
            {
                "jenkins-name": "ent-giem-sasw04",
                "host": "ent-giem-sasw04",
                "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


                  }
                }
    
            }
        }
}

CodePudding user response:

I added this :

    list.jvm.pega['jenkins-name'].each { elemupdate ->
      echo "Item: ${elemupdate}"
    
    
       stage('delete_pegarules_schema') {
    
    
    
       node("$elemupdate") {

......

}

}
  • Related