Parsing/Querying 地图在 groovy
Parsing/Querying Map in groovy
在我的 Jenkins 管道中,我正在捕获 jenkins 管道中每个阶段的状态。
#!/usr/bin/env groovy
rstages=[:]
pipeline {
agent {
label 'RDama-Machine'
}
stages {
stage('Validate Fail fast') {
parallel {
stage('stage A') {
steps {
echo 'stage A started'
echo 'stage A Ended'
}
post {
always {
stageresults()
}
} //post
}
stage('stage B C') {
stages {
stage('stage B') {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
echo 'stage B started'
sleep 5
bat 'exit 1'
echo 'stage B Ended' //will not execute because of above sh return
}
}
post {
always {
stageresults()
}
} //post
}
stage('stage C') {
steps {
echo 'stage C started'
sleep 10
echo 'stage C Ended' //will not execute because of above stage fail
}
post {
always {
stageresults()
}
} //post
}
stage('Validate-Status') {
steps {
script {
println rstages
}//script
}
}
}
}
stage('stage D') {
steps {
echo 'stage D started'
echo 'stage D Ended' //May complete before Stage A fails
}
post {
always {
stageresults()
}
} //post
}
}
}
stage('final stage sequential') {
steps {
script {
echo "The complete run!"
}
}
post {
always {
stageresults()
}
} //post
}
}
}
def stageresults(){
script{
//println "RESULT: ${currentBuild.result}"
//println "current stage name is: ${env.STAGE_NAME}"
def cstage="${env.STAGE_NAME}".replaceAll("[^a-zA-Z0-9 ]+","")
//println cstage
//println rstages."${env.STAGE_NAME}" = "'${currentBuild.result}'"
rstages.put((cstage), "${currentBuild.result}")
println rstages
//if("${currentBuild.result}" == "FAILURE") {
//println "printing logs for failed stage"
//}
}
}
当我打印输出时 println rstages
我看到下面。
{stage A=SUCCESS, stage D=SUCCESS, stage B=FAILURE, stage C=SUCCESS, final stage sequential=SUCCESS}
首先,当我们将元素写入数组时,不确定为什么它显示在大括号中,而不是方括号[]。
第二件事是,对于每个键,如果有任何值关联为“FAILURE”,我想将该键名打印为失败。例如,在上面的列表中,“阶段 B”失败了。
你能帮帮忙吗。谢谢。
rstages=[:]
是一个Map的声明。在 Jenkins 管道脚本中,地图是用大括号打印的(尽管在常规 Groovy jdk toString()
中实现了用括号打印地图)。
在您的示例中,可以这样打印与“FAILURE”关联的键:
rstages.findAll { it.value == 'FAILURE' }.each {
println "stage ${it.key} failed"
}
在我的 Jenkins 管道中,我正在捕获 jenkins 管道中每个阶段的状态。
#!/usr/bin/env groovy
rstages=[:]
pipeline {
agent {
label 'RDama-Machine'
}
stages {
stage('Validate Fail fast') {
parallel {
stage('stage A') {
steps {
echo 'stage A started'
echo 'stage A Ended'
}
post {
always {
stageresults()
}
} //post
}
stage('stage B C') {
stages {
stage('stage B') {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
echo 'stage B started'
sleep 5
bat 'exit 1'
echo 'stage B Ended' //will not execute because of above sh return
}
}
post {
always {
stageresults()
}
} //post
}
stage('stage C') {
steps {
echo 'stage C started'
sleep 10
echo 'stage C Ended' //will not execute because of above stage fail
}
post {
always {
stageresults()
}
} //post
}
stage('Validate-Status') {
steps {
script {
println rstages
}//script
}
}
}
}
stage('stage D') {
steps {
echo 'stage D started'
echo 'stage D Ended' //May complete before Stage A fails
}
post {
always {
stageresults()
}
} //post
}
}
}
stage('final stage sequential') {
steps {
script {
echo "The complete run!"
}
}
post {
always {
stageresults()
}
} //post
}
}
}
def stageresults(){
script{
//println "RESULT: ${currentBuild.result}"
//println "current stage name is: ${env.STAGE_NAME}"
def cstage="${env.STAGE_NAME}".replaceAll("[^a-zA-Z0-9 ]+","")
//println cstage
//println rstages."${env.STAGE_NAME}" = "'${currentBuild.result}'"
rstages.put((cstage), "${currentBuild.result}")
println rstages
//if("${currentBuild.result}" == "FAILURE") {
//println "printing logs for failed stage"
//}
}
}
当我打印输出时 println rstages
我看到下面。
{stage A=SUCCESS, stage D=SUCCESS, stage B=FAILURE, stage C=SUCCESS, final stage sequential=SUCCESS}
首先,当我们将元素写入数组时,不确定为什么它显示在大括号中,而不是方括号[]。
第二件事是,对于每个键,如果有任何值关联为“FAILURE”,我想将该键名打印为失败。例如,在上面的列表中,“阶段 B”失败了。
你能帮帮忙吗。谢谢。
rstages=[:]
是一个Map的声明。在 Jenkins 管道脚本中,地图是用大括号打印的(尽管在常规 Groovy jdktoString()
中实现了用括号打印地图)。在您的示例中,可以这样打印与“FAILURE”关联的键:
rstages.findAll { it.value == 'FAILURE' }.each { println "stage ${it.key} failed" }