1

In most of my jenkins jobs, I have bash script.

In some conditions, I want to make my build instable.

When I exit the script with the code 0, the build finish in SUCCESS (GREEN COLOR) and when I exit with another code the job is failed (RED COLOR).

Is there a way to make the jenkins job unstable (YELLOW COLOR) from a bash script ?

Youssouf Maiga
  • 6,701
  • 7
  • 26
  • 42
  • 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) – burnettk Apr 14 '17 at 22:25

2 Answers2

2

If you use the pipeline plugin and write your jobs in groovy pipline dsl, yes:

try {
    sh "yourscript.sh"
} catch(Exception e) {
    currentBuild.result = 'UNSTABLE'
}
haschibaschi
  • 2,734
  • 23
  • 31
2

Jenkins is designed to set an automatic fail when the exit code of your script is 1. There are plenty of ways of overriding this, but most involve Jenkins CLI or a plugin. Using the returnStatus flag for shell commands https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/, Jenkins will not automatically fail but instead, return the status code.

Here's the documentation:

returnStatus (optional) Normally, a script which exits with a nonzero status code will cause the step to fail with an exception. If this option is checked, the return value of the step will instead be the status code. You may then compare it to zero, for example.

script{
    def returnVal = sh(
        returnStatus: true,
        script: '''
        **your script here**
        '''
    )
    if(returnVal != 0) {
        currentBuild.result = 'UNSTABLE'
    }
}
radkenji
  • 21
  • 3
  • 1
    Thanks for providing code which might help solve the problem, but generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem. – Neuron Jun 16 '18 at 22:10