在C#中使用GitLabCI

时间:2015-11-04 14:46:58

标签: c# gitlab gitlab-ci

我一直在研究C#应用程序,并想尝试GitLab CI。我只能看到Ruby,无法找到有关如何使用它构建C#应用程序的任何信息。

当我运行测试设置时,我会进行提交,但我没有构建。

enter image description here

我应该如何进行简单的构建?我可以使用哪个命令?我不介意我的构建失败(但是构建)。

5 个答案:

答案 0 :(得分:38)

我只想分享我的.gitlab-ci.yml完成单元测试。您将不得不调整您的nuget和可能的其他路径。这是针对同名解决方案中的单个项目。

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

答案 1 :(得分:20)

为了构建C#应用程序,您应该为Gitlab CI中的项目配置Windows Runner(带有shell执行程序)。

你的.gitlab-ci.yml文件看起来应该是这样的:

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

在Windows计算机上,您需要以下工具:

  • 跑步者安装
  • git,添加到PATH
  • c:\ nuget上的最新nuget.exe(或其他地方,确保你在.gitlab-ci.yml文件中找到了正确的路径)

答案 2 :(得分:9)

其他答案都很好。但是我想解释一下如何安装跑步者。我使用自己的本地系统(Windows),所以我选择运行shell。但如果您愿意,可以使用泊坞窗图像。

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!

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

之后,你可以像这样使用yml文件:

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

答案 3 :(得分:8)

我知道,迟到了,但我想进一步扩展这个。

在Windows机器上安装构建运行器有很大帮助,@ prasanth-louis就如何做到这一点有一个很好的例子。

对于.gitlab-ci.yml,您可以使用Cake Build

进一步简化它
stages:
    - build
build:
    stage: build
    script: 
        - .\build.ps1 -Target Build
    tags:
        - windows

您的build.cake文件可能如下所示(基于example repo):

#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);

答案 4 :(得分:1)

这是我工作的.gitlab-ci.yml文件,用于C#应用程序,其中NUnit作为单元测试框架,mono作为基本映像。

不是很喜欢但是可以工作:

image: mono:latest

stages:
    - build
    - test

variables:
    solution: "Project.sln"
    test: "Project.Test"

before_script:
    - nuget restore

build:
    stage: build
    script:
        - msbuild /p:Configuration=Release $solution

test:
    stage: test
    script:
        - msbuild /p:Configuration=Release $solution
        - mono ./packages/NUnit.ConsoleRunner.3.10.0/tools/nunit3-console.exe ./$test/bin/Release/$test.dll