groovy中Map的解析/查询
在我的Jenkins管道中,我捕获Jenkins管道中每个阶段的状态groovy中Map的解析/查询,groovy,jenkins-pipeline,jenkins-groovy,Groovy,Jenkins Pipeline,Jenkins Groovy,在我的Jenkins管道中,我捕获Jenkins管道中每个阶段的状态 #!/usr/bin/env groovy rstages=[:] pipeline { agent { label 'RDama-Machine' } stages { stage('Validate Fail fast') { parallel { stage('stage A') {
#!/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,我想将该键名打印为failed。例如,在上述列表中,阶段B失败
你能帮忙吗。谢谢
rstages=[:]是一个声明。在Jenkins管道脚本中,地图是用大括号打印的,尽管在常规Groovy中jdk toString被实现为用括号打印地图
在您的示例中,可以通过以下方式打印与故障相关的密钥:
rstages.findAll { it.value == 'FAILURE' }.each {
println "stage ${it.key} failed"
}
听起来这不是列表,而是地图。调用.class以了解它是什么。或者,如果.class像通常对映射那样返回null,因为它被解释为对键类的查询,请尝试.getClass.Thank Vitalii。谢谢你的帮助。