How do I get errors to propagate in the TeamCity PowerShell runner

Ruben Bartelink picture Ruben Bartelink · Jul 25, 2012 · Viewed 15.9k times · Source

I have a TeamCity 7 Build Configuration which is pretty much only an invocation of a .ps1 script using various TeamCity Parameters.

I was hoping that might be a simple matter of setting:

  • Script

    File

  • Script File

    %system.teamcity.build.workingDir%/Script.ps1

  • Script execution mode

    Execute .ps1 script with "-File" argument

  • Script arguments

    %system.teamcity.build.workingDir% -OptionB %BuildConfigArgument% %BuildConfigArg2%

And then I would expect:

  • if I mess up my arguments and the script won't start, the Build fails
  • if my Script.ps1 script throws, the Build fails
  • If the script exits with a non-0 Error Level I want the Build to Fail (maybe this is not idiomatic PS error management - should a .ps1 only report success by the absence of exceptions?)

The question: It just doesn't work. How is it supposed to work? Is there something I'm doing drastically wrong that I can fix by choosing different options?

Answer

Ruben Bartelink picture Ruben Bartelink · Jul 3, 2015

As doc'd in the friendly TeamCity manual:

Setting Error Output to Error and adding build failure condition

In case syntax errors and exceptions are present, PowerShell writes them to stderr. To make TeamCity fail the build, set Error Output option to Error and add a build failure condition that will fail the build on any error output.

The keys to making this work is to change two defaults:

  1. At the top level in the Build Failure Conditions, switch on an error message is logged by build runner:
  2. In the [PowerShell] Build Step, Show advanced options and set Error output: Error