Build, test and deploy .NET Core projects with TeamCity

Dmitry Tretyakov

teamcity-net-coreThe .NET Core development platform is the next wave of .NET ecosystem evolution which brings cross-platform targets for your existing or new products. Microsoft ships .NET Core SDK packages with a runtime for supported platforms and command line tools (.NET CLI) to build, test, and pack such projects.

The .NET Core support plugin for TeamCity aims to bring streamline support for .NET Core CLI by providing .NET Core build steps, CLI detection on the build agents, and auto-discovery of build steps in your repository.

Installation

The plugin is available for download from the TeamCity plugin gallery and can be installed as an additional TeamCity plugin. Next the .NET Core SDK has to be installed on your build agent machines. The plugin uses the PATH environment variable to determine the .NET CLI tools location, but you can override it by specifying the DOTNET_HOME environment variable as follows: DOTNET_HOME=C:\Program Files\dotnet\

You can check that plugin has been properly initialized by navigating to the build agent parameters:

net-core-parameters

Building and testing .NET Core projects

The .NET Core plugin supports auto-discovery of build steps, and usually all you need is to use the Create project from URL wizard:

net-core-build-steps

If you want to tune some parameters in your build steps, the .NET Core plugin provides details about configuration options and hints at their possible values:

net-core-step

Specify project build version

While building and creating project packages, the .NET CLI tools use the version number defined in the project.json file. If you want to provide a custom build number, you can update your project.json by leveraging the File Content Replacer build feature as follows:

net-core-version

