Setting up .net core continuous integration build with VSTS/TFS

categories: [ VSTS / TFS ] tags: [ Automated Builds ] [ dotnet core ] [ Visual Studio ]
created: 23 Apr 2017 @ 17:06 modified: 25 Apr 2017 @ 19:42

You might be wondering after recently posting Setting up a standard continuous integration build with VSTS/TFS why I'd need to post specifically on .net core. After all it's still a Visual Studio solution so things should just work, the keyword here is should Smile. Things didn't just work when I tried to get this working so decided to share my experience here (as well as document it for myself later Smile with tongue out).

Sample project

So we going to need a sample project for this, I'm going to create a standard .net core web project

image

using asp.net core 1.1 as a Web Application

image

We also going to add another project using the Unit Test Project (.NET Core) template

image

We now need to add the MVC nuget reference to this test project. Open manage Nuget reference for solution

  1. In the Installed tab
  2. Click on Microsoft.AspNetCore.Mvc
  3. Check the test project
  4. Click Install

image

Next we'll

  1. Add a reference to our web project
  2. Rename the test to "HomeControllerTests.cs"
  3. Rename the test class to "HomeControllerTests"
  4. Add a About Test Method which you can find in my GitHub Gists

image

That should be all we need to demonstrate CI being harder for .net core Smile. Check this code into TFS and we all set to get started

image

Let's start off by creating a standard build how explained in the last post I did Setting up a standard continuous integration build with VSTS/TFS, I only did the first section with non of the extra fluff Smile.

Changes to build for .net core

The first thing you will notice if you just try run the build it will break

Restoring .net core packages

So our first set of errors is that the build can't find a bunch of references even though the restore step ran through.

image

This is because we need to do our restore a different way using the .net core restore. Let's fix this by adding the .NET Core (PREVIEW) task to our build

image

Next we need to

  1. Place this task first in the list of tasks
  2. Change the Command to restore
  3. Set the Project(s) to "**\*.sln"

image

Click Save. Run your build. This time our build runs through but is partially succeeded.

image

We going to remove the Publish symbols path step from our build because we aren't going to need it

SNAGHTML12c92962

Click Save after removing this and run your build so we can confirm everything is green before continuing on.

Running .net core tests

If you looked at your build after it ran you would have noticed that there is no test results

image

We know that we have a test and that we are running tests in the build so what's going on here?

Well you probably guessed it but we need to add the .NET Core (PREVIEW) step again this time

  1. Place the step after the Build solution step
  2. Change the Command to test
  3. Set the Project(s) to "**\*.Tests.csproj"
  4. Set the Arguments to "--no-build --logger:trx --configuration $(BuildConfiguration)"

image

What we are doing now is using .net core's test capabilities, telling the running we want to log the results using the trx format and that we want to run the BuildConfiguration that the build is running under.

Click Save. Run the build. You will notice that there is still no test results showing up and this is because we need to tell the build system to publish these results. Add the Publish Test Results task

image

Next we'll

  1. Make sure we place that task after the dotnet test task
  2. Set the Test Result Format to VSTest
  3. Set the Test Results Files to "**/TestResults/*.trx"
  4. Check the Merge Test Results checkbox
  5. Open the Advanced section and set the Configuration to "$(BuildConfiguration)"
  6. Check the Always run (we want to publish results if test fail Smile with tongue out, thank you Mikael Krief)

image

Click Save. Run the build. This time when the build has run if you go to the Summary you will notice 2 weird things

  1. We have 2 tests found (but only building 1 configuration)
  2. The build says there is no tests found

image

The 2nd issue is easy to fix, if you don't expect to have regular .net tests in the solution remove the standard test step to get rid of this warning

image

The 1st issue is because we are running the tests but not cleaning up the test results after the builds test results after they have generated or before we generate them in the build. The easiest way to fix this is to Clean all build directories before starting your build as I mentioned in the standard CI process blog mentioned multiple times in this post

SNAGHTML12df5ea0

If you don't want to do this you can look at adding in a delete task before your run tests step to remove all trx files.

If we make either change, save and run the build. We'll notice that we have 1 test result. Note you may not have seen this experience if your build was running on a different agent each time but when it did run on the same agent again you would have seen this behavior

image

Now our CI process is behaving like normal Smile

Conclusion

Yes there is extra steps you need to do for .net core but in the end the process is still considered easy, especially if you have developed with .net core and know the dotnet commands.