Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failure / Unstable via Script Return Code

Tags:

bash

jenkins

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 ?

like image 764
khmarbaise Avatar asked Aug 22 '14 08:08

khmarbaise


3 Answers

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:

  • Text-finder plugin.
  • Fail the build plugin (it can set any status), but you will need to use it together with Conditional Build Step plugin.
  • Groovy plugin (will need to figure out full Groovy code).
  • Groovy post-build plugin (comes with easy method to set build result).
like image 54
Slav Avatar answered Oct 08 '22 23:10

Slav


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

like image 34
gib Avatar answered Oct 09 '22 01:10

gib


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

like image 1
Pedro Avatar answered Oct 09 '22 01:10

Pedro