TeamCity命令行构建运行程序:如何使构建失败?

28
我们正在使用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将报告我的构建失败,原因是“编译失败”。


1
GOTO OK 去哪里了? %ERRORLEVEL% 是什么? - Tomas Jansson
1个回答

22

请参考与TeamCity交互的构建脚本主题。

您可以按以下方式报告构建日志中的消息:

##teamcity[message text='<message text>' errorDetails='<error details>' status='<status value>']

其中:

  • status属性可以采用以下值:NORMAL、WARNING、FAILURE、ERROR。默认值为NORMAL。
  • errorDetails属性仅在状态为ERROR时使用,在其他情况下将被忽略。

如果此消息的状态为ERROR,并且构建配置常规设置页面上选中了“如果构建运行器记录错误消息,则构建失败”复选框,则此消息将导致构建失败。例如:

##teamcity[message text='Exception text' errorDetails='stack trace' status='ERROR']

更新2013-08-30:

从TeamCity 7.1开始,应使用buildProblem服务消息报告构建失败:

##teamcity[buildProblem description='<description>' identity='<identity>']

谢谢!我使用buildStatus消息成功让这个东西工作了。我会用这个信息更新原贴。 - Mikael Koskinen
3
这个方法也适用于PowerShell脚本,例如 echo "##teamcity[message text='oops' errorDetails='' status='ERROR']" - Chris S

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接