我有一个在 TeamCity 上运行 Invoke-MsBuild 的 powershell 脚本。在构建过程中,我看到其中一个项目出现“警告:构建失败”。然而 teamcity 最后表示构建成功。如何强制 TC 退出并显示错误代码或至少报告构建失败。这是构建脚本的一部分。
Write-Host "Attempting to build $SolutionPath"
$buildSucceeded = Invoke-MsBuild -Path $SolutionPath -MsBuildParameters $MSBuildParams -BuildLogDirectoryPath "$BuildPath\Logs" -KeepBuildLogOnSuccessfulBuilds
TeamCity 不太擅长捕获和报告 Powershell 引发的异常。我所有的 Powershell 脚本都用类似的东西包装
Try {
Write-Host "Attempting to build $SolutionPath"
$buildSucceeded = Invoke-MsBuild -Path $SolutionPath -MsBuildParameters $MSBuildParams -BuildLogDirectoryPath "$BuildPath\Logs" -KeepBuildLogOnSuccessfulBuilds
}
Catch [Exception] {
Write-Error ('Failed: ' + $_.Exception.Message)
Exit 1
}
通过退出非零来弥补这个问题。
我承认这相当麻烦,而且有些不尽如人意。我怀疑使用一般性可能会有所收获-ErrorAction https://blogs.technet.microsoft.com/heyscriptingguy/2014/07/09/handling-errors-the-powershell-way/大多数(所有?)Powershell cmdlet 支持的参数。 (话虽如此,我不确定Invoke-MsBuild -ErrorAction Stop
works.)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)