David Betteridge
Theme

We have been successfully using TeamCity to build one of our main products for the last year. Although we were fairly happy with the process there was a few concerns around the solution.

  1. The steps required to create a build are spread out in TeamCity as a series of settings, batch files and shell (dos) commands. This meant that before making large configuration changes we had to backup TeamCity and then hope for the best. This meant that we tended to avoid making major improvements to the process for fear of breaking something.

  2. We have three branches (Release, Features and Patches) which inherit from a set of common templates. This works fine for day-to-day operation, but isn't ideal for when we need to make a change to just one branch (normally features). After making and testing the change we have to remember to also make the same change to the common templates when features is merged into release. This can be a couple of months after making the initial change.

  3. Due to the number of steps in the build process (and there inter-dependencies) is it not possible to test the build configuration locally.

Cake

Earlier in the year I went to an excellent talk at DotNetYork on using Cake to author your builds. After a bit of research and a couple of conversations with @kev_bite it became clear that we could also use it to improve our build process.

I won't go into too much detail here as there is a getting started guide on [https://cakebuild.net/] but in summary you define the steps of your build in what's called a cake file. (This is really a C# with a lot of build related helper methods). If there isn't a helper method to do what you need then there is normally an open-source add-in available. Failing that, you can resort to writing C# in the cake script file.

To run the build you first run the build.ps1 powershell script, this downloads everything you need (mainly cake.exe). Cake.exe then runs and processes your cake script.

The only things you need to check into source control are the build.ps1 script and your cake scripts.

Team City with Cake

Cake can be used with a number of different build servers, but here are a few tips if you wish to use it with TeamCity.

  1. Using the powershell runner to launch the build.ps1 script. However if you run it as a file then TeamCity won't spot a failure if the cake script fails. Instead you should define the following script in TeamCity

./build.ps1 exit $LASTEXITCODE

  1. You can pass parameters to the cake script either by using arguments to build.ps1 or by defining environment variables in TeamCity. I would strongly recommend defining environment variables for a couple of reasons. Firstly by defining them at a higher level in TeamCity they can be shared across multiple build configurations. Secondly, cake.exe is a bit fussy when it comes to being passed arguments and also being told that the cake script isn't called build.cake.

  2. The final task in your cake script should collect all the artifacts you wish to store into a single folder (I call ours Artifacts). The artifact path in TeamCity then just has to be "Artifacts"

Happy Building