Using GitLabCI with C#

I've been working on a C# application and wanted to try the GitLab CI out. All I can see is Ruby and can't find any information on how to build a C# application using it.

When I run the test settings, I make the commit, but I don't have my build.

Enter image description here

How should I make a simple build? Which command could I use for that? I don't mind if I get a failed build (but a build).


Solution 1:

I just wanted to share my .gitlab-ci.yml complete with unit testing. You will have to adjust your nuget and possibly other paths. This is for a single project in a solution of the same name.

variables:
  PROJECT_NAME: "ProjectNameGoesHere"
before_script:
  - echo "starting build for %PROJECT_NAME%"
  - echo "Restoring NuGet Packages..."
  - d:\tools\nuget restore "%PROJECT_NAME%.sln"
stages:
  - build
  - test
build:
  stage: build
  script:
  - echo "Release build..."
  - '"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe" /consoleloggerparameters:ErrorsOnly /maxcpucount /nologo /property:Configuration=Release /verbosity:quiet "%PROJECT_NAME%.sln"'
  artifacts:
    untracked: true
test:
  stage: test
  script:
  - echo "starting tests"
  - cd %PROJECT_NAME%Tests/bin/Release
  - '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\MSTest.exe" /testcontainer:%PROJECT_NAME%Tests.dll'
  dependencies:
  - build

Solution 2:

In order to build a C# application you should have a Windows runner (with shell executor) configured for a project in GitLab CI.

Your .gitlab-ci.yml file should look something like that:

stages:
  - build

job:
  stage: build
  script:
  - echo "Restoring NuGet Packages..."
  - '"c:\nuget\nuget.exe" restore "MySolution.sln"'
  - ''
  - echo "Release build..."
  - C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe /consoleloggerparameters:ErrorsOnly /maxcpucount /nologo /property:Configuration=Release /verbosity:quiet "MySolution.sln"
  tags:
  except:
  - tags

On a Windows machine you need the following tools:

  • Runner installed
  • Git, added to PATH
  • Latest nuget.exe at C:\nuget (or somewhere else. Just make sure you got the path right in the .gitlab-ci.yml file)

Solution 3:

The other answers are good. But I'd like to explain how to install a runner in addition. I use my own local system (Windows), so I chose to run shell. But you could use a Docker image if you'd like.

cd C:\Multi-Runner
gitlab-ci-multi-runner register

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
https://gitlab.com
Please enter the gitlab-ci token for this runner
xxx
Please enter the gitlab-ci description for this runner
my-runner
INFO[0034] fcf5c619 Registering runner... succeeded
Please enter the executor: shell, docker, docker-ssh, ssh?
shell
INFO[0037] Runner registered successfully. Feel free to start it, but if it's
running already the config should be automatically reloaded!

Source: https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/blob/master/docs/install/windows.md

Afterwards, you can use a YAML file a like this:

stages:
    - build
job:
    stage: build
    script: '"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe" "something.sln"'

Solution 4:

Installing the build runner on a Windows machine helps a lot, and @prasanth-louis has a great example on how to do that.

As for the .gitlab-ci.yml file, you can simplify it even more by using Cake Build:

stages:
    - build
build:
    stage: build
    script:
        - .\build.ps1 -Target Build
    tags:
        - windows

And your build.cake file can look like this (based of the example repository):

#tool nuget:?package=NUnit.ConsoleRunner&version=3.4.0

var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");

var solution = "./example-project.sln";
var buildDir = Directory("./example-project/bin");

Task("Default")
    .IsDependentOn("Unit-Tests")
    .Does(() =>
{
    Information("Running Default task!");
});

Task("Clean")
    .Does(() =>
{
    CleanDirectory(buildDir);
});

Task("PackageRestore")
    .IsDependentOn("Clean")
    .Does(() =>
{
    Information("Restoring NuGet packages for {0}", solution);
    NuGetRestore(solution);
});

Task("Build")
    .IsDependentOn("PackageRestore")
    .Does(() =>
{
    Information("Restoring NuGet packages for {0}", solution);
    MSBuild(solution, settings => settings.SetConfiguration(configuration));
});

Task("Unit-Tests")
    .IsDependentOn("Build")
    .Does(() =>
{
    NUnit3("./example-project.Tests/**/bin/" + configuration + "/*.Tests.dll");
});

Task("Publish")
    .Does(() =>
{

});

RunTarget(target);