Reputation: 1094
UPDATE: This appears to have been a bug introduced in Pipeline: Declarative
plugin version 1.3.5
- downgrading to 1.3.4.1
resolves this issue. Ticket was created at:
How do I get the real status of a build when using on a remote executor?
Dilemma:
currentBuild.result
is NULL and currentBuild.currentResult
is SUCCESSHow do I access the actual failure when builds are executed on a remote executioner?
Code view:
pipeline {
agent any
stages {
stage("test run") {
steps {
sh "exit 1"
}
}
}
post {
always {
echo "I always run: ${currentBuild.result} <> ${currentBuild.currentResult}"
}
success {
echo "I'm successful: ${currentBuild.result} <> ${currentBuild.currentResult}"
}
failure {
echo "I failed: ${currentBuild.result} <> ${currentBuild.currentResult}"
}
fixed {
echo "I'm fixed!: ${currentBuild.result} <> ${currentBuild.currentResult}"
}
}
}
And the output:
[Pipeline] Start of Pipeline
[Pipeline] node
Running on build-096575a3-e6af-4fff-9ca1-84cc46ba4b86-f9b8d29c in /var/vcap/data/jenkins-slave/workspace/test-job
[Pipeline] {
[Pipeline] stage
[Pipeline] { (test run)
[Pipeline] sh
+ exit 1
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
I always run: null <> SUCCESS
[Pipeline] echo
I failed: null <> SUCCESS
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
Upvotes: 6
Views: 12359
Reputation: 144
You can use try catch:
try {
sh "exit 1"
currentBuild.result = 'SUCCESS'
}catch (Exception err) {
currentBuild.result = 'FAILURE'
}
Upvotes: 0
Reputation: 1094
(Posting an "official answer" to my issue):
This is a "bug" introduced in Pipeline: Declarative
plugin version 1.3.5
- downgrading to 1.3.4.1
resolves this issue. Ticket was created at:
There are back and forths between users and the devs about whether this is really a bug or not. The devs are recommending NOT to use currentBuild.result
(anymore) but many are pointing out this is a BC break and causing a lot of issues.
Issue has been marked Critical
- may get rolled back or redeveloped to be better implemented.
If you relied on this feature, either currentBuild.result
or currentBuild.currentResult
, I recommend posting your comments on this bug.
Upvotes: 3