Visual Studio allows you to develop, test, and compile .NET Framework applications inside a single IDE.
However, if you want to set up automated builds for a Continuous Integration pipeline you’ll need to learn how to do so via the command line.
In this post, we’ll set up a build server to compile .NET Framework applications using Cake.
When is this useful?
Creating a continuous integration pipeline will allow you to automatically:
- Compile your application
- Run tests
- Create binaries / installers
- Deploy your application
In order to do any of that, you’ll need to have a way for a server to build your application via the command line.
Is this just for Windows?
If you’re writing a .NET Core application and want to build on Linux, the process is a little different. For simplicity’s sake, we’ll only cover Windows in this post.
- Windows Server 2012 R2 or higher
Installing Build Tools
- Download Build Tools for Visual Studio 2017
- Click continue
- In the Workloads section, choose the types of applications you will be building. In my case, I’ve chosen Web Development Build Tools and .NET Core Build Tools but you can check the ones relevant to your application (Which might be none of them).
- In the Individual Components section, choose the targeting pack and SDK for any of the .NET Framework versions your applications use.
- Make sure that NuGet targets and build tasks is checked
- Click install
- Go take a break! This will take a little while… maybe 5-10 minutes.
- Click restart when it’s all done
Using Cake to build an application
Download the sample project zip and extract it.
Here are the two files that are used to perform the build:
- Installs cake if it is not found on your computer
- C# script that tells cake how to build your application
In the build.cake file, there is a collection of Tasks that are executed in order by using the IsDependentOn() call.
Running the build script
- Open a powershell terminal
- Navigate to the folder you extracted the sample project zip to
You might get an error like this:
PS C:\Users\Administrator\Downloads\example-master> .\build.ps1 .\build.ps1 : File C:\Users\Administrator\Downloads\example-master\build.ps1 cannot be loaded. The file C:\Users\Administrator\Downloads\example-master\build.ps1 is not digitally signed. You cannot run this script on the current system. For more information about running scripts and setting execution policy, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:1 + .\build.ps1 + ~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) , PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess
This is typically because Windows realizes you downloaded the file from the internet and blocks it. You can get around this by telling Windows that the files did not come from the internet using a powershell command.
If you received the error, follow these steps:
Get-ChildItem .\ | Unblock-File
- Close the Powershell Window
- Reopen the Powershell Window and navigate to the folder
Test Run Summary Overall result: Passed Test Count: 2, Passed: 2, Failed: 0, Inconclusive: 0, Skipped: 0 Start time: 2018-04-22 23:57:57Z End time: 2018-04-22 23:57:57Z Duration: 0.113 seconds ======================================== Default ======================================== Task Duration -------------------------------------------------- Clean 00:00:00.0121812 Restore-NuGet-Packages 00:00:02.5454776 Build 00:00:02.8819069 Run-Unit-Tests 00:00:01.0125675 -------------------------------------------------- Total: 00:00:06.4554858
If you saw the above result, congratulations! You compiled a C# application and ran a set of unit tests via the command line without installing Visual Studio.
Try it with your own solution
You can take the build.cake file and build.ps1 file and copy it into your own .NET application’s folder.
Then you can edit these lines in the build.ps1 file to point to your own project:
var buildDir = Directory("./src/Example/bin") + Directory(configuration);
MSBuild("./src/Example.sln", settings =>
For your initial try, I would also comment out or remove the Run-Unit-Tests task and remove the
Did you have any luck? If you ran into issues, check out the Cake website for more information. In future posts we’ll cover some common issues and how to resolve them.