.net core tutorials

How to write a Cake Build script for ASP.net core project

Ensuring your software product can be built and deployed with ease with a repeatable process is the consistent across a number of environment i.e. on a build server or on a developers laptop, is absolutely fundamental and critical success factor for your software development efforts.

Investing time and effort in this in the early days, pays enormous benefits in the long run. Having reaped the benefits in previous startups, while  also being able to experience the downsides of not doing so on other large enterprise projects.

In this article I will be discussing how to use Cake to develop your build scripts.

What is Cake ?

Cake (C# Make) is a cross platform build automation system with a C# DSL to do things like compiling code, copy files/folders, running unit tests, compress files and build NuGet packages.

Why Cake ?

I have to be honest, my primary reason for making use of cake is that I absolutely hate using Powershell! Personally I do not find it at all intuitive, difficult to read and understand.

Over the years I have found that I am not alone and there are very few developers who actually like using Powershell.

One of the side effects of having a quasi-racist affliction to Powershell, is that unfortunately it is used in a number of Build Scripts and Build Automation routines. Therefore I have had to try and make sense of Powershell over the years

Using Cake developers no longer need to learn Powershell to write Build Automation scripts, we can now use C# to write Build Scripts!

  • Cake speaks C#
    If you are working with developers that use C#, Cake will be easier to learn. You just write C# and it uses Roslyn to run the script.
  • Developed in C# & Open Source
    You can view the source code on GitHub
  • Addins available
    A build language must be able to connect to many different types of system, it’s important to have the ability to coordinate with different systems. Check out the Cake Addin Library

It is also possible to make use of Cake in a cross platform Continuous Integration/Deployment scenario. In this post I will detail the steps required to install and configure a Cake Build Server on Ubuntu 16.x.

Although Cake is admittedly easy to use, once you know how I did experience a bit of steep learning curve in getting started with it.  The project documentation is  pretty light, in this regard and for me at least there was a lot of feeling my round in the dark.  I thought I save others time but documenting as much as possible what I learned.

I thought the best way to do this would be to walk through the process of  creating a new project and then create a build process using cake. In this specific example I will be using ASP.net Core project in Visual Studio Code on ubuntu, however the process is fairly similar on any platform. I’ll try explain differences as much as possible where I found them.

Install Mono

Although not necessay to build ASP.net core, cake does make use of the of Mono to run two executables i.e. Nuget.exe and Cake.exe.

All code for this article is available on Github

Code on GitHub

Start new project

We’ll open Visual Studio Code in a new folder , in my instance I have called this cakedemo

Use the Cake Bootstrapper

The Build Process is as important as source control,  (you wouldn’t start writing code without at least creating a git repository would you ? 🙂 )

Therefore we’ll make a start creating our build scripts, despite having anything to build yet.  In VS Code open the command pallete  Shift + Ctrl + p  or View -> Command Pallette, and search for Cake.

Selecting the Cake Bootstrapper will then display another two options. A bootstrapper  for builds on Windows making use of PowerShell or build.ps1 ,   and one for Linux making use of BASH  or build.sh.

We may as well add both just to give us greater flexibility going forward.  We may have have some developers working on Windows machines and some on either Linux or Macs. We may also decide at a later stage we may want to change our Build Agent or build server platform.  Having both options available helps us make the switch easier.

After we finished adding both our Project Explorer view should now contain 2 files.

The two files we have just added, essentially are entry points into our Build Process.  If we open one of the files, in my case it will be the build.sh file,  ( yes I hate PowerShell that much I never want to read it!)  and quickly examine it.  I won’t go into too much detail but will hopefully touch on the areas of most interest.  The file is also commented which does help to understand it a lot quicker.


Add gitignore file

Lets go ahead and add out .gitignore  file to our project and exclude the tools folder from being commited to our repository.  The Bootstrapper will ensure that the folder and tools are download on whichever environment it executes so there is no need to store them.

Ctrl  + n  to create a new file then add your content thenShift + Ctrls and name the file .gitignore


Create build.cake file

Another job the build.sh file does is call the build.cake file, which will build your project.  This is a file that we will develop ourselves using C# that will carry out all the steps we would like to execute during our build process.

Ctrl  + n  to create a new file thenShift + Ctrls name build.cake

Once the file has been created we can now start writing all the plumbing code we will need.  The code below is purely how I like to lay this out.  This is by no means a complete file, and I am not entirely sure if this is how more seasoned cake users would lay out the file, but it works for me now, and I am absolutely positive that I will change my mind in the future, but it’s good enough to start with.


Reviewing the file you’ll notice it is fairly straight forward to grok.  It’s mostly easy to understand C#  mostly consisting cake based API calls

If you have a task that depends on the outcome of another task, you simply use the fluent syntax to .IsDependentOn


Add our project

We have a basic build process, but we haven’t got anything to build yet. So lets go ahead and create an empty project and build it.

First, lets create a Folder in root of project folder and call it src . Then open the integrated terminal window and change directory into the src folder.

learn how to set up your ubuntu workstation to include project quick start templates .

Check out Create a .net core web application on ubuntu in 5 minutes

I will generate a new Asp.net core Web API, using yo aspnet. In my case I called my project demoapi

Once the the application has been generated. We won’t do the usual tasks of running dotnet restore or dotnet build because we are going to use our build.cake file to do that for us.

We now need to just need to add some detail to our build.cake, about the project we want to build. Essentially I am just going to add values to my variables.

Once this complete, we can just change back to a project root folder where our build files are cd .. and then execute the build.sh

Provided we have provided all the correct information to the variables in our build file, we should see a successful build.

While creating this blog post, Microsoft released the Release Candidate 3 (RC3) for Dotnet Core. So I took the opportunity to learn how this affects Cake Build

This enables using a Solution File for and also we see the demise of the proj.json in favour of the return of .csproj

if you have your project generated using the old Yo Generator you can update the project using dotnet migrate after you have insttalled RC4

Instruction to upgrade to RC4


Now that we have a basic build working and a minimal structure of our build file in place, enabling us to extend our build file to include executing our unit tests and any other additional features we would like to implement.

We can now also commit our project to our version control and make use of the build file on out Continuous Integration server , like TeamCity, Travis , Jenkins etc. I will discuss how we can do this in a forthcoming blog posts.

Additional Resources & References


Gary Woodfine

Freelance Full Stack Developer at threenine.co.uk
Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.

Affiliate Disclaimer

Disclosure: Please note that some of the links included in the blog posts are affiliate links, which means I will earn a commission if you decide to make a purchase.

I only include affiliate links to products, services and companies that I have personal experience and have actually used. I only recommend them because they are helpful and useful, not because of the small commissions I make if you decide to buy something.

Please do not spend any money on these products unless you feel they are relevant, suitable and will help you achieve your goals.

Buying anyone of these products and the commisions earned will not make me an overnite multi millionaire, but they will help to cover the hosting costs and compensate for the countless hours and effort I put in to create meaningful and valuable free content I provide to help others succeed.

You've also undoubtedly noticed that I also use various advertising networks - unless of of course you're using an Ad blocker, this is also an attempt to reduce my monthly deficit I experience in providing free relevant, valuable and on occassion insightful content for the benefit of others.

I only really make some money if people click on the ads, unless of course you consider 1c per 1000 impressions real money!

Bear in mind just how many impressions I need to make before I can cover my £20 hosting costs a month!

If you are using an adblocker and guilt stricken you can of course donate using any of the buttons below, but I won't be holding my breath.

Buy me a coffeeBuy me a coffee