3

I use CAKE 0.22.0 and TeamCity 9.x.

In TeamCity, I have only one build step invoking build.ps1. In turn, it runs build.cake.

In build.ps1, I have added the following:

trap
{
    write-output $_
    ##teamcity[buildStatus status='FAILURE' ]
    exit 1
}

The reason is to circumvent a known PowerShell bug (i.e., executing a script with -file returns an exit code of 0 when it shouldn't).

When the exception is thrown within build.ps1, TeamCity correctly displays the build failure:

enter image description here

However, if an error occurs within build.cake, TeamCity incorrectly claims that everything ran successfully:

enter image description here

Here is the corresponding build log for the "successful" build referenced by the screenshot above:

enter image description here

As you can see, the error was thrown within a build.cake task. This error was not captured by the trap clause in build.ps1, and so TeamCity was not informed of the build failure.

I thought about adding an OnError clause to all my tasks in build.cake (the clause will contain something similar to Information(@"Some error message\n##teamcity[buildStatus status='FAILURE']]")), but this will lead to a ghastly amount of duplicate code.

Is there a succinct way to ensure that any errors thrown within the tasks in build.cake are caught in the trap clause in build.ps1?

M.Y. Babt
  • 2,733
  • 7
  • 27
  • 49
  • 1
    I think you might be barking up the wrong tree. The `trap` fix will only help you if your Powershell script is throwing an exception. When using cake, the PS script is just calling out the the cake executable and returning the exit code Cake returns. Cake will handle any thrown exceptions from tasks. In fact your build log says `Process exited with code 1`, which to me says the PS script is returning the correct code, and perhaps your build step is not using the exit code to indicate an error. I'd check your TeamCity build step. – heavyd Oct 02 '17 at 14:31
  • @heavyd Thank you for your reply! In my TeamCity build step, the parameter ``Format stderr output as`` is set to ``Warning``. Could this explain why TeamCity displays the build as successful? – M.Y. Babt Oct 02 '17 at 14:40
  • 1
    Sorry, I was mistaken in my original comment, the settings are not on the individual build steps, but in the "Failure Conditions" section of the Build Configuration. [See docs here](https://confluence.jetbrains.com/display/TCD10/Build+Failure+Conditions). Make sure the `the build process exit code is not zero` is checked. – heavyd Oct 02 '17 at 14:50
  • @heavyd Thank you for your suggestion. I have checked my failure conditions and ``the build process exit code is not zero`` is indeed checked, so the issue lies elsewhere... – M.Y. Babt Oct 03 '17 at 07:02

1 Answers1

4

Cake itself builds on TeamCity with these build step settings and build fails when Cake scripts reports an build failure.

Runner type: PowerShell

Step name: Build Cake

Execute step: If all previous steps finished successfully

PowerShell version: 3.0

Platform: Auto

Edition: Desktop

Format stderr output as: warning

Working directory:

Script: Source code

Script source:

.\build.ps1
exit $LASTEXITCODE

Script execution mode: Execute .ps1 from external file

Screenshot below of our settings

enter image description here

devlead
  • 4,935
  • 15
  • 36
  • Thank you for your reply! I have adopted your suggestion and I will report back the next time a build fails -- if the failure is correctly displayed then I will accept your answer. :-) – M.Y. Babt Oct 03 '17 at 07:07
  • 1
    Well go ahead an break that build ;) – devlead Oct 03 '17 at 11:50
  • Hello, unfortunately, TeamCity continues to claim that failed builds are successful. However, after changing *Format stderr output as* from "warning" to "error", I managed to achieve the result that I want. According to TeamCity's documentation (https://confluence.jetbrains.com/display/TCD9/PowerShell), **"To fail a build if 'an error message is logged by build runner' (see Build Failure Conditions), change the default setting of the Error Output selector from warning to error."** If you update your answer to align with TeamCity's documentation I will re-accept it :-) Thank you! – M.Y. Babt Oct 12 '17 at 13:57