.NET Core and GoCD

I’ve been reading up on continuous delivery and the GoCD tool is a great way to learn the concepts and abstractions to help model your delivery pipeline and work towards improving it.

This post documents setting up a .NET core project in GoCD in a minimal way for demo purposes. I cover setting up a hello world app and simple scripts to build, test, and package. Then I will create a pipeline in GoCD to build our site and run unit tests. Finally I’ll show how to add additional pipelines for manual acceptance tests and deployment.

If you want to know about my environment and .NET core project for this demo you can read the next section Demo Environment Setup. If you want to just jump ahead to the GoCD you can scroll down to the GoCD Setup or GoCD Pipelines sections.

Demo Environment Setup

Demo Environment
For this post I’ll be using the following file structure:

Where I keep my development git repo.
C:\demo\development
Where I will keep my bare git repo to be used as GoCD material.
C:\demo\git_server

Material Setup
First I’ll setup a file based git server in C:\demo\git_server. I’m just doing this for simplicity. You could also use Github or another SCM server.

mkdir hwapp
cd hwapp
git init --bare

Then I’ll setup a share so the git repo can be used as a material and cloned into my development folder.
\\lenovo-pc\git_server\hwapp

Then clone that into my development folder.


cd development
git clone \\lenovo-pc\git_server\hwapp

Now we will just setup the hello world .NET Core app.


cd hwapp
dotnet new

Then run it to make sure everything is working.

dotnet restore
dotnet run

Add a .gitignore file.

.gitignore
bin/
obj/
project.lock.json
_build

To keep things simple I won’t use a build tool and just write a few small windows commands.

build.cmd file.
build.cmd
git clean -xfd
dotnet restore
dotnet build

test.cmd file.
test.cmd
dotnet test

package.cmd file.
package.cmd
IF [%1] == [] GOTO error

@Echo Off
dotnet pack -o _build --version-suffix %1
EXIT /B

:error
Echo version required.

Add some example unit tests using Xunit.net:

Here is my updated project json to allow me to run xunit tests.

{
  "version": "1.0.0-*",
  "testRunner": "xunit",
  "buildOptions": {
    "emitEntryPoint": true
  },
  "dependencies": {
      "xunit": "2.1.0",
      "dotnet-test-xunit": "1.0.0-rc2-build10025"
  },
  "frameworks": {
      "netcoreapp1.0": {
          "dependencies": {
              "Microsoft.NETCore.App": {
                  "type": "platform",
                  "version": "1.0.0-rc2-3002702"
              }
          },
          "imports": [
              "dnxcore50",
              "portable-net45+win8"
          ]
      }
  }
}

Add xunit tests.
tests.cs

using Xunit;

namespace MyFirstDotNetCoreTests
{
    public class Class1
    {
        [Fact]
        public void PassingTest()
        {
            Assert.Equal(4, Add(2, 2));
        }

        int Add(int x, int y)
        {
            return x + y;
        }
    }
}

Now the build, test, package cmds should work. Test and commit and push that to origin.

git add -A
git commit -m 'initial hello world app'
git push origin master

GoCD Setup

Note: For windows users I would suggest a simple file path without spaces. I’ll use C:\demo\GoCD\Server and C:\demo\GoCD\Agent for the install and host it on localhost.

Getting up and running with GoCD is simple. Download the server and agent and run the installers. Once it is running your GoCD server should be available at http://localhost:8153 or http://your-server-installation-hostname:8153. Verify the server and agents are working by visiting the site and checking the agents tab.

Now that the test environment is setup we can start adding a pipeline.
Continue reading “.NET Core and GoCD”