运行 shell 脚本时如何在 Jenkins 中标记构建不稳定
在我正在进行的一个项目中,我们使用 shell 脚本来执行不同的任务。有些是运行 rsync 的 sh/bash 脚本,有些是 PHP 脚本。 PHP 脚本之一正在运行一些集成测试,这些测试输出到 JUnit XML、代码覆盖率报告等。
Jenkins 能够根据退出状态将作业标记为成功/失败。在 PHP 中,如果脚本在运行期间检测到测试失败,则会以 1 退出。其他 shell 脚本运行命令并使用这些命令的退出代码将构建标记为失败。
// :: End of PHP script:
// If any tests have failed, fail the build
if ($build_error) exit(1);
在 Jenkins 术语中,不稳定的构建定义为:
如果构建成功,并且一个或多个发布者报告它不稳定,则该构建不稳定。例如,如果配置了 JUnit 发布者并且测试失败,则构建将被标记为不稳定。
在运行 shell 脚本时,如何让 Jenkins 将构建标记为不稳定而不是仅成功/失败?
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 scripts is running some integration tests that output to JUnit XML, code coverage reports, and similar.
Jenkins is able to mark the jobs as successful / failed based on exit status. In PHP, the script exits with 1 if it has detected that the tests failed during the run. The other shell scripts run commands and use the exit codes from those to mark a build as failed.
// :: End of PHP script:
// If any tests have failed, fail the build
if ($build_error) exit(1);
In Jenkins Terminology, an unstable build is defined as:
A build is unstable if it was built successfully and one or more publishers report it unstable. For example if the JUnit publisher is configured and a test fails then the build will be marked unstable.
How can I get Jenkins to mark a build as unstable instead of only success / failed when running shell scripts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
现代 Jenkins 版本(自 2.26,2016 年 10 月起)解决了这个问题:它只是执行 shell 构建步骤的高级选项!
您可以选择并设置任意退出值;如果匹配,构建将不稳定。只需选择一个不太可能由构建中的真实进程启动的值即可。
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.
使用 Text-finder 插件。
不要以状态 1 退出(这会导致构建失败),而是执行以下操作:
在构建后操作中启用文本查找器,设置正则表达式以匹配您打印的消息 (
TESTS FAILED!
)并选中该条目下的“如果找到则不稳定”复选框。Use the Text-finder plugin.
Instead of exiting with status 1 (which would fail the build), do:
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.无需打印魔术字符串和使用 TextFinder 即可完成此操作。 这里有关它的一些信息。
基本上,您需要在 shell 脚本中使用来自 http://yourserver.com/cli 的 .jar 文件,然后您可以使用以下命令将构建标记为不稳定:
要在错误时标记构建不稳定,您可以使用:
问题是 jenkins-cli.jar 必须可以从 shell 脚本中获取。您可以将其放在易于访问的路径中,或通过作业的 shell 脚本下载:
It can be done without printing magic strings and using TextFinder. Here's some info on it.
Basically you need a .jar file from http://yourserver.com/cli available in shell scripts, then you can use the following command to mark a build unstable:
To mark build unstable on error, you can use:
The problem is that jenkins-cli.jar has to be available from shell script. You can either put it in easy-to-access path, or download in via job's shell script:
您应该使用 Jenkinsfile 来包装构建脚本,并使用
currentBuild.result = "UNSTABLE"
将当前构建标记为 UNSTABLE。You should use Jenkinsfile to wrap your build script and simply mark the current build as UNSTABLE by using
currentBuild.result = "UNSTABLE"
.您还应该能够使用 groovy 并执行 textfinder 所做的操作
使用 groovy 构建后插件将构建标记为不稳定
另请参阅 Groovy Postbuild 插件
you should also be able to use groovy and do what textfinder did
marking a build as un-stable with groovy post-build plugin
Also see Groovy Postbuild Plugin
在我的作业脚本中,我有以下语句(此作业仅在 Jenkins master 上运行):
您可以在 Jenkins wiki 上看到此内容以及有关设置构建状态的更多信息: jenkins-ci.org/display/JENKINS/Jenkins+CLI" rel="noreferrer">https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+CLI
In my job script, I have the following statements (this job only runs on the Jenkins master):
You can see this and a lot more information about setting build statuses on the Jenkins wiki: https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+CLI
配置 PHP 构建以生成 xml junit 报告
完成构建脚本,状态为 0
<前><代码>...
退出 0;
为测试报告 XML 添加构建后操作发布 JUnit 测试结果报告。当测试失败时,该插件会将稳定版本更改为不稳定版本。
<前><代码>**/build/junit.xml
添加带有控制台输出扫描和未选中选项的Jenkins Text Finder插件。该插件因致命错误而导致整个构建失败。
Configure PHP build to produce xml junit report
Finish build script with status 0
Add post-build action Publish JUnit test result report for Test report XMLs. This plugin will change Stable build to Unstable when test are failing.
Add Jenkins Text Finder plugin with console output scanning and unchecked options. This plugin fail whole build on fatal error.
从此处复制我的答案,因为我花了一些时间寻找这个:
这现在可以在较新版本的 Jenkins,您可以执行以下操作:
管道语法生成器在高级选项卡中向您显示此内容:
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:
The Pipeline Syntax generator shows you this in the advanced tab:
除了所有其他答案之外,jenkins 还允许使用
unstable()
方法(我认为这更清楚)。此方法可以与消息参数一起使用,该消息参数描述了构建不稳定的原因。
除此之外,您还可以使用 shell 脚本(bat 或 sh)的
returnStatus
来启用此功能。例如:
当然,您可以根据您的需求和退货状态制作更细粒度的东西。
此外,为了引发错误,您还可以使用
warnError()
代替unstable()
。它会指示您的构建失败而不是不稳定,但语法是相同的。In addition to all others answers, jenkins also allows the use of the
unstable()
method (which is in my opinion clearer).This method can be used with a message parameter which describe why the build is unstable.
In addition of this, you can use the
returnStatus
of your shell script (bat or sh) to enable this.For example:
Of course, you can make something with more granularity depending on your needs and the return status.
Furthermore, for raising error, you can also use
warnError()
in place ofunstable()
. It will indicate your build as failed instead of unstable, but the syntax is same.我发现最灵活的方法是读取 groovy 后期构建插件中的文件。
如果文件内容为“true”,则构建将设置为不稳定。这将适用于本地主服务器和运行作业的任何从服务器,以及可以写入磁盘的任何类型的脚本。
I find the most flexible way to do this is by reading a file in the groovy post build plugin.
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.
我想我会为可能正在寻找类似内容的人发布另一个答案。
在我们的构建工作中,我们会遇到这样的情况:我们希望构建继续,但被标记为不稳定。对于我们来说,它与版本号有关。
因此,我想在构建上设置一个条件,如果满足该条件,则将构建设置为不稳定。
我使用条件步骤(单个)选项作为构建步骤。
然后,我使用执行系统 Groovy 脚本作为构建步骤,该步骤将在满足该条件时运行。
我使用 Groovy Command 并将脚本设置为以下,
这似乎工作得很好。
我在这里偶然发现了解决方案
http://tech.akom.net/archives/112-Marking-Jenkins-build-UNSTABLE-from-environment-inject-groovy-script.html
I thought I would post another answer for people that might be looking for something similar.
In our build job we have cases where we would want the build to continue, but be marked as unstable. For ours it's relating to version numbers.
So, I wanted to set a condition on the build and set the build to unstable if that condition is met.
I used the Conditional step (single) option as a build step.
Then I used Execute system Groovy script as the build step that would run when that condition is met.
I used Groovy Command and set the script the following
That seems to work quite well.
I stumbled upon the solution here
http://tech.akom.net/archives/112-Marking-Jenkins-build-UNSTABLE-from-environment-inject-groovy-script.html
仅当作业状态未从成功更改为失败或中止时,TextFinder 才有效。
对于这种情况,请在 PostBuild 步骤中使用 groovy 脚本:
请参阅我写的有关它的帖子中的更多详细信息:
http://www.tikalk.com/devops/JenkinsJobStatusChange/
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:
See more details in a post I've wrote about it:
http://www.tikalk.com/devops/JenkinsJobStatusChange/
作为现有答案的更轻量级替代方案,您可以使用简单的 HTTP POST 来访问 Groovy 脚本控制台 REST API 来设置构建结果:
优点:
对于此解决方案,您的环境必须满足以下条件:
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:
Advantages:
For this solution, your environment must meet these conditions:
如果您想使用声明性方法,我建议您使用这样的代码。
如果您想将所有内容保留在一个阶段中,请使用
catchError
If you want to use a declarative approach I suggest you to use code like this.
In case you want to keep everything in one stage use
catchError
将构建设置为不稳定的一种简单方法是在“execute shell”块中运行
exit 13
One easy way to set a build as unstable, is in your "execute shell" block, run
exit 13
您可以只调用“exit 1”,构建将在此时失败并且不会继续。我最终创建了一个直通 make 函数来为我处理它,并调用 safemake 而不是 make 进行构建:
You can just call "exit 1", and the build will fail at that point and not continue. I wound up making a passthrough make function to handle it for me, and call safemake instead of make for building: