TeamCity 命令行构建运行程序:如何使构建失败?
我们使用 TeamCity 的命令行构建运行程序来调用 bat 文件。 bat 文件通过调用 Visual Studio 2008 的“devenv.exe”来构建我们的解决方案,然后执行单元测试并创建正确的文件夹结构。
我们想要做的是,如果对 devenv 的调用失败,则停止执行 bat 文件,并使 TeamCity 意识到构建失败。我们可以通过检查 ErrorLevel (如果构建失败,则为 1)来捕获失败的 devenv 调用,并且我们可以在此时退出 bat 文件。但是我们如何告诉 TeamCity 构建失败?
这是我们尝试过的:
call "build.bat"
IF ERRORLEVEL 1 EXIT /B 1
但 TeamCity 无法识别我们的退出代码。相反,构建日志如下所示:
[08:52:12]: ========== Build: 28 succeeded or up-to-date, 1 failed, 0 skipped ==========
[08:52:13]: C:\_work\BuildAgent\work\bcd14331c8d63b39\Build>IF ERRORLEVEL 1 EXIT /B 1
[08:52:13]: Process exited with code 0
[08:52:13]: Publishing artifacts
[08:52:13]: [Publishing artifacts] Paths to publish: [build/install, teamcity-info.xml]
[08:52:13]: [Publishing artifacts] Artifacts path build/install not found
[08:52:13]: [Publishing artifacts] Publishing files
[08:52:13]: Build finished
因此 TeamCity 将报告构建成功。我们该如何解决这个问题?
解决方案:
TeamCity 提供了一种名为 服务消息可用于处理此类情况。 我已将构建脚本更新为如下所示:
IF %ERRORLEVEL% == 0 GOTO OK
echo ##teamcity[buildStatus status='FAILURE' text='{build.status.text} in compilation']
EXIT /B 1
:OK
因此,TeamCity 将报告我的构建因“编译失败”而失败。
We're using TeamCity's command line build runner to call a bat-file. The bat-file builds our solution by calling the Visual Studio 2008's "devenv.exe" and then it executes the unit tests and creates the correct folder structure.
What we would like to do is to stop executing the bat-file if the call to devenv fails and make the TeamCity to realize that the build failed. We can catch the failed devenv call by checking the ErrorLevel (which is 1 if the build failed) and we can exit our bat-file at that point. But how can we tell to the TeamCity that the build failed?
This is what we've tried:
call "build.bat"
IF ERRORLEVEL 1 EXIT /B 1
But TeamCity doesn't recognize our exit code. Instead the build log looks like this:
[08:52:12]: ========== Build: 28 succeeded or up-to-date, 1 failed, 0 skipped ==========
[08:52:13]: C:\_work\BuildAgent\work\bcd14331c8d63b39\Build>IF ERRORLEVEL 1 EXIT /B 1
[08:52:13]: Process exited with code 0
[08:52:13]: Publishing artifacts
[08:52:13]: [Publishing artifacts] Paths to publish: [build/install, teamcity-info.xml]
[08:52:13]: [Publishing artifacts] Artifacts path build/install not found
[08:52:13]: [Publishing artifacts] Publishing files
[08:52:13]: Build finished
So TeamCity will report that the build was successful. How can we fix this?
Solution:
TeamCity provides a mechanism called Service Messages which can be used to handle situations like this.
I've updated my build script to look like the following:
IF %ERRORLEVEL% == 0 GOTO OK
echo ##teamcity[buildStatus status='FAILURE' text='{build.status.text} in compilation']
EXIT /B 1
:OK
As a result TeamCity will report my build as failed because of a "Failure in compilation".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅构建与 TeamCity 的脚本交互主题。
更新 2013-08-30:
自 TeamCity 起应使用
报告 7.1 构建失败buildProblem
服务消息改为:See Build Script Interaction with TeamCity topic.
Update 2013-08-30:
As of TeamCity 7.1 build failures should be reported using the
buildProblem
service message instead: