我有一個這樣的 Jenkinsfile:
pipeline {
agent any
stages {
stage('Parallel') {
parallel {
stage('Non-critical stage') {
steps {
script {
// Other steps...
if (/*some error condition*/) {
catchError(buildResult: null, stageResult: 'UNSTABLE') {
// Fail only the stage, but leave the buildResult as is
error("Error message")
}
}
}
}
}
stage('critical stage') {
steps {
// a different stage that really matters
}
}
}
post {
always {
script {
if (currentBuild.currentResult != 'SUCCESS') {
// When the Non-critical stage fails, the currentResult is UNSTABLE...
// ...even though only the stageResult should have been set.
// However, in the Jenkins UI the build result is SUCCESS
}
}
}
}
}
}
}
目標是Non-critical stage不讓構建失敗,何時some error condition滿足。但僅將階段標記為UNSTABLE。這在詹金斯作業得很好。構建結果將是SUCCESS,即使Non-critical stage是UNSTABLE。但是,在該階段的post塊設定為。ParallelcurrentBuild.currentResultUNSTABLE
post塊是被執行的最后一件事。所以我不明白,SUCCESS當正在執行的 Jenkinsfile 代碼的最后一位中的 currentResult 是 UNSTABLE 時,如何在 Jenkins 中顯示構建結果。
同樣,當Non-critical stage跳過時,currentBuild.currentResult也SUCCESS如預期的那樣。所以結果UNSTABLE肯定是由error()呼叫引起的。
uj5u.com熱心網友回復:
我發現我的問題是,后期階段并不直接在管道塊的范圍內。而是直接在stage('Parallel')街區內。
像這樣向下移動帖子塊后:
pipeline {
agent any
stages {
stage('Parallel') {
parallel {
stage('Non-critical stage') {
steps {
script {
// Other steps...
if (/*some error condition*/) {
catchError(buildResult: null, stageResult: 'UNSTABLE') {
// Fail only the stage, but leave the buildResult as is
error("Error message")
}
}
}
}
}
stage('critical stage') {
steps {
// a different stage that really matters
}
}
}
}
}
post {
always {
script {
if (currentBuild.currentResult != 'SUCCESS') {
// Now the currentResult is 'SUCCESS'
}
}
}
}
}
該currentBuild.currentResult屬性正確地保存了該值SUCCESS
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/480719.html
