bash 通过脚本返回代码失败/不稳定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25442343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:38:42  来源:igfitidea点击:

Failure / Unstable via Script Return Code

bashjenkins

提问by khmarbaise

I would like to get the result of my jenkins build job either failed (red), unstable (yellow) or successfull (green)...

我想得到我的 jenkins 构建工作的结果,要么失败(红色),要么不稳定(黄色),要么成功(绿色)...

If i return non zero from my shell script i can only get failed. Is there any return code which produces unstable ?

如果我从我的 shell 脚本返回非零,我只会失败。是否有任何返回码会产生不稳定?

回答by Slav

Update:Newer versions of Jenkins support exit codes that would set unstable. For more info, please refer here(thanks @Pedro and @gib)

更新:较新版本的 Jenkins 支持将设置unstable. 有关更多信息,请参阅此处(感谢 @Pedro 和 @gib)

I did not test this.

我没有测试这个。

Original answer below:

原答案如下:

No. A script exit code will not produce UNSTABLEbuild result in Jenkins.

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

The only way to set UNSTABLEbuild result is programmatically through Jenkins.

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

This is what the test harnessportion of Jenkins does.

这就是Jenkins的测试工具部分所做的。

You can also use the Jenkins CLIdirectly from your script to set result of the currently executing build. Instead of returning a specific exit code, have your script execute the Jenkins CLI command. For details, on your own server, goto http://<your-server>/cli/command/set-build-result

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

There are also a number of plugins that can do that same, including:

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

回答by gib

This is now possible in newer versions of Jenkins, you can do something like this:

这现在可以在较新版本的 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}")
    }
  }
}

The Pipeline Syntax generator shows you this in the advanced tab:

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

Pipeline Syntax Example

管道语法示例

回答by Pedro

As indicated in one of the comments this is and issue reported by jenkins team, already fixed and initially planed for version 2.26. See the following issue for details Permit "Execute shell" jobs to return 2 for "unstable"

正如其中一条评论所示,这是 jenkins 团队报告的问题,已经修复并最初计划用于 2.26 版。有关详细信息,请参阅以下问题Permit "Execute shell" jobs to return 2 for "unstable"

But, it seems to be dependent on another issuethat it is still blocking it

但是,这似乎取决于它仍在阻止它的另一个问题

回答by smendola

As others have posted, there is no return [exit] code that signals "unstable", but there are a number of approaches you can take. To save you the trouble of reading the Jenkins docs and experimenting, as I did, here's one way to do it, which has worked for me.

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

It's based on the "CLI" that Jenkins provides. The CLI is implemented as a standalone JAR that can be downloaded from Jenkins itself. In the following sample, we download that JAR (if not already downloaded), and invoke it with the command "set-build-result" and argument "unstable".

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

Your script should then exit with a zero status, or else the build will be FAILED.

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

The console log will show the build becoming "unstable" when the script build step finishes; notimmediately upon execution of that CLI command.

当脚本构建步骤完成时,控制台日志将显示构建变得“不稳定”;不会立即执行该 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

回答by fantastory

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

回答by Yeow_Meng

Simple way in a pipeline:

管道中的简单方法:

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