我的Jenkins管道并行运行多个任务。看来,如果一个阶段失败,则所有后续阶段都将运行它们的failure
post块(无论它们是否实际失败)。我不知道这是设计使然还是做错了什么。
注意:此管道在Windows节点上运行,因此 bat('exit /b 1')
pipeline {
agent any
stages {
stage('Parallel steps') {
parallel {
stage('Successful stage') {
steps {
script { sleep 10 }
}
post {
failure {
echo('detected failure: Successful stage')
}
}
}
stage('Failure stage') {
steps {
script { bat('exit /b 1') }
}
post {
failure {
echo('detected failure: Failure stage')
}
}
}
}
}
}
}
在上面的管道中,只有“失败阶段”失败,但是在输出中我看到了这一点,表明这failure
两个步骤都执行了条件!
Started by user Doe, John
Running on WINDOWS_NODE in D:\workspace
[Successful stage] Sleeping for 10 sec
[Failure stage] [test] Running batch script
[Failure stage] D:\workspace>exit /b 1
Post stage
[Pipeline] [Failure stage] echo
[Failure stage] detected failure: Failure stage
[Failure stage] Failed in branch Failure stage
Post stage
[Pipeline] [Successful stage] echo
[Successful stage] detected failure: Successful stage
ERROR: script returned exit code 1
Finished: FAILURE
对我来说,检测哪个并行阶段失败并将其报告给整个管道的最佳方法是什么?
看来这是声明性管道的已知 错误。我不得不放弃使用内置的post-> failure块,而改用try / catch,它有自己的问题:
此代码正常工作。仅故障阶段回显“检测到的故障”,而不是两者。
pipeline {
agent any
stages {
stage('Parallel steps') {
parallel {
stage('Successful stage') {
steps {
script {
try {
sleep 10
} catch (e) {
echo('detected failure: Successful stage')
throw(e)
}
}
}
}
stage('Failure stage') {
steps {
script {
try {
bat('exit /b 1')
} catch (e) {
echo('detected failure: Failure stage')
throw(e)
}
}
}
}
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句