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