如何为C#项目设置GitlabCI作业工件?

c#

这就是我使用 Github 的方式:

jobs:
  run-tests:

    runs-on: ubuntu-latest
    
    defaults:
      run:
        working-directory: ./MyApp

    steps:
    - uses: actions/checkout@v2
    
    - name: Setup .NET
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 5.0.x

    - name: Restore dependencies
      run: dotnet restore
      
    - name: Build project
      run: dotnet build --no-restore
      
    - name: Run tests
      run: dotnet test --no-build

这次在 Gitlab 上,我的项目解决方案文件位于存储库的根目录中。我创建了一个.gitlab-ci.yml文件并从

image: mcr.microsoft.com/dotnet/sdk:5.0

stages:
  - restore-dependencies
  - build-solution
  - run-tests

restore-dependencies:
  stage: restore-dependencies
  script:
    - dotnet restore --packages packages
  artifacts:
    paths:
      - packages

build-solution:
  stage: build-solution
  script:
    - dotnet build --no-restore --source packages --output build
  artifacts:
    paths:
      - build
  dependencies:
    - restore-dependencies

run-tests:
  stage: run-tests
  script:
    - dotnet test --no-build --output build
  dependencies:
    - build-solution

第一个作业通过,但第二个作业失败,因为它无法从第一个作业中找到恢复的文件。所以我的解决方案有一个TestProject1,第二个工作是无法在其中找到资源文件...TestProject1objproject.assets.json

如何修复管道配置?

回答

您正在使用单独的作业来运行每个命令。Gitlab 在一个新容器中运行每个作业,销毁来自前一个命令的任何上下文,除了明确指定的工件。要像在 Github 操作中配置的那样运行顺序命令,您只需添加多个步骤,这些步骤将在同一图像中顺序运行,从而保留命令之间的上下文。

stages:
 - build-solution

run-tests:
  image: mcr.microsoft.com/dotnet/sdk:5.0
  stage: build-solution
  script:
    - dotnet restore
    - dotnet build --no-restore
    - dotnet test --no-build

或者,您可以使用工件和标志在作业之间传输每个命令的输出:

我相信工件必须是您的 $CI_PROJECT_DIR (source) 的相对路径,并且根据dotnet restore文档,默认写入位置位于主目录中。

您可以尝试使用(源)将写入位置指定为包/dotnet restore --packages packages

和读取位置为包/与dotnet build --source packages (源)

然后你需要指定这个工件,如:

artifacts:
  paths:
    - packages

您需要类似的--output标志用法来在build-solution阶段中保存您的构建工件。

这更复杂,但在某些情况下可能是需要的。


以上是如何为C#项目设置GitlabCI作业工件?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>