Building .NET Core applications in TeamCity

Continuous integration is a critical part of the development lifecycle move changes quickly from code through testing and packaging to deployment.  These steps apply to TeamCity, but the work is really performed in docker, so could be done on nearly any CI:

It’s challenging to expect the TeamCity build agents to be up to date with the latest runtime.  If you can’t count on that, then running command line tasks with Docker is your best bet.

First, the build.  Add a “command line” runner.  The working directory on the agent will be the location where the source is checked out, so you’ll need to attach this directory to a docker container with the -v option.  Then within that container, you’ll need to run the dotnet commands, so the container needs to be based off the SDK images provided from Microsoft.  The latest image at time of writing supports the msbuild project file format.

To actually build, you need to run a bash command to

  1. Change to the directory that is mounted from the host (the build agent’s working directory).
  2. Restore dependencies with dotnet restore
  3. Build the project or solution with dotnet build
  4. Test the project with dotnet test
  5. Build a nuget package with dotnet pack

All of that can be done within the same container using the following command line:

docker run -v `pwd`:/src --rm -i microsoft/dotnet:sdk /bin/bash -c "cd src; dotnet restore && dotnet build && dotnet test mytestproject/mytestproject.fsproj && dotnet pack myproject/myproject.fsproj"

Once executed, a successful build will result in a nuget package under the working directory, in this case, at myproject/bin/Debug/myproject.1.0.0-alpha.nupkg.  The && chains commands together upon success, so, for example, if the tests fail, the package won’t be produced.  The container was started with --rm and so it will be removed upon exit of the command, cleaning up any build artifacts.  An additional build step can publish that package to a nuget repository.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s