问题
I have a powershell script that runs Invoke-MsBuild on TeamCity. During a build I saw a "WARNING: FAILED to build" one of the projects. However teamcity at the end said the build was successful. How can I force TC to exit with an error code or at least report the build failed. Here is part of the build script.
Write-Host "Attempting to build $SolutionPath"
$buildSucceeded = Invoke-MsBuild -Path $SolutionPath -MsBuildParameters $MSBuildParams -BuildLogDirectoryPath "$BuildPath\Logs" -KeepBuildLogOnSuccessfulBuilds
回答1:
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.)
回答2:
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.
来源:https://stackoverflow.com/questions/47164778/team-city-build-fail-not-returning-fail-code