在Jenkins管道中将json变量列表循环到stage

6pp0gazn  于 2023-01-18  发布在  Jenkins
关注(0)|答案(1)|浏览(155)

我已经能够获得一个特定键的值列表,该键是所提供的json中的数组,将出现在shell脚本中:
"echo ${list.jvm.pega['jenkins-name']}"输出以下值:

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

我怎样才能循环数组中的每个列表,并将其传递给stage下的节点?预期的解决方案是将每个列表循环为

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`

                """               
              }  
            }

该脚本为:

#!/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` 

            """
        
          }  
        }

     }

}

json文件:

{
    "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

                  }
                }
    
            }
        }
}
nimxete2

nimxete21#

我补充道:

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

......

}

}

相关问题