Jenkins 构建脚本在 Google 测试执行后退出 [英] Jenkins Build Script exits after Google Test execution

查看:60
本文介绍了Jenkins 构建脚本在 Google 测试执行后退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 Jenkins 构建 Qt GUI 应用程序.我添加了 3 个构建步骤:

I am building a Qt GUI application via Jenkins. I added 3 build steps:

  • 构建测试可执行文件
  • 运行测试可执行文件
  • 使用 gcovr 编译覆盖率报告

出于某种原因,用于运行测试可执行文件的 shell 任务在执行后停止.即使是简单的 echo 也不会运行.测试是用 Google Test 编写的,并输出 xUnit XML 文件,这些文件在构建后进行分析.一些测试会启动应用程序用户界面,所以我安装了 jenkins xvnc 插件来让它们运行.

For some reason, the shell task for running the test executable stops after execution. Even a simple echo does not run after. The tests are written with Google Test and output xUnit XML files, which are analyzed after the build. Some tests start the applications user interface, so I installed the jenkins xvnc plugin to get them to run.

构建任务如下:

构建

cd $WORKSPACE/projectfiles/QMake
sh createbin.sh

测试

cd $WORKSPACE/bin
./Application --gtest_output=xml

覆盖率报告

cd $WORKSPACE/projectfiles/QMake/out
gcovr -x -o coverage.xml

现在,第一个构建任务末尾的 echo 被正确打印,但第二个任务末尾的 echo 没有正确打印.因此,尽管 Google 测试输出可见,但第三个构建任务甚至不会运行.我认为问题可能在于某些 Google 测试失败,但是为什么脚本会因为测试失败而停止执行?

Now, an echo at the end of the first build task is correctly printed, but an echo at the end of the second is not. The third build task is therefore not even run, although the Google Test output is visible. I thought that maybe the problem is that some of the Google Tests fail, but why whould the script stop executing just because the tests fail?

也许有人可以给我一个关于为什么第二个任务停止的提示.

Maybe someone can give me a hint on why the second task stops.

编辑

控制台输出如下所示:

Updating svn://repo/ to revision '2012-11-15T06:43:15.228 -0800'
At revision 2053
no change for svn://repo/ since the previous build
Starting xvnc
[VG5] $ vncserver :10

New 'ubuntu:10 (jenkins)' desktop is ubuntu:10

Starting applications specified in /var/lib/jenkins/.vnc/xstartup
Log file is /var/lib/jenkins/.vnc/ubuntu:10.log

[VG5] $ /bin/sh -xe /tmp/hudson7777833632767565513.sh
+ cd /var/lib/jenkins/workspace/projectfiles/QMake
+ sh createbin.sh
... Compiler output ...
+ echo Build Done
Build Done
[VG5] $ /bin/sh -xe /tmp/hudson4729703161621217344.sh
+ cd /var/lib/jenkins/workspace/VG5/bin
+ ./Application --gtest_output=xml
Xlib:  extension "XInputExtension" missing on display ":10".
[==========] Running 29 tests from 8 test cases.
... Test output ...
 3 FAILED TESTS
Build step 'Execute shell' marked build as failure
Terminating xvnc.
$ vncserver -kill :10
Killing Xvnc4 process ID 1953
Recording test results
Skipping Cobertura coverage report as build was not UNSTABLE or better ...
Finished: FAILURE

推荐答案

一般来说,如果一个构建步骤失败,其余的将不会被执行.

Generally, if one Build Step fails, the rest will not be executed.

注意日志中的这一行:

[VG5] $ /bin/sh -xe

-x 使 shell 在执行前在控制台中打印每个命令.
如果任何命令失败,-e 会使 shell 退出并显示错误.

The -x makes the shell print each command in console before execution.
The -e makes the shell exit with error if any of the commands failed.

在这种情况下,失败"是任何单个命令的返回码都不是 0.
您可以通过直接在机器上运行来验证这一点:

A "fail" in this case, would be a return code of not 0 from any of the individual commands.
You can verify this by running this directly on the machine:

./Application --gtest_output=xml
echo $?

