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 c
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:
Simple way in a pipeline:
try {
sh "<Whatever your command mayeth be>"
} catch (Exception e) {
// Caught a non-zero exit code
// Mark build as unstable.
currentBuild.result='UNSTABLE'
}
As others have posted, there is no return [exit] code that signals "unstable", but there are a number of approaches you can take. To save you the trouble of reading the Jenkins docs and experimenting, as I did, here's one way to do it, which has worked for me.
It's based on the "CLI" that Jenkins provides. The CLI is implemented as a standalone JAR that can be downloaded from Jenkins itself. In the following sample, we download that JAR (if not already downloaded), and invoke it with the command "set-build-result" and argument "unstable".
Your script should then exit with a zero status, or else the build will be FAILED.
The console log will show the build becoming "unstable" when the script build step finishes; not immediately upon execution of that CLI command.
unstable() {
test -f jenkins-cli.jar || wget -q ${JENKINS_URL}/jnlpJars/jenkins-cli.jar
java -jar jenkins-cli.jar set-build-result unstable
}
do-something-that-might-go-badly || unstable
exit 0
Any Build Step Plugin
Fail The Build Plugin
first build step: Execute command
Command: echo build_step
second build step: conditional step
Run: Not
!:Execute command
Command: echo test_step
Builder: Set the build result
Result: Unstable
change "echo test_step" to "echo_test_step" to see how it works
if BUILD fails the result is FAILED, and TEST is not runned
if BUILD succedes, TEST runs, and if fails the result is UNSTABLE
if BUILD succedes, and TEST succedes, the result is SUCCESS
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
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: