Error in CAKE task not displayed in TeamCity overview

Viewed 755

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?

1 Answers

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

Related