简体   繁体   English

使用GitLab CI Pipeline构建.NET解决方案

[英]Build .NET solution using GitLab CI Pipeline

I have a solution with several .NET projects in it. 我有一个包含多个.NET项目的解决方案。 I use GitLab, not self-hosted, for version control and would like to start using their CI tools as well. 我使用GitLab(不是自托管的)进行版本控制,并且也希望开始使用其CI工具。 I have added the following .gitlab-ci.yml file to my root: 我已将以下.gitlab-ci.yml文件添加到我的根目录:

stages:
  - build
  - test

build_job:
  stage: build
  script:
  - 'echo building...'
  - 'msbuild.exe Bizio.sln'
  except:
  - tags

test_job:
  stage: test
  script:
  - 'echo: testing...'
  - 'msbuild.exe Bizio.sln'
  - 'dir /s /b *.Tests.dll | findstr /r Tests\\bin\\ > tests_list.txt'
  - 'for /f %%f in (tests_list.txt) do mstest.exe /testcontainer: "%%f"'
  except:
  - tags

The build stage always fails because it doesn't know what msbuild is. build阶段总是失败,因为它不知道msbuild是什么。 The exact error is: 确切的错误是:

/bin/bash: line 61: msbuild.exe: command not found / bin / bash:第61行:msbuild.exe:找不到命令

After some investigating, I've figured out that I'm using a shared runner. 经过一番调查,我发现我正在使用共享跑步程序。 Here is the entire output from the job run: 这是作业运行的全部输出:

Running with gitlab-runner 10.6.0-rc1 (0a9d5de9)
  on docker-auto-scale 72989761
Using Docker executor with image ruby:2.5 ...
Pulling docker image ruby:2.5 ...
Using docker image sha256:bae0455cb2b9010f134a2da3a1fba9d217506beec2d41950d151e12a3112c418 for ruby:2.5 ...
Running on runner-72989761-project-1239128-concurrent-0 via runner-72989761-srm-1520985217-1a689f37...
Cloning repository...
Cloning into '/builds/hyjynx-studios/bizio'...
Checking out bc8085a4 as master...
Skipping Git submodules setup
$ echo building...
building...
$ C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe Bizio.sln
/bin/bash: line 61: msbuild.exe: command not found
ERROR: Job failed: exit code 1

It looks like the shared runner I have is using a Docker image for Ruby, which seems wrong. 看来我拥有的共享运行程序正在将Docker映像用于Ruby,这似乎是错误的。 I don't know how I can change that or select a different one that can be used for .NET. 我不知道如何更改该内容或选择可用于.NET的其他内容。 After some further investigating I'm getting worried that I'll have to jump through a lot of hoops to get what I want, like using an Azure VM to host a GitLab Runner that can build .NET apps. 经过进一步调查后,我担心我将不得不跳很多圈才能获得想要的东西,例如使用Azure VM托管可以构建.NET应用程序的GitLab Runner。

What do I need to do to use GitLab's CI pipelines to build my .NET solution using a non-self-hosted GitLab instance? 使用GitLab的CI管道使用非自托管的GitLab实例构建.NET解决方案,我需要做什么?

You should be able to setup your own shared runner on a machine with the Framework 4 build tools on it (either using a Docker image, like microsoft/dotnet-framework-build, or just your native machine). 您应该能够在装有Framework 4构建工具的机器上设置自己的共享运行器(使用Docker映像,例如microsoft / dotnet-framework-build,或者仅使用本机)。

