[英]Create JSON strings from Groovy variables in Jenkins Pipeline
[英]Looping the list of variables of json into stage in Jenkins pipeline
我已經能夠獲取特定鍵的值列表,該鍵是提供的 json 中的數組,位於 shell 腳本中:
"echo ${list.jvm.pega['jenkins-name']}"
output 這些值:
["ent-giem-sasw02","ent-giem-sasw03","ent-giem-sasw04"]
我如何能夠循環數組中的每個列表並將其傳遞給舞台下的節點。 預期的解決方案是將每個列表循環為
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
}
}
}
}
}
我添加了這個:
list.jvm.pega['jenkins-name'].each { elemupdate ->
echo "Item: ${elemupdate}"
stage('delete_pegarules_schema') {
node("$elemupdate") {
......
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.