How to mark a build unstable in Jenkins when running shell scripts

后端 未结 14 2072
情歌与酒
情歌与酒 2020-11-27 11:31

In a project I\'m working on, we are using shell scripts to execute different tasks. Some are sh/bash scripts that run rsync, and some are PHP scripts. One of the PHP script

相关标签:
14条回答
  • 2020-11-27 11:54

    The TextFinder is good only if the job status hasn't been changed from SUCCESS to FAILED or ABORTED. For such cases, use a groovy script 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
        }
     }
    

    See more details in a post I've wrote about it: http://www.tikalk.com/devops/JenkinsJobStatusChange/

    0 讨论(0)
  • 2020-11-27 11:56

    You should use Jenkinsfile to wrap your build script and simply mark the current build as UNSTABLE by using currentBuild.result = "UNSTABLE".

       stage {
          status = /* your build command goes here */
          if (status === "MARK-AS-UNSTABLE") {
            currentBuild.result = "UNSTABLE"
          }
       }
    
    0 讨论(0)
  • 2020-11-27 12:01

    Modern Jenkins versions (since 2.26, October 2016) solved this: it's just an advanced option for the Execute shell build step!

    You can just choose and set an arbitrary exit value; if it matches, the build will be unstable. Just pick a value which is unlikely to be launched by a real process in your build.

    0 讨论(0)
  • 2020-11-27 12:06

    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.

    0 讨论(0)
  • 2020-11-27 12:06

    I find the most flexible way to do this is by reading a file in the groovy post build plugin.

    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 file contents are 'true' the build will be set to unstable. This will work on the local master and on any slaves you run the job on, and for any kind of scripts that can write to disk.

    0 讨论(0)
  • 2020-11-27 12:06

    As a lighter alternative to the existing answers, you can set the build result with a simple HTTP POST to access the Groovy script console REST API:

        curl -X POST \
         --silent \
         --user "$YOUR_CREDENTIALS" \
         --data-urlencode "script=Jenkins.instance.getItemByFullName( '$JOB_NAME' ).getBuildByNumber( $BUILD_NUMBER ).setResult( hudson.model.Result.UNSTABLE )" $JENKINS_URL/scriptText
    

    Advantages:

    • no need to download and run a huge jar file
    • no kludges for setting and reading some global state (console text, files in workspace)
    • no plugins required (besides Groovy)
    • no need to configure an extra build step that is superfluous in the PASSED or FAILURE cases.

    For this solution, your environment must meet these conditions:

    • Jenkins REST API can be accessed from slave
    • Slave must have access to credentials that allows to access the Jenkins Groovy script REST API.
    0 讨论(0)
提交回复
热议问题