【问题标题】:Build .NET solution using GitLab CI Pipeline使用 GitLab CI Pipeline 构建 .NET 解决方案
【发布时间】:2018-08-22 10:34:24
【问题描述】:

我有一个包含多个 .NET 项目的解决方案。我使用非自托管的 GitLab 进行版本控制,并且也想开始使用他们的 CI 工具。我已将以下 .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

build 阶段总是失败,因为它不知道msbuild 是什么。确切的错误是:

/bin/bash:第 61 行:msbuild.exe:找不到命令

经过一番调查,我发现我正在使用共享跑步者。以下是作业运行的全部输出:

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

看起来我的共享运行器正在使用 Ruby 的 Docker 映像,这似乎是错误的。我不知道如何更改或选择可用于 .NET 的其他选项。经过一些进一步的调查,我开始担心我必须跳过很多圈才能得到我想要的东西,比如使用 Azure VM 来托管可以构建 .NET 应用程序的 GitLab Runner。

我需要做什么才能使用 GitLab 的 CI 管道通过非自托管 GitLab 实例构建我的 .NET 解决方案?

【问题讨论】:

    标签: .net msbuild continuous-integration gitlab gitlab-ci-runner


    【解决方案1】:

    您应该能够在装有 Framework 4 构建工具的机器上设置您自己的共享运行程序(使用 Docker 映像,如 microsoft/dotnet-framework-build,或仅使用您的本机机器)。

    最简单的情况是使用您自己的桌面,您知道您的解决方案已经在其中构建。 (因为使用 Docker 映像进行构建是绝对可能的,但涉及确保您的机器上运行 docker 的所有额外步骤。

    • 从https://docs.gitlab.com/runner/install/windows.html在你的电脑上下载gitlab-runner

      • 在电脑上创建目录(C:\gitlab-runner)
      • 将最新的二进制文件x86 或x64 下载到该文件夹​​
      • 将二进制文件重命名为“gitlab-runner.exe”
    • 为您的跑步者获取一个 gitlab-ci 令牌
      • 可能最简单的方法是转到 gitlab.com 中的项目,然后转到 Settings -> CI/CD 并展开 General Pipeline Settings。
      • 在 Runner Token 部分,单击 Reveal Value 按钮以显示令牌值。您将在跑步者注册步骤中需要此信息。
    • 根据Registering Runners - Windows注册gitlab runner
      • 打开提升的命令提示符(以管理员身份运行)
      • cd 到 c:\gitlab-runner
      • 输入gitlab-runner register
      • 注册提示将引导您完成注册跑步者的步骤,但简而言之,您将输入
      • gitlab.com 作为你的协调者 URL,输入你项目中的令牌
      • 为您的跑步者命名
      • 标记您的跑步者(以便您可以将其与它能够构建、测试等的项目相关联 - 为简单起见,您现在可以跳过标记)
      • 允许它运行未标记的作业(再次,简单,说真的)
      • 将跑步者锁定到当前项目(简单,说真的)
      • 并选择执行器(进入 shell,基本上就是说使用 Windows 命令行)
    • 将 gitlab-runner 安装为服务,以便它始终检查待办事项
      • 在命令提示符下,输入gitlab-runner install
      • 然后输入gitlab-runner start
      • (现在,如果您转到服务,您将看到 gitlab-runner 列出,并且它应该正在运行 - 就在运行器崩溃时/如果运行器崩溃,您应该转到服务重新启动它)

    哇。现在运行器已设置,它应该在您推送提交或请求合并时激活。

    如果您在正确构建 .gitlab-ci.yml 文件时仍然遇到问题,您可以在命令行中转到解决方案文件夹,然后在本地调试它(无需通过 gitlab.com 继续触发它)执行c:\gitlab-runner\gitlab-runner build(例如测试构建步骤)。

    如果构建步骤在查找解决方案文件时遇到问题,您可能需要尝试将其从“msbuild.exe Bizio.sln”更改为“msbuild.exe .\Bizio.sln”

    【讨论】:

    • 我在类似问题上需要帮助。我的问题是 yml 文件。当我在服务器中运行应用程序时,它工作正常。但我没有得到构建应用程序的确切命令。可以给我样品吗?
    【解决方案2】:

    为了补充 realrae 的答案,.Net Core 应用程序的另一个选项是在 .gitlab-ci.yml 文件的第一行中包含对容器的调用。这将允许构建在共享的 GitLab.com 运行器上进行。

    image: microsoft/dotnet
    

    如果您正在构建传统的 .Net 应用程序,您可以尝试使用 mono docker 镜像。如果这对您不起作用,据我所知,本地跑步者是您唯一的选择。

    image: mono:latest
    

    【讨论】:

    • 你好。当我使用microsoft/dotnet:latest 时,出现 MSB3644 错误。可能是因为...sdk/2.2.300...。我是对的还是sdk版本与框架版本不同?有更新的图片吗?
    【解决方案3】:

    或多阶段构建:

    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"
    
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-29
    • 2016-10-24
    • 2018-12-11
    • 1970-01-01
    • 2010-12-17
    • 2016-01-04
    • 2011-12-24
    • 2018-08-26
    相关资源
    最近更新 更多