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

后端 未结 14 2076
情歌与酒
情歌与酒 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 12:16

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

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

    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

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

    Duplicating my answer from here because I spent some time looking for 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 you this in the advanced tab:

    0 讨论(0)
提交回复
热议问题