Psake is a powerful build system, build on Powershell for a lot of functionality and familiarity. And TeamCity is a really nice continuous integration server, checkout the CodeBetter TeamCity install for a good demo. Hooking the two up was not a easy as using just Msbuild or Nant, but it’s not really too bad.

First, your Psake script needs to check the exit codes of the applications it calls. This is important to do regardless because Powershell doesn’t error out based on the exit code. The trunk version of Psake has a convenience function to execute a command and check it’s exit code is greater than 0, called exec:

You can also pass a message as the second argument.

With this checking in place, next is a bat script to use from the TeamCity command line build option.

@echo off
powershell -NoProfile -ExecutionPolicy unrestricted -Command "& {Import-Module ‘.\tools\psake\psake.psm1’; invoke-psake -t %1; if ($Error -ne ‘’) {write-host "ERROR: $error" -fore RED; exit $error.Count} }"`

This executes a Powershell prompt which imports the Psake module and then calls the provided Psake task. Finally, it checks the $Error variable and sets the exit code to the number of errors returned by the script. This will fail the TeamCity build and allows any error thrown by Powershell to break the build.

Last is to have TeamCity run the bat file with the desired target.

psake.bat Compile

One thing to note is that some things return exit code other then 0 but that are not errors. In that case you can check the Powershell variable $LastExitCode and throw errors based on that.

Thanks to the psake users group for all their help!