执行Google Test后,Jenkins Build脚本退出 [英] Jenkins Build Script exits after Google Test execution

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

问题描述

我正在通过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 Test输出,但第三个构建任务甚至没有运行.我以为可能是某些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

推荐答案

通常,如果一个 Build Step 失败,其余的将不会执行.

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

请注意您日志中的这一行:

Pay attention to this line from your log:

[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)设置为失败.当构建步骤"失败时,詹金斯将停止并使整个工作失败.

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

然后,我在第二行中添加了基本的错误处理. &&的意思是如果以前的cd成功则仅执行./Application.您永远不知道,也许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,所以脚本(构建步骤)的总体结果将是成功的(即0),然后将执行下一个构建步骤.

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.

顺便说一句,您还可以通过适当的错误处理将所有三个构建步骤都放到一个构建步骤中.

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.

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

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