【问题标题】:Failure / Unstable via Script Return Code通过脚本返回码失败/不稳定
【发布时间】:2014-10-16 00:45:57
【问题描述】:

我想获得我的 jenkins 构建作业失败(红色)、不稳定(黄色)或成功(绿色)的结果...

如果我从我的 shell 脚本返回非零,我只能失败。有没有产生不稳定的返回码?

【问题讨论】:

标签: bash jenkins


【解决方案1】:

更新: 较新版本的 Jenkins 支持将设置为 unstable 的退出代码。更多信息,请参考here(感谢@Pedro 和@gib)

我没有测试这个。

原答案如下:

没有。脚本退出代码不会在 Jenkins 中生成 UNSTABLE 构建结果。

设置UNSTABLE 构建结果的唯一方法是通过 Jenkins 以编程方式。

这就是 Jenkins 的 test harness 部分所做的。

您也可以直接从您的脚本中使用Jenkins CLI 来设置当前执行构建的结果。不要返回特定的退出代码,而是让您的脚本执行 Jenkins CLI 命令。详情请在自己的服务器上转至http://<your-server>/cli/command/set-build-result

还有许多插件可以做到这一点,包括:

【讨论】:

【解决方案2】:

这现在可以在较新版本的 Jenkins 中实现,您可以执行以下操作:

#!/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}")
    }
  }
}

管道语法生成器在高级选项卡中显示:

【讨论】:

    【解决方案3】:

    正如其中一个 cmets 所示,这是 jenkins 团队报告的问题,已经修复并最初计划用于 2.26 版。详情见下期Permit "Execute shell" jobs to return 2 for "unstable"

    但是,看来还是要靠another issue才屏蔽它

    【讨论】:

    • 鼓励链接到外部资源,但请在链接周围添加上下文,以便您的其他用户了解它是什么以及为什么存在。始终引用重要链接中最相关的部分,以防目标站点无法访问或永久离线。
    • 看起来这已被还原(或从未登陆),我无法让它在 Jenkins 2.73.2 中工作。
    • 在这里工作,只需打开执行shell的高级选项并设置您想要的不稳定退出代码
    【解决方案4】:
    Any Build Step Plugin
    Fail The Build Plugin
    
    first build step: Execute command
      Command: echo build_step
    second build step: conditional step
      Run: Not
        !:Execute command
          Command: echo test_step
      Builder: Set the build result
        Result: Unstable
    
    change "echo test_step" to "echo_test_step" to see how it works
    
    if BUILD fails the result is FAILED, and TEST is not runned
    if BUILD succedes, TEST runs, and if fails the result is UNSTABLE
    if BUILD succedes, and TEST succedes, the result is SUCCESS
    

    【讨论】:

      【解决方案5】:

      正如其他人所发布的,没有表示“不稳定”的返回 [退出] 代码,但您可以采取多种方法。为了省去您阅读 Jenkins 文档和进行实验的麻烦,就像我所做的那样,这里有一种方法,它对我很有效。

      它基于 Jenkins 提供的“CLI”。 CLI 是作为一个独立的 JAR 实现的,可以从 Jenkins 本身下载。在以下示例中,我们下载该 JAR(如果尚未下载),并使用命令“set-build-result”和参数“unstable”调用它。

      然后您的脚本应该以零状态退出,否则构建将失败。

      当脚本构建步骤完成时,控制台日志将显示构建变得“不稳定”; 不是在执行该 CLI 命令后立即。

      unstable() {
        test -f jenkins-cli.jar || wget -q ${JENKINS_URL}/jnlpJars/jenkins-cli.jar
        java -jar jenkins-cli.jar set-build-result unstable
      }
      
      do-something-that-might-go-badly || unstable
      exit 0
      

      【讨论】:

      • 感谢您提供此代码 sn-p,它可能会提供一些即时帮助。一个正确的解释would greatly improve 其教育价值通过展示为什么这是一个很好的解决问题的方法,并将使它对未来有类似但不相同的问题的读者更有用。请edit您的答案添加解释,并说明适用的限制和假设。
      【解决方案6】:

      管道中的简单方法:

      try {
          sh "<Whatever your command mayeth be>"
      } catch (Exception e) {
          // Caught a non-zero exit code
          // Mark build as unstable.
          currentBuild.result='UNSTABLE'
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-06
        • 2019-08-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多