8

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 ?

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • 1
    possible duplicate of [How to mark a build unstable in Jenkins when running shell scripts](http://stackoverflow.com/questions/8148122/how-to-mark-a-build-unstable-in-jenkins-when-running-shell-scripts) (it's about PHP rather than Bash, but the concept should be the same) – Oliver Charlesworth Aug 22 '14 at 08:09
  • I would say the answer to my question is simply no. Only by using a supplemental plugin. – khmarbaise Aug 22 '14 at 08:52
  • That linked question talks about the CLI mode jar doing this. That means there is some way to do it. You just need to figure out what API/etc. call that jar is making to do that. – Etan Reisner Aug 22 '14 at 11:36
  • 1
    The issue is known by jenkins team : https://issues.jenkins-ci.org/browse/JENKINS-23786 – ıɾuǝʞ Nov 30 '14 at 10:30
  • It is totally possible to achieve that now, without additional plugins: https://stackoverflow.com/a/49676269/219241 – Alan Franzoni Dec 11 '18 at 10:56

6 Answers6

19

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:

Slav
  • 27,057
  • 11
  • 80
  • 104
  • This was true at the time, but you can now use `returnStatus` to achieve this (see https://stackoverflow.com/a/47158087/2799191). – gib Nov 07 '17 at 12:32
  • You can do the same thing even without a pipeline/groovy. See https://stackoverflow.com/a/49676269/219241 as well. – Alan Franzoni Apr 26 '18 at 09:59
4

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:

Pipeline Syntax Example

gib
  • 1,951
  • 1
  • 21
  • 24
1

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

Pedro
  • 692
  • 8
  • 24
  • _Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline._ – Bugs Feb 03 '17 at 13:13
  • Looks like this was reverted (or never landed), I can't get this to work in Jenkins 2.73.2. – gib Nov 07 '17 at 12:33
  • works here, just open the advanced options for execute shell and set the exit code you want for unstable – Somatik Dec 11 '17 at 22:46
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
Fantastory
  • 1,891
  • 21
  • 25
0

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
smendola
  • 2,273
  • 1
  • 15
  • 14
  • 3
    Thank you for this code snippet, which may provide some immediate help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its educational value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Jun 21 '17 at 10:58
0

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'
}
Yeow_Meng
  • 208
  • 2
  • 8