I would like to get the result of my jenkins build job either failed (red), unstable (yellow) or successfull (green)...
If i return non zero from my shell script i can only get failed. Is there any return code which produces unstable ?
Update: Newer versions of Jenkins support exit codes that would set unstable
. For more info, please refer here (thanks @Pedro and @gib)
I did not test this.
Original answer below:
No. A script exit code will not produce UNSTABLE
build result in Jenkins.
The only way to set UNSTABLE
build result is programmatically through Jenkins.
This is what the test harness portion of Jenkins does.
You can also use the Jenkins CLI directly from your script to set result of the currently executing build. Instead of returning a specific exit code, have your script execute the Jenkins CLI command. For details, on your own server, goto http://<your-server>/cli/command/set-build-result
There are also a number of plugins that can do that same, including:
This is now possible in newer versions of Jenkins, you can do something like this:
#!/usr/bin/env groovy
properties([
parameters([string(name: 'foo', defaultValue: 'bar', description: 'Fails job if not bar (unstable if bar)')]),
])
stage('Stage 1') {
node('parent'){
def ret = sh(
returnStatus: true, // This is the key bit!
script: '''if [ "$foo" = bar ]; then exit 2; else exit 1; fi'''
)
// ret can be any number/range, does not have to be 2.
if (ret == 2) {
currentBuild.result = 'UNSTABLE'
} else if (ret != 0) {
currentBuild.result = 'FAILURE'
// If you do not manually error the status will be set to "failed", but the
// pipeline will still run the next stage.
error("Stage 1 failed with exit code ${ret}")
}
}
}
The Pipeline Syntax generator shows you this in the advanced tab:
As indicated in one of the comments this is and issue reported by jenkins team, already fixed and initially planed for version 2.26. See the following issue for details Permit "Execute shell" jobs to return 2 for "unstable"
But, it seems to be dependent on another issue that it is still blocking it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With