我们使用 TeamCity 的命令行构建运行程序来调用 bat 文件。 bat 文件通过调用 Visual Studio 2008 的“devenv.exe”来构建我们的解决方案,然后执行单元测试并创建正确的文件夹结构。
我们想要做的是在调用 devenv 失败时停止执行 bat 文件,并使 TeamCity 意识到构建失败。我们可以通过检查 来捕获失败的 devenv 调用。错误级别 (如果构建失败,则为 1),此时我们可以退出我们的 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 提供了一种名为 Service Messages 的机制。可用于处理此类情况。
我已经更新了我的构建脚本,如下所示:
IF %ERRORLEVEL% == 0 GOTO OK
echo ##teamcity[buildStatus status='FAILURE' text='{build.status.text} in compilation']
EXIT /B 1
:OK
因此,由于“编译失败”,TeamCity 将报告我的构建失败。
最佳答案
见 Build Script Interaction with TeamCity话题。
You can report messages for build log in the following way:
##teamcity[message text='<message text>' errorDetails='<error details>' status='<status value>']
where:
- The status attribute may take following values: NORMAL, WARNING, FAILURE, ERROR. The default value is NORMAL.
- The errorDetails attribute is used only if status is ERROR, in other cases it is ignored.
This message fails the build in case its status is ERROR and "Fail build if an error message is logged by build runner" checkbox is checked on build configuration general settings page. For example:
##teamcity[message text='Exception text' errorDetails='stack trace' status='ERROR']
2013-08-30 更新:
从 TeamCity 7.1 开始,应使用
buildProblem
报告构建失败。代替服务消息:##teamcity[buildProblem description='<description>' identity='<identity>']
关于visual-studio-2008 - TeamCity 命令行构建运行程序 : How to make the build fail?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3674163/