Here you need to specify the target project.json files to process, fill in (\{[^{]*\"version\"\s*:\s*\")([^\"]+)(\") in the Find what and $1%build.number%$3 in the Replace with fields. Then, after executing the vcs checkout, the File Content Replacer will set the version, for instance %build.number% in the previous example.

Note: Currently .NET CLI tools support projects with project.json-based structure, but the upcoming versions will rely on the new csproj structure.

Testing .NET Core projects

The xUnit framework provide dotnet test runner which report the information about test results on the fly:

net-core-tests

The NUnit test runner does not support it out of the box, but you can vote for the following issue. If you need any other test framework support, please leave a comment in the related issue.

Deploying .NET Core projects

.NET Core libraries

Your projects can contain a number of shared libraries which can be reused in dependent project builds. Instead of artifact dependencies, it is recommended that you use the build-in TeamCity NuGet feed. In this the specified build artifacts paths need to include the produced NuGet packages.

Tip: You can use the NuGet Dependency Trigger to start a build when a package has been updated in some nuget feed.

Publishing ASP.NET Core projects

Such projects can be deployed to a wide range of platforms. As a result of executing the dotnet publish build step, you will receive the published package data in your file system. Then you need to decide how to deploy it to the target system.

For Windows servers, the Web Deploy utility can be used. Alternatively, you can deploy the package with the auto-generated %profile%-publish.ps1 or default-publish.ps1 PowerShell scripts. More details on how to provide proper arguments to these scripts can be found in the Web Publishing documentation.

For other server platforms you can publish package by leveraging TeamCity Deployer build steps like FTP/SSH/SMB.

Feedback

You are welcome to try the plugin for TeamCity, and your feedback will be really appreciated, so please share your thoughts in the comments to this blog post, in the forum or issue tracker.

Happy building modern .NET Core projects!

banner_blog@2x

Comments below can no longer be edited.

27 Responses to Build, test and deploy .NET Core projects with TeamCity

  1. Sam says:

    November 3, 2016

    Thanks!
    Great to have some detailed instructions for .net core!
    Since I had some trouble running the %profile%-publish.ps1 effectively, I wrote a post on how to deploy to Azure / IIS using the Powershell script: https://medium.com/monkii/how-to-deploy-asp-net-core-sites-using-teamcity-or-just-command-line-cf05fdee58f5

  2. Kelby says:

    November 15, 2016

    TeamCity 10.0.2 –

    Something that isn’t mentioned in this article or the most up to date article for installing addition plugins here: https://confluence.jetbrains.com/display/TCD10/Installing+Additional+Plugins

    I used the upload plugin zip feature within the web application. I chose the zip file and named it “NET Core” and it refused to get picked up from TeamCity after restarting. Nothing was in the TeamCity logs. Before I contacted support and after ripping out some hair, I browsed to the plugins folder and renamed the file from “NET Core” to “NETCore.zip” and TeamCity finally picked it up! Either it was having a problem with the space in the file name or missing the zip extension, either way, this has to be a bug.

    • Pavel Sher says:

      November 16, 2016

      Thank you for reporting this problem! We’ll try to find out the cause and fix it in the nearest bugfix release.

  3. Jhonny Yin says:

    January 12, 2017

    This article was great and help me set up dotnet builds with TC. Will code coverage be added to this soon?

  4. Jhonny Yin says:

    January 12, 2017

    Will code coverage be added to this?

    • Dmitry Tretyakov says:

      January 15, 2017

      Thanks for a reply, could you please vote for this issue in the plugin tracker?

  5. Vadim says:

    January 18, 2017

    TeamCity 10.0.4 how Install plugin, after plugin installation agent not starting? If I installing only agent part it works, there is no .Net core(dotnet) in Runners Type. Thanks

    • Dmitry Tretyakov says:

      January 18, 2017

      Hi, Vadim. Could you please create an issue in our tracker? We need to know TeamCity .NET Core plugin version from the Administration -> Plugins List page and look at the teamcity-agent.log file from the build agent.

  6. Michael Freidgeim says:

    January 20, 2017

    Using animated image in this post is not a good idea. Initially it looks cool, but images are changed too quickly to understand content. Also when reading text below the image, flicking disrupts the reading.

    • Gary Woodfine says:

      February 7, 2017

      I have to agree with Michael. The animated gif is annoying. It flicks too quickly though the screens and it doesn’t serve any real purpose other than annoy your users

    • Ivan Boyko says:

      March 4, 2017

      Agree

  7. Richard says:

    January 23, 2017

    I got the following error when publishing to IIS. Anything I miss?

    [21:48:25][Step 3/3] Configuring the following project for use with IIS: ‘.\output’
    [21:48:25][Step 3/3] No web.config found. Creating ‘.\output\web.config’
    [21:48:26][Step 3/3] System.IO.DirectoryNotFoundException: Could not find a part of the path ‘C:\BuildAgentPlatinumProd\work\81197cfcde494597\src\Api\MarketExposure.Api\output\web.config’.
    [21:48:26][Step 3/3] at System.IO.Win32FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
    [21:48:26][Step 3/3] at System.IO.Win32FileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
    [21:48:26][Step 3/3] at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
    [21:48:26][Step 3/3] at Microsoft.AspNetCore.Server.IISIntegration.Tools.PublishIISCommand.Run()
    [21:48:26][Step 3/3] at Microsoft.AspNetCore.Server.IISIntegration.Tools.Program.c__DisplayClass0_0.b__0()
    [21:48:26][Step 3/3] at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
    [21:48:26][Step 3/3] at Microsoft.AspNetCore.Server.IISIntegration.Tools.Program.Main(String[] args)
    [21:48:26][Step 3/3] Could not find a part of the path ‘C:\BuildAgentPlatinumProd\work\81197cfcde494597\src\Api\MarketExposure.Api\output\web.config’.

    • Richard says:

      January 23, 2017

      Make it work now. Looks like the published folder must be in an absolute path.

  8. Gary Woodfine says:

    February 7, 2017

    Has anybody tried building a dotnet core app on a linux server (ubuntu) ?
    I installed the Plugin, no issues, but seem to be able to get my build agent to recognise that it is a dotnet core build agent?

    I have installed both dotnet core and cake build on the build server,

    • Dmitry Tretyakov says:

      February 8, 2017

      Hello Garry, what dotnet core cli version did you installed on your build agent? After installation you need to restart TeamCity build agent service and check that on the build agent configuration parameters appeared DotNetCore parameter. To detect .net cli location this plugin uses PATH / DOTNET_HOME environment variables of user under which TeamCity build agent is running.

  9. Ehsan says:

    February 7, 2017

    What is involved in using these tools with the new .netcore CLI version (rc3 as of today)? We are having issues getting our .netcore package to put itself on our NuGet store.

    • Dmitry Tretyakov says:

      February 8, 2017

      Hello, could you please create an issue with some details about how do you publish your nuget package and what the error did you see in the build log in the TeamCity or project repository tracker?

  10. Claudio Bernasconi says:

    February 16, 2017

    This article inspired me to try to setup TeamCity for my dotnet core side projects. I faced some challenges since this article is mainly written from the eyes of a person who knows about TeamCity in detail. I have written down a step-by-step guide on how to setup TeamCity for CI/CD for .NET Core applications on my blog. I think this is especially helpful for beginners.

    http://www.claudiobernasconi.ch/2017/02/16/continuous-integration-and-deployment-of-asp-net-core-applications/

    Keep up the good work!

  11. Arkady says:

    February 17, 2017

    The problem is with project.json file. In Visual Studio 2017 it changed to *.csproj file. Will you update plugin and article? 🙂

    • Dmitry Tretyakov says:

      February 17, 2017

      Hi Arkady, the latest .net core plugin version already supports csproj builds. To build a new project generated by Visual Studio 2017 you need to use up to date plugin version, update .net core cli on the build agent and then point in your build steps to *.sln and/or *.csproj files instead of project.json. If you have some troubles with it, please file an issue in the issue tracker. We will try to provide an updated blog post when .net cli will reach rtm.

  12. Carsten says:

    February 27, 2017

    Use the following regex to avoid matching unwanted “version”s
    (^\{[^{]*\”version\”\s*:\s*\”)([^\”]+)(\”)

  13. Wes says:

    March 7, 2017

    What would the regex be for the new .csproj format? Now it’s in an xml element like

    1.0.3

    • Dmitry Tretyakov says:

      March 20, 2017

      Hi, patterns in File Content Replacer for .NET Core csproj will be available in the TeamCity 2017.1, but you can manually specify something like that:

      (<(Version)\s*>).*(<\/\s*\2\s*>)

      where second group can be replaced by %build.number%

    • Arve Systad says:

      April 5, 2017

      We use the following, which works fine.

      Find what: (?:)([0-9.]*)(?:)
      Replace with: %build.number%

      And then just make sure your build.number is what you want.

  14. Leszek says:

    March 13, 2017

    Can you add support for Net core projects targeting Net framework 4.x ?

    More info:
    http://www.hanselman.com/blog/HowToReferenceAnExistingNETFrameworkProjectInAnASPNETCore10WebApp.aspx

  15. Tom says:

    April 26, 2017

    How do you actually use WebDeploy with dotnet publish? I’m really struggling to find any resources on how to perform this step! Most articles seem to just assume you’re publishing to the same machine, or manually copying the files over to a remote server.

    I’ve got a publish profile set up in my project, so it would be really useful if I could pass that to donet publish.

Subscribe

Subscribe for updates