Sponsor: Using RabbitMQ or Azure Service Bus in your .NET systems? Well, you could just use their SDKs and roll your own serialization, routing, outbox, retries, and telemetry. I mean, seriously, how hard could it be?

Build as Code
Removing the build steps out of your build system and turning into code has two major benefits to me:- Build Anywhere: Once you have a build script, you should be able to execute and build it anywhere. No longer are you relying on the build server and it’s build steps which are specific to that build system.
- Source Control: Since you build is now in code, can can live right along side your application in source control. This means that if you have the source, you have the build script.
Cake?
Cake (C# Make) is a build automation tool for .NET. It allows you to write C# to run various Tasks that you would likely find as build steps on your build server. Such as compile code (msbuild), run unit tests (xunit, nunit, mstests), creating nuget packages, create deployments via Octopus Deploy and a pile more.Getting Started
It’s really easy to get started using Cake. I’ve created a demo which is available on GitHub. It contains a basic Hello World application along side the Cake script described in the rest of this blog.Video
If you prefer watching a video tutorial, I’ve added a video that outlines the same content as this blog.Build Script
First thing you need to do is get the Cake build script that is written in PowerShell. From your project folder, run the following command to download the latest PowerShell Ssript (build.ps1)Invoke-WebRequest http://cakebuild.net/download/bootstrapper/windows -OutFile build.ps1
Cake Script
Next we need to create our Cake script. This script will define all the build tasks that the PowerShell Build Script (build.ps1) will execute. Create an empty file “build.cake” in the same directory along side the build.ps1. I recommend using VS Code along with the Cake extension to create and edit your build.cake file.Build
In this my demo application, I have a CakeDemo.sln that I’m going to build. it contains two projects, CakeDemo and CakeDemo.Tests which has an xUnit test.


Running Tests
There are many different types of tasks you can define in your cake script. Take a look at all the built-in API’s. A common task you may perform is running Unit Tests. In my Demo I have a project with an xUnit test. We can run the xunit console test running from our cake script as well. The fist thing we do is tell Cake we need an external tool from NuGet. In our case we need the xunit console runner. At the top of our build.cake script, add#tool "nuget:?package=xunit.runner.console"Now we can add a task to run our xunit tests. I’m going to modify our script a bit further in order to separate our compiling using msbuild and running our xunit tests into separate tasks. You can tell Cake which tasks are depend on on others. In this example, our xUnit task is going to be dependent on a build. Let’s execute our build script again and see the output of our build and the xUnit console runner.

Integration
Now that we have our build in source code instead it being defined as build steps on our build server, we can simply invoke our build.ps1 from our build server as a single step.More
There are a ton of built-in tasks and the Cake documentation is really good. Hopefully you can see the benefits of having a build script in code which can be version controlled and run anywhere. Let me know if you have any comments related to Cake or any other build automation tooling either in comments or on twitter.Source Code
