I have a Jenkinsfile like this:
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
}
}
}
}
}
}
}
In the Non-critical stage
the goal is to not let the build fail, when some error condition
is met. But to only mark the stage as UNSTABLE
. This works fine in Jenkins. The build result will be SUCCESS
, even when the Non-critical stage
is UNSTABLE
. However, in the post
block of the Parallel
stage currentBuild.currentResult
is set to UNSTABLE
.
The post
block is the last thing being executed. So I do not understand, how the build result can be shown as SUCCESS
in Jenkins, when the currentResult is UNSTABLE in the last bit of Jenkinsfile code being executed.
Also when the Non-critical stage
is skipped, currentBuild.currentResult
is SUCCESS
as expected. So the result being UNSTABLE
is definitely caused by the error()
call.
CodePudding user response:
I found out that my problem was, that the post stage was not directly in the scope of the pipeline block. And instead was directly in the stage('Parallel')
block.
After moving the post block down like this:
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'
}
}
}
}
}
the currentBuild.currentResult
property correctly holds the value SUCCESS