Reputation: 31750
We are using TeamCity Enterprise 8.0.5.
I have a TeamCity build step which runs a PowerShell (.ps1) script, which looks like this:
try
{
# Break something
$a = 1 / 0
}
catch
{
Exit 1
}
Despite this, in the build log, the step succeeds and exits with code 0.
[10:02:18][Step 2/3] Process exited with code 0
I want the step to fail if there are any failures in the script. How can I make this happen?
Upvotes: 9
Views: 5237
Reputation: 22476
For people still finding this now, years later, what you should do is output a Build Problem service message. This will tell TeamCity to fail the build:
Write-Host "##teamcity[buildProblem description='<description>']"
You can use Write-Output if your script is not part of a PowerShell pipeline. Write-Host is guaranteed to write to the "console" that TeamCity reads.
If you do this before your exit statement, your script will remain compatible with command-line usage. You may also check for TeamCity first by using if($null -ne $Env:TEAMCITY_VERSION)
if you want to suppress these messages when running on the command line.
See the full documentation on service messages for more details.
Be sure to escape the description text such that the characters ' | [ ] are prefixed with the | (pipe) character, and \r \n are changed to |r |n. See escaping documentation
It is also possible to use Write-Error as is shown in another answer here to send text to STDERR and let the TeamCity build configuration's Failure Conditions optionally detect those messages as failures. This might produce erroneous failures depending on what other uses you have for STDERR - for example, certain programs are known to output informational messages there.
Upvotes: 2
Reputation: 142
Suppose you are using psake and your goal is to fail your build if your build script fails. The script which imports the psake module and invokes the build script does not fail, so TeamCity takes it as a successful build.
Add this code into your first script to fail your build if the second script fails.
Import-Module .\psake\psake.psm1
Invoke-Psake .\build-steps.ps1 @args
if($psake.build_success -eq $false){
Write-Host "There was an error running psake script"
exit 1
}
Remove-Module psake
Do not remove the module before the if
statement.
Upvotes: -4
Reputation: 31750
I have just discovered this post:
PowerShell runner - script fails but the build succeeds - 'Process exited with code 0'
There is a bug in TeamCity which means that non-zero PowerShell return codes are not picked up.
The solution suggested is to create a build failure condition on detection of certain text output into the build log.
However, my solution involved something different.
In the catch block you only have to use the Write-Error cmdlet:
catch
{
Write-Error -Exception $_.Exception
}
Then you must ensure that two things are set correctly in TeamCity:
Firstly, the build step Error Output should be set to error, and not warning:
Secondly, in the build failure conditions screen, make sure an error message is logged by build runner is checked:
Upvotes: 10