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

发布于 2024-09-18 04:55:33 字数 1461 浏览 5 评论 0原文

我们使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦纸 2024-09-25 04:55:33

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

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

##teamcity[消息文本='<消息文本>' errorDetails='<错误详细信息>' status='<状态值>']

地点:

  • 状态属性可以采用以下值:NORMAL、WARNING、
    失败,错误。默认值为正常。
  • 错误详情
    仅当状态为 ERROR 时才使用属性,在其他情况下则使用
    被忽略。

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

##teamcity[消息文本='异常文本' errorDetails='堆栈跟踪' status='错误']

更新 2013-08-30:

自 TeamCity 起应使用 报告 7.1 构建失败buildProblem 服务消息改为:

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

See Build Script Interaction with TeamCity topic.

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']

Update 2013-08-30:

As of TeamCity 7.1 build failures should be reported using the buildProblem service message instead:

##teamcity[buildProblem description='<description>' identity='<identity>']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文