How to note build instability in Jenkins when running shell scripts

In the project I'm working on, we use shell scripts to perform various tasks. Some SH / Bash scripts that run Rsync, and some are PHP scripts. One of the PHP scripts runs some integration tests that are output in JUnit XML, code coverage reports, etc.

Jenkins is able to mark assignments as successful / unsuccessful based on exit status . In PHP, the script exits with 1 if it finds that the tests failed during the run. Other shell scripts run commands and use exit codes to mark the assembly as unsuccessful.

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

In Jenkins terminology, an unstable assembly is defined as

A line is unstable if it was built successfully, and one or more publishers report its instability. For example, if the JUnit publisher is configured and the test fails, the assembly will be marked as unstable.

How can I get Jenkins to mark the assembly as unstable, and not just success / failure when running shell scripts?

+58
php shell build jenkins status
Nov 16 2018-11-11T00:
source share
12 answers

Use the Text-finder plugin.

Instead of exiting state 1 (which will cause the build to fail), do:

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

Than turn on the Text Finder in the actions after the assembly, set the regular expression according to the message you printed ( TESTS FAILED! ) And check the "Unstable if found" checkbox in this entry.

+47
Nov 16 '11 at 8:10
source share

This can be done without printing magic lines and using TextFinder. Below is information about him.

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

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

To mark an assembly 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 must be accessible from a shell script. You can either put it in a simple access path or load it through the shell of the script job:

 wget ${JENKINS_URL}jnlpJars/jenkins-cli.jar 
+51
Jan 11 '12 at 16:19
source share

you can also use groovy and do what textfinder did

marking assembly as unstable with groovy plugin after assembly

 if(manager.logContains("Could not login to FTP server")) { manager.addWarningBadge("FTP Login Failure") manager.createSummary("warning.gif").appendText("<h1>Failed to login to remote FTP Server!</h1>", false, false, false, "red") manager.buildUnstable() } 

Also see Groovy Postbuild Plugin.

+6
Aug 17 '12 at 19:25
source share

In my script work, I have the following statements (this task is only performed for the Jenkins wizard):

 # 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 fi 

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

+5
Nov 20 '12 at 19:52
source share
  • Configure PHP build to generate xml junit report

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

     ... exit 0; 
  • Add Post-Build Action Publish a JUnit test result report for XML test reports. This plugin will change the Stable build to Unstable if the test fails.

     **/build/junit.xml 
  • Add the Jenkins Text Finder plugin with console output and unchecked options. This plugin does not fully work with a fatal error.

     PHP Fatal error: 
+3
Feb 17 '14 at 20:52
source share

I find the most flexible way to do this by reading the file in the post w build> plugin. enter image description here

 import hudson.FilePath import java.io.InputStream def build = Thread.currentThread().executable String unstable = null if(build.workspace.isRemote()) { channel = build.workspace.channel; fp = new FilePath(channel, build.workspace.toString() + "/build.properties") InputStream is = fp.read() unstable = is.text.trim() } else { fp = new FilePath(new File(build.workspace.toString() + "/build.properties")) InputStream is = fp.read() unstable = is.text.trim() } manager.listener.logger.println("Build status file: " + unstable) if (unstable.equalsIgnoreCase('true')) { manager.listener.logger.println('setting build to unstable') manager.buildUnstable() } 

If the contents of the file are true, the assembly will be set to unstable. This will work on the local host and on any slaves on which this task is performed, and on any scripts that may be written to disk.

+2
Feb 02 '16 at 16:22
source share

If the shell ended with an unsuccessful command, everything is OK (the assembly failed :) If the command fails inside the shell script, check after the command:

  if [ "$?" -ne 0 ]; then exit 1 fi 
+1
Nov 29 '13 at 13:48 on
source share

TextFinder is only good if the job status has not been changed from SUCCESS to FAILED or ABORTED. For such cases, use the groovy script step in the PostBuild step:

 errpattern = ~/TEXT-TO-LOOK-FOR-IN-JENKINS-BUILD-OUTPUT.*/; manager.build.logFile.eachLine{ line -> errmatcher=errpattern.matcher(line) if (errmatcher.find()) { manager.build.@result = hudson.model.Result.NEW-STATUS-TO-SET } } 

More details in the post I wrote about this: http://www.tikalk.com/devops/JenkinsJobStatusChange/

0
Mar 03 '16 at 6:50
source share

You should use the Jenkinsfile to package the assembly script and just mark the current assembly as UNSTABLE with currentBuild.result = "UNSTABLE" .

    stage {
       status = / * your build command goes here * /
       if (status === "MARK-AS-UNSTABLE") {
         currentBuild.result = "UNSTABLE"
       }
    }
0
May 10 '17 at 1:47 pm
source share

Duplicating my answer from here , because I spent some time on this:

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 this on an advanced tab:

Pipeline Syntax Example

0
Nov 07 '17 at 12:36 on
source share

I thought I would send another answer to people who might be looking for something like that.

In our construction problem, we have cases when we want the assembly to continue, but be marked as unstable. For us, this applies to version numbers.

So, I wanted to set the condition for the assembly and establish that the assembly is unstable if this condition is met.

As a build step, I used the conditional step option (single) .

Then I used the Run Groovy script system as a build step that will be executed when this condition is met.

I used the Groovy command and installed the script as follows

 import hudson.model.* def build = Thread.currentThread().executable build.@result = hudson.model.Result.UNSTABLE return 

This seems to work quite well.

I came across a solution here

http://tech.akom.net/archives/112-Marking-Jenkins-build-UNSTABLE-from-environment-inject-groovy-script.html

0
Dec 05 '17 at 15:00
source share

You can just call "exit 1" and the build will fail and will not continue. I finished creating an end-to-end function to execute it for me, and call safemake instead of make for building:

 function safemake { make "$@" if [ "$?" -ne 0 ]; then echo "ERROR: BUILD FAILED" exit 1 else echo "BUILD SUCCEEDED" fi } 
-3
Nov 07
source share



All Articles