Team City build fail not returning fail code

断了今生、忘了曾经 提交于 2019-12-06 10:47:51

TeamCity isn't very good at catching and reporting exceptions thrown from Powershell. All my Powershell scripts are wrapped with something like

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
}

that makes up for the problem by exiting non-zero.

I admit this is quite cumbersome and somewhat unsatisfactory. I suspect something may be gained by using the general -ErrorAction parameter that most (all?) Powershell cmdlets support. (Having said that, I'm not sure Invoke-MsBuild -ErrorAction Stop works.)

You can add $ErrorActionPreference = "Stop" at the beginning of your powershell script, this should stop script execution on error and exit with non zero code.

The default setting of $ErrorActionPreference is Continue, which is possibly why you are seeing that behaviour. You can read more here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!