如果 echo $? 显示 0,则表示上一条命令成功完成.如果它显示其他任何内容,则表示来自上一个命令(来自 ./Application)的错误代码,Jenkins 将其视为此类.

If the echo $? displays 0, it indicates successful completion of the previous command. If it displays anything else, it indicates an error code from the previous command (from ./Application), and Jenkins treats it as such.

现在,这里有几件事情在起作用.首先是如果一个命令失败(默认行为),你的第二个构建步骤(本质上是一个临时的 shell 脚本 /tmp/hudson4729703161621217344.sh)被设置为失败.当构建步骤失败时,Jenkins 将停止并导致整个工作失败.

Now, there are several things at play here. First is that your second Build Step (essentially a temporary shell script /tmp/hudson4729703161621217344.sh) is set to fail if one command fails (the default behaviour). When the Build Step fails, Jenkins will stop and fail the whole job.

您可以通过将 set +e 添加到第二个构建步骤的顶部来修复此特定行为.这不会因为个别命令失败而导致脚本(构建步骤)失败(它会显示命令的错误,然后继续).

You can fix this particular behaviour by adding set +e to the top of your second Build Step. This will not cause the script (Build Step) to fail due to individual command failure (it will display an error for the command, and continue).

然而,脚本(构建步骤)的整体结果是最后一个命令的退出代码.由于在您的 OP 中,您的脚本中只有 2 个命令,而最后一个命令失败,这将导致整个脚本(构建步骤)被视为失败,尽管您使用了 +x我补充道.请注意,如果您添加 echo 作为第三个命令,这实际上会起作用,因为最后一个脚本命令 (echo) 成功了,但是这种解决方法"并不是您所需要的.

However, the overall result of the script (Build Step) is the exit code of the last command. Since in your OP, you only have 2 commands in the script, and the last is failing, it will cause the whole script (Build Step) to be considered a failure, despite the +x that you've added. Note that if you add an echo as the 3rd command, this would actually work, since the last script command (echo) was successful, however this "workaround" is not what you need.

您需要的是将正确的错误处理添加到您的脚本中.考虑一下:

What you need is proper error handling added to your script. Consider this:

set +e
cd $WORKSPACE/bin && ./Application --gtest_output=xml
if ! [ $? -eq 0 ]; then
    echo "Tests failed, however we are continuing"
else
    echo "All tests passed"
fi

脚本中发生了三件事:

  1. 首先,我们告诉 shell 不要在单个命令失败时退出

  1. First, we are telling shell not to exit on failure of individual commands

然后我在第二行添加了基本的错误处理.&& 的意思是执行 ./Application if-and-only-if-and-only-if the previous cd is successfully. 你永远不知道,也许bin 文件夹丢失,或者其他任何可能发生的事情.顺便说一句,&& 在内部工作在相同的错误代码等于 0 原则上

Then i've added basic error handling in the second line. The && means "execute ./Application if-and-only-if the previous cd was successful. You never know, maybe the bin folder is missing, or whatever else can happen. BTW, the && internally works on the same error code equals 0 principle

最后,现在对 ./Application 的结果有正确的错误处理.如果结果不为 0,那么我们表明它失败了,否则我们表明它已经通过了.请注意,这是因为最后一个命令不是(可能)失败的 ./Application,而是来自 if-else 可能性之一的 echo,脚本的整体结果(Build Step) 将成功(即 0),将执行下一个 Build Step.

Lastly, there is now proper error handling for the result of ./Application. If the result is not 0, then we show that it had failed, else we show that it had passed. Note, this since the last command is not a (potentially) failing ./Application, but an echo from either of if-else possibilities, the overall result of the script (Built Step) will be a success (i.e 0), and the next Build Step will be executed.

顺便说一句,您也可以通过适当的错误处理将所有 3 个构建步骤整合到一个构建步骤中.

BTW, you can as well put all 3 of your build steps into a single build step with proper error handling.

是的...这个答案可能比要求的要长一点,但我想让您了解 Jenkins 和 shell 如何处理退出代码.

Yes... this answer may be a little longer than what's required, but i wanted you to understand how Jenkins and shell treat exit codes.

这篇关于Jenkins 构建脚本在 Google 测试执行后退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