Take the 2-minute tour ×

In a project I'm working on we are using shell scripts to execute different tasks. Some scripts are SH/Bash that runs Rsync and some are PHP scripts. One of the PHP scripts are running some integration tests that output to JUnit XML, code coverage reports and similar.

Jenkins are able to mark the jobs as successful / failed based exit status. In PHP the script exits with 1 if it has detected that failed tests during the run. The other shell scripts runs commands and uses the exit codes from those to mark a build as failed.

// :: End of PHP script:
// If any tests have failed, fail the build
if ($build_error) exit(1);

In the Jenkins Terminology a unstable build is defined as


How can I get Jenkins to mark a build as unstable instead of only success / failed when running shell scripts?

share|improve this question

7 Answers 7

up vote 16 down vote accepted

Use the Text-finder plugin.

Instead of exiting with status 1 (which would fail the build), do:

if ($build_error) print("TESTS FAILED!");

Than in the post-build actions enable the Text Finder, set the regular expression to match the message you printed (TESTS FAILED!) and check the "Unstable if found" checkbox under that entry.

share|improve this answer

It can be done without printing magic strings and using TextFinder. Here's some info on it.

Basically you need a .jar file from http://yourserver.com/cli available in shell scripts, then you can use the following command to mark a build unstable:

java -jar jenkins-cli.jar set-build-result unstable

To mark build unstable on error, you can use:

failing_cmd cmd_args || java -jar jenkins-cli.jar set-build-result unstable

The problem is that jenkins-cli.jar has to be available from shell script. You can either put it in easy-to-access path, or download in via job's shell script:

wget ${JENKINS_URL}jnlpJars/jenkins-cli.jar
share|improve this answer
I really like this solution, I implemented an ruby class for this for easy reuse in my rakefiles. :) –  Shire Mar 16 '12 at 22:20
+1 - this is a better solution than the accepted answer because the Text Finder can only search for one string per job, so you can only set the build status to one of two values. –  gareth_bowles Nov 15 '13 at 23:31
Interesting solution. But if your Jenkins requires authentication, you will need to set up public key authentication in its config or any jenkins-cli command will fail with an AccessDeniedException. –  Tom De Leu Feb 11 at 11:26

you should also be able to use groovy and do what textfinder did

marking a build as un-stable with groovy post-build plugin

Also see Groovy Postbuild Plugin

share|improve this answer

In my job script, I have the following statements (this job only runs on the Jenkins master):

# This is the condition test I use to set the build status as UNSTABLE
if [ ${PERCENTAGE} -gt 80 -a ${PERCENTAGE} -lt 90 ]; then
  echo WARNING: disc usage percentage above 80%

  # Download the Jenkins CLI JAR:
  curl -o jenkins-cli.jar ${JENKINS_URL}/jnlpJars/jenkins-cli.jar

  # Set build status to unstable
  java -jar jenkins-cli.jar -s ${JENKINS_URL}/ set-build-result unstable


You can see this and a lot more information about setting build statuses on the Jenkins wiki: https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+CLI

share|improve this answer

If shell ended with failed command that all OK (build failed:) In case of failed command inside shell script do the check after command:

 if [ "$?" -ne 0 ]; then
    exit 1
share|improve this answer

You can just call "exit 1", and the build will fail at that point and not continue. I wound up making a passthrough make function to handle it for me, and call safemake instead of make for building:

function safemake {
  make "$@"
  if [ "$?" -ne 0 ]; then
    exit 1
share|improve this answer
exit 1, will as far as I know only make the build fail. I do not want the build to fail, I want it to be marked unstable. –  HNygard Nov 8 '12 at 0:48
  1. Configure PHP build to produce xml junit report

    <phpunit bootstrap="tests/bootstrap.php" colors="true" >
           <log type="junit" target="build/junit.xml" 
               logIncompleteSkipped="false" title="Test Results"/>
  2. Finish build script with status 0

    exit 0;
  3. Add post-build action Publish JUnit test result report for Test report XMLs. This plugin will change Stable build to Unstable when test are failing.

  4. Add Jenkins Text Finder plugin with console output scanning and unchecked options. This plugin fail whole build on fatal error.

    PHP Fatal error:
share|improve this answer

Your Answer


