build - 即使测试失败,Teamcity 也会运行构建步骤

我在使用 Teamcity 时遇到问题,即使之前的步骤不成功,它仍在继续运行构建步骤。

构建配置的最后一步是部署我的站点,如果我的任何测试失败,我不希望它这样做。

每个构建步骤都设置为仅在之前的所有步骤都成功时才执行。

在“构建失败条件”选项卡中,我检查了 Fail build if 下的以下选项:

-build process exit code is not zero
-at least one test failed
-an out-of-memory or crash is detected (Java only)

这不起作用 - 即使测试失败 TeamCity 部署我的网站,为什么?

我什至尝试添加一个额外的构建失败条件,它将在构建日志中查找特定文本(即“测试运行失败”。)

在概览页面中查看已完成的测试时,您可以看到针对最新版本的错误消息:

“测试运行失败。”文本出现在构建日志中

但它仍然会部署它。

有谁知道如何解决这个问题?看来问题已经运行很久了,here .

显然有一个解决方法:

So far we do not consider this feature as very important as there is an obvious workaround: the script can check the necessary condition and do not produce the artifacts as configured in TeamCity.

e.g. a script can move the artifacts from a temporary directory to the directory specified in the TeamCity as publish artifacts from just before the finish and in case the build operations were successful.

但我不清楚具体如何做到这一点,听起来也不是最好的解决方案。任何帮助表示赞赏。

编辑:我还能够解决快照依赖项的问题,我将有一个依赖于测试构建的单独“部署”构建,现在它不运行如果测试失败。

This对于设置依赖关系很有用。

最佳答案

这是 TeamCity 7.1 中的一个已知问题(参见 http://youtrack.jetbrains.com/issue/TW-17002),该问题已在 TeamCity 8.x+ 中得到修复(参见 this answer)。

TeamCity 区分失败的构建 和失败的构建步骤。虽然失败的单元测试会使整个构建失败,但不幸的是,TeamCity 仍然认为测试步骤本身是成功的,因为它没有返回非零错误代码。因此,后续步骤将继续运行。

已经提出了多种解决方法,但我发现它们要么需要非常重要的设置,要么会影响 TeamCity 的测试体验。

但是,在查看了 suggestion from @arex1337 之后,我们找到了一种让 TeamCity 做我们想做的事情的简单方法。只需在包含以下内联脚本的现有测试步骤之后添加一个额外的 Powershell 构建步骤(将 YOUR_TEAMCITY_HOSTNAME 替换为您的实际 TeamCity 主机/域):

$request = [System.Net.WebRequest]::Create("http://YOUR_TEAMCITY_HOSTNAME/guestAuth/app/rest/builds/%teamcity.build.id%")
$xml = [xml](new-object System.IO.StreamReader $request.GetResponse().GetResponseStream()).ReadToEnd()
Microsoft.PowerShell.Utility\Select-Xml $xml -XPath "/build" | % { $status = $_.Node.status }

if ($status -eq "FAILURE") {
    throw "Failing this step because the build itself is considered failed. This is our way to workaround the fact that TeamCity incorrectly considers a test step to be successful even if there are test failures. See http://youtrack.jetbrains.com/issue/TW-17002"
}

这个内联 PowerShell 脚本只是使用 TeamCity REST API 来询问构建本身是否作为一个整体被认为是失败的(变量 %teamcity.build.id%" 将是替换为执行该步骤时具有实际构建 ID 的 TeamCity。如果整个构建被视为失败(例如,由于测试失败),则此 PowerShell 脚本会引发错误,导致进程返回非零错误代码导致单个构建步骤本身被视为不成功。此时,可以阻止后续步骤运行。

请注意,此脚本使用 guestAuth,这需要启用 TeamCity guest 帐户。或者,您可以改用 httpAuth,但您需要更新脚本以包含 TeamCity 用户名和密码(例如 http://USERNAME:PASSWORD@YOUR_TEAMCITY_HOSTNAME/httpAuth/app/rest/builds/%teamcity. build.id%)。

因此,如果有任何先前的单元测试失败,则设置为执行“仅当所有先前的步骤都成功”的所有后续步骤都将被跳过。如果在 JetBrains 解决问题之前我们的任何 NUnit 测试都不成功,我们会使用它来阻止自动部署。

Thanks to @arex1337 for the idea.

https://stackoverflow.com/questions/15254581/

相关文章:

logging - 渐变颜色输出

build - 具有项目编译依赖项的 Gradle 嵌套多项目

build - 在 CentOS/RHEL 上制作和构建实用程序?

带有 CLI 的 Angular 2 - 为生产而构建

git - 为什么我的 TeamCity 构建停止工作?

go - 如何正确使用构建标签?

visual-studio - Visual Studio : debug information

groovy - Gradle:强制自定义任务始终运行(无缓存)

java - 在java中构建URL

c++ - 在 Sublime Text 中构建系统