27

我们正在使用 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 会报告我的构建失败。

4

1 回答 1

21

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

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

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

在哪里:

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

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

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

2013 年 8 月 30 日更新:

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

##teamcity[buildProblem description='<description>' identity='<identity>']
于 2010-09-09T06:14:21.947 回答