The simplest case to get going is using your own desktop, where you know your solution already builds. 最简单的方法是使用您自己的桌面,您已经在其中建立了解决方案。 (Since using Docker images for the build is absolutely possible, but involves all of the extra steps of making sure you have docker working on your machine). (因为使用Docker镜像进行构建是绝对可能的,但是涉及确保您的计算机上使用Docker的所有额外步骤)。

  • Download gitlab-runner on your computer from https://docs.gitlab.com/runner/install/windows.html https://docs.gitlab.com/runner/install/windows.html将 gitlab-runner下载到您的计算机上

    • Create a directory on computer (C:\\gitlab-runner) 在计算机上创建目录(C:\\ gitlab-runner)
    • Download the latest binary x86 or x64 to that folder 将最新的二进制x86x64下载到该文件夹
    • Rename the binary to "gitlab-runner.exe" 将二进制文件重命名为“ gitlab-runner.exe”
  • Get a gitlab-ci token for your runner 为您的跑步者获取gitlab-ci令牌
    • Probably the easiest way to do this is to go to your project in gitlab.com and go to Settings -> CI/CD and expand General Pipeline Settings. 可能最简单的方法是转到gitlab.com中的项目,然后转到“设置”->“ CI / CD”并展开“常规管道设置”。
    • In the Runner Token section, click the Reveal Value button to show the token value. 在“运行者令牌”部分中,单击“显示值”按钮以显示令牌值。 You will need this during the runner registration step. 在跑步者注册步骤中,您将需要此功能。
  • Register the gitlab runner according to Registering Runners - Windows 根据注册gitlab亚军注册运动员-视窗
    • Open an elevated command prompt (Run as administrator) 打开提升权限的命令提示符(以管理员身份运行)
    • cd to c:\\gitlab-runner cd到c:\\ gitlab-runner
    • type gitlab-runner register 键入gitlab-runner register
    • The register prompts will take you through the steps to register the runner, but in a nutshell, you will be putting in 注册提示将引导您完成注册跑步者的步骤,但总而言之,您将输入
    • gitlab.com as your coordinator URL, entering the token from your project gitlab.com作为协调器URL,从您的项目中输入令牌
    • giving your runner a name 给跑步者起个名字
    • tagging your runner (so that you can associate it with projects that it is capable of building, testing, etc - for simplicity, you can skip tags now) 标记跑步者(以便您可以将其与能够构建,测试等的项目关联-为简单起见,您现在可以跳过标签)
    • allowing it to run for untagged jobs (again, simplicity, say true) 允许它运行未加标签的作业(再次,简单,说对了)
    • lock runner to current project (simplicity, say true) 将跑步者锁定到当前项目(简单地说是真的)
    • and choosing the executor (enter shell, which is basically saying use the Windows Command-line) 并选择执行程序(输入shell,基本上就是说使用Windows命令行)
  • Install gitlab-runner as a service, so that it is mostly always checking for work to do 将gitlab-runner安装为服务,以便它通常总是检查要执行的工作
    • In your command prompt, type gitlab-runner install 在命令提示符下,键入gitlab-runner install
    • Then type gitlab-runner start 然后输入gitlab-runner start
    • (Now, if you go to Services, you will see gitlab-runner listed, and it should be running - Just when when/if the runner crashes, you should go to Services to restart it) (现在,如果转到“服务”,则会看到列出了gitlab-runner,它应该正在运行-当跑步机崩溃时,您应该去“服务”重新启动它)

Whew. ew。 Now that runner is setup, it should be activated when you push a commit or a merge is requested. 现在已经设置好了运行器,当您推动提交或请求合并时,应该将其激活。

If you are having issues still with the .gitlab-ci.yml file building properly, you can debug it locally (without having to keep triggering it through gitlab.com) by going to your solution folder in the command line and then executing c:\\gitlab-runner\\gitlab-runner build (To test the build step, for example). 如果您仍然无法正确构建.gitlab-ci.yml文件,可以通过在命令行中转到解决方案文件夹然后执行c:\\gitlab-runner\\gitlab-runner build来在本地调试它(而不必通过gitlab.com不断触发它)。 c:\\gitlab-runner\\gitlab-runner build (例如,测试构建步骤)。

If the build step has problem finding your solution file, you might want to try changing it from 'msbuild.exe Bizio.sln' to 'msbuild.exe .\\Bizio.sln' 如果生成步骤在查找解决方案文件时遇到问题,则可能需要尝试将其从“ msbuild.exe Bizio.sln”更改为“ msbuild.exe。\\ Bizio.sln”

To complement the answer by reallyrae, the other option for .Net Core apps is to include a call to the container as the first line in your .gitlab-ci.yml file. 为了通过realrae补充答案,.Net Core应用程序的另一个选项是在.gitlab-ci.yml文件的第一行中包含对容器的调用。 This will allow the build to happen on a shared GitLab.com runner. 这将允许在共享的GitLab.com运行程序上进行构建。

image: microsoft/dotnet

If you are building a traditional .Net application, you can try the mono docker image. 如果您要构建传统的.Net应用程序,则可以尝试使用mono docker映像。 If that doesn't work for you, the local runner is, as far as I know, your only option. 如果这对您不起作用,据我所知,本地跑步者是您唯一的选择。

image: mono:latest

Or multi stage builds: 或多阶段构建:

stages:
  - build-dotnet
  - test-dotnet
  - build-mono
  - test-mono

build-dotnet:
  stage: build-dotnet
  image: microsoft/dotnet:latest
  before_script:
    - "cd source"
    - "dotnet restore"
    - "cd .."
    - "cd samples"
    - "dotnet restore"
    - "cd .."
  script:
      - "cd source"
      - "dotnet build"
      - "cd .."
      - "cd samples"
      - "dotnet build"
      - "cd .."

build-mono:
  stage: build-mono
  image: mono:latest
  before_script:
  script:
    - nuget restore ./source/Source.sln
    - msbuild 
        /p:Configuration="Release"
        /p:Platform="Any CPU" 
        "./source/Source.sln"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM