Build ASP.NET Core with Jenkins on Windows

A post on how I configured Jenkins v2 on Windows for ASP.NET Core applications using Pipeline as code.

Before we start

  • Why Windows when ASP.NET Core can build and run on linux? This Jenkins instance was going to be used for other specific Windows builds as well. At least one of the ASP.NET Core applications needs to be built against the .NET Full Framework.
  • Why Jenkins? It’s what I’ve been using personally or on other teams for many years now. I’m sure Teamcity or VSTS are more than capable.
  • I use rake to run my build tasks (choco install ruby)
  • I’m using git (choco install git)

Versions as of writing

  • Windows 10 Pro (Build 15063.138)
  • Jenkins v2.55.
  • git 2.12

Install Jenkins

I’m using Jenkins v2 with the new Blue Ocean UI (currently installed as a plugin).

  1. Go to https://jenkins.io/download/
  2. Download either LTS or Weekly installer for Windows.
  3. Run the installer. I usually install Jenkins to an empty volume (e.g. D:\).
  4. Go into Manage Jenkins -> Manage Plugins. Install “Blue Ocean”. Restart Jenkins when finished.

Install VSTools

This installs MSBuild and related files. I’ve based it on this answer on StackOverflow.

  1. Go to https://www.visualstudio.com/downloads/.
  2. Scroll down to the expandable section and click on “Other Tools and Frameworks”.
  3. Select “Download” button for “Build Tools for Visual Studio 2017”.
  4. Open up Command Prompt and execute the following: vs_buildtools.exe --add Microsoft.VisualStudio.Workload.MSBuildTools --add Microsoft.VisualStudio.Workload.WebBuildTools --quiet

You can find more information about those component identifiers at https://<docs.microsoft.com/en-us/visualstudio/install/workload-component-id-vs-build-tools>

A couple of notes about MSBuild and Visual Studio 2017:

  • MSBuild has now been moved into the Visual Studio 2017 installation folder.
  • You can install Visual Studio 2017 anywhere.
  • Visual Studio 2017 now supports side-by-side installation between the different sku’s (enterprise, professional community, buildtools).
  • There is no standard registry key or environment variable that indicates where Visual Studio, let alone MSBuild is installed.
  • Microsoft has some recommendations on how to find where Visual Studio 2017 is installed at. E.g. vswhere.
  • It is no longer as easy to determine where MSBuild is, unlike before.

To get around this, what I’ve done for my environments is add an MSBUILD15 environment variable that points to the folder of msbuild. Any build scripts I have bomb out with a helpful message if that variable isn’t set.

Note that you can add this variable through Jenkins so its presented to your build scripts without having to edit Windows settings. Go to Manage Jenkins -> Manage Nodes then edit the node that you are configuring.

Configure Your Project for Pipelines

As we will be using Pipelines in Jenkins (instructions to Jenskins on what to do with your project), we need to create a Jenkinsfile in the root of our repository. This will tell Jenkins what the steps are for our pipeline and how to execute them.

As this is a simple project and I’m using rake to execute the task, this is what my Jenkinsfile looks like:

node {
    stage 'Checkout'
        checkout scm

    stage 'Build'
        bat 'rake build'

    stage 'Test'
        bat 'rake test'
}

You can read all about Pipelines at https://jenkins.io/doc/book/pipeline/.

Create new Pipeline

As I stated before I’m using Blue Ocean so the steps below reflect that.

  1. In the normal Jenkins view, click on the “Open Blue Ocean” button in the nav-bar.
  2. Select “New Pipeline”.
  3. Select where your code is stored. For me this is “Git” (this project is on Bitbucket).
  4. Enter in the repository URL and create a new Credential for it. I use SSH Private Key which is configured as a read-only key for this particular repository in bitbucket.
  5. Select Create Pipeline.
  6. Jenkins should connect to the repository, find the Jenkinsfile and perform the defined tasks. If everything was run successfully, you should see something like this:

Poll Repository for Changes

As my Jenkins instance is running within a LAN and is not visible to bitbucket, we need to tell Jenkins to poll regularly for changes to the repository. To do that:

  1. Edit the project in Jenkins (Select the cog in the top-right hand corner).
  2. Scroll down to “Scan Multibranch Pipeline Triggers” and check the checkbox and select an appropriate Interval:
  3. Save

And that should be it.