如何 运行 流水线而不考虑阶段失败并捕获错误并在最后报告失败的阶段
How to run pipeline irrespective of stage failure and capture errors and report the failed stages at end
首先,感谢您关注我的问题。
我有以下管道。
pipeline {
agent any
stages {
stage(‘one’) {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
bat '''
echo “stage one”;
exit 1;
'''
echo "RESULT: ${currentBuild.result}"
//echo "RESULT2 ${currentBuild.stageResult}"
} //catch
}//steps
}//stage 1
stage('RunParallel') {
parallel {
stage(‘two’) {
steps {
bat '''
echo “stage two”;
exit 0;
'''
echo "RESULT: ${currentBuild.result}"
}
}
stage(‘three’) {
steps {
bat '''
echo “stage three”;
exit 0;
'''
echo "RESULT: ${currentBuild.result}"
}
}
stage(‘four’) {
steps {
bat '''
echo “stage four”;
exit 0;
'''
echo "RESULT: ${currentBuild.result}"
}
}
}//parallel
}//runParallel
stage(‘validate’) {
steps {
echo "RESULT: ${currentBuild.result}"
bat 'echo validation stage'
}
}
stage(‘five’) {
steps {
bat '''
echo “stag e 5”;
exit 0;
'''
}
}
}
post {
always {
bat '''
echo ‘I will always execute this!’
'''
}
}
}
管道看起来像他的。
我的目标是:
- 即使阶段 1 失败,我也需要 运行 阶段 2,3,4
- 一个。如果阶段 1、2、3、4 失败,我必须在验证阶段报告它并使构建失败
b.如果阶段 1、2、3、4 成功,我需要通过验证并继续进行阶段 5 和后续阶段
现在我可以用 catch 块做第一步了。并且,停留在第 2 步。您能否指导如何捕获阶段结果并报告失败的阶段。
最后,我成功地捕获了所有阶段结果并过滤了失败的阶段。现在我可以围绕它编写条件。这是我得到的。
#!/usr/bin/env groovy
rstages=[:]
pipeline {
agent any
stages {
stage(‘one’) {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
bat '''
echo “stage one”;
exit 1;
'''
script{
echo "stage name is: ${env.STAGE_NAME}"
echo "RESULT: ${currentBuild.result}"
}
} //catch
}//steps
post {
always {
script{
println "RESULT: ${currentBuild.result}"
println "current stage name is: ${env.STAGE_NAME}"
rstages."${env.STAGE_NAME}" = "${currentBuild.result}"
}
}
}
}//stage 1
stage('RunParallel') {
parallel {
stage(‘two’) {
steps {
bat '''
echo “stage two”;
exit 0;
'''
echo "RESULT: ${currentBuild.result}"
}
post {
always {
script{
println "current stage name is: ${env.STAGE_NAME}"
rstages."${env.STAGE_NAME}" = "${currentBuild.result}"
}
}
}
}
stage(‘three’) {
steps {
bat '''
echo “stage three”;
exit 0;
'''
echo "RESULT: ${currentBuild.result}"
}
post {
always {
script{
println "current stage name is: ${env.STAGE_NAME}"
rstages."${env.STAGE_NAME}" = "${currentBuild.result}"
}
}
}
}
stage(‘four’) {
steps {
bat '''
echo “stage four”;
exit 0;
'''
echo "RESULT: ${currentBuild.result}"
}
post {
always {
script{
println "current stage name is: ${env.STAGE_NAME}"
rstages."${env.STAGE_NAME}" = "${currentBuild.result}"
}
}
}
}
}//parallel
}//runParallel
stage(‘validate’) {
steps {
script{
//println RESULT: "${currentBuild.result}"
//println "stage name is: ${env.STAGE_NAME}"
println "Printing all stage results"
rstages.each{entry -> println "$entry.key=$entry.value"} // to print all elements in map object rstages
}
script{
//help: https://www.baeldung.com/groovy-maps
println "printing failed stages"
def fstages=rstages.findAll{ key, value -> value.contains('FAIL') } //to print all keys matching a value FAILURE
println fstages
}
}
post {
always {
script{
println "current stage name is: ${env.STAGE_NAME}"
rstages."${env.STAGE_NAME}" = "${currentBuild.result}"
}
}
}
}
stage(‘five’) {
steps {
bat '''
echo “stage 5”;
exit 0;
'''
}
}
}
post {
always {
bat '''
echo ‘I will always execute this!’
'''
}
}
}
首先,感谢您关注我的问题。
我有以下管道。
pipeline {
agent any
stages {
stage(‘one’) {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
bat '''
echo “stage one”;
exit 1;
'''
echo "RESULT: ${currentBuild.result}"
//echo "RESULT2 ${currentBuild.stageResult}"
} //catch
}//steps
}//stage 1
stage('RunParallel') {
parallel {
stage(‘two’) {
steps {
bat '''
echo “stage two”;
exit 0;
'''
echo "RESULT: ${currentBuild.result}"
}
}
stage(‘three’) {
steps {
bat '''
echo “stage three”;
exit 0;
'''
echo "RESULT: ${currentBuild.result}"
}
}
stage(‘four’) {
steps {
bat '''
echo “stage four”;
exit 0;
'''
echo "RESULT: ${currentBuild.result}"
}
}
}//parallel
}//runParallel
stage(‘validate’) {
steps {
echo "RESULT: ${currentBuild.result}"
bat 'echo validation stage'
}
}
stage(‘five’) {
steps {
bat '''
echo “stag e 5”;
exit 0;
'''
}
}
}
post {
always {
bat '''
echo ‘I will always execute this!’
'''
}
}
}
管道看起来像他的。
我的目标是:
- 即使阶段 1 失败,我也需要 运行 阶段 2,3,4
- 一个。如果阶段 1、2、3、4 失败,我必须在验证阶段报告它并使构建失败 b.如果阶段 1、2、3、4 成功,我需要通过验证并继续进行阶段 5 和后续阶段
现在我可以用 catch 块做第一步了。并且,停留在第 2 步。您能否指导如何捕获阶段结果并报告失败的阶段。
最后,我成功地捕获了所有阶段结果并过滤了失败的阶段。现在我可以围绕它编写条件。这是我得到的。
#!/usr/bin/env groovy
rstages=[:]
pipeline {
agent any
stages {
stage(‘one’) {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
bat '''
echo “stage one”;
exit 1;
'''
script{
echo "stage name is: ${env.STAGE_NAME}"
echo "RESULT: ${currentBuild.result}"
}
} //catch
}//steps
post {
always {
script{
println "RESULT: ${currentBuild.result}"
println "current stage name is: ${env.STAGE_NAME}"
rstages."${env.STAGE_NAME}" = "${currentBuild.result}"
}
}
}
}//stage 1
stage('RunParallel') {
parallel {
stage(‘two’) {
steps {
bat '''
echo “stage two”;
exit 0;
'''
echo "RESULT: ${currentBuild.result}"
}
post {
always {
script{
println "current stage name is: ${env.STAGE_NAME}"
rstages."${env.STAGE_NAME}" = "${currentBuild.result}"
}
}
}
}
stage(‘three’) {
steps {
bat '''
echo “stage three”;
exit 0;
'''
echo "RESULT: ${currentBuild.result}"
}
post {
always {
script{
println "current stage name is: ${env.STAGE_NAME}"
rstages."${env.STAGE_NAME}" = "${currentBuild.result}"
}
}
}
}
stage(‘four’) {
steps {
bat '''
echo “stage four”;
exit 0;
'''
echo "RESULT: ${currentBuild.result}"
}
post {
always {
script{
println "current stage name is: ${env.STAGE_NAME}"
rstages."${env.STAGE_NAME}" = "${currentBuild.result}"
}
}
}
}
}//parallel
}//runParallel
stage(‘validate’) {
steps {
script{
//println RESULT: "${currentBuild.result}"
//println "stage name is: ${env.STAGE_NAME}"
println "Printing all stage results"
rstages.each{entry -> println "$entry.key=$entry.value"} // to print all elements in map object rstages
}
script{
//help: https://www.baeldung.com/groovy-maps
println "printing failed stages"
def fstages=rstages.findAll{ key, value -> value.contains('FAIL') } //to print all keys matching a value FAILURE
println fstages
}
}
post {
always {
script{
println "current stage name is: ${env.STAGE_NAME}"
rstages."${env.STAGE_NAME}" = "${currentBuild.result}"
}
}
}
}
stage(‘five’) {
steps {
bat '''
echo “stage 5”;
exit 0;
'''
}
}
}
post {
always {
bat '''
echo ‘I will always execute this!’
'''
}
}
}