Ola*_*son 7 .net c# gitlab gitlab-ci gitlab-ci-runner
这就是我使用 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
Run Code Online (Sandbox Code Playgroud)
这次在 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
Run Code Online (Sandbox Code Playgroud)
第一个作业通过,但第二个作业失败,因为它无法从第一个作业中找到恢复的文件。所以我的解决方案有一个TestProject1
,第二个工作是无法在其中找到资源文件...\TestProject1\obj\project.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
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用工件和标志在作业之间传输每个命令的输出:
我相信工件必须是您的 $CI_PROJECT_DIR (source) 的相对路径,并且根据dotnet restore
文档,默认写入位置位于主目录中。
您可以尝试使用(源)将写入位置指定为包/dotnet restore --packages packages
和读取位置为包/与dotnet build --source packages
(源)
然后你需要指定这个工件,如:
artifacts:
paths:
- packages
Run Code Online (Sandbox Code Playgroud)
您需要类似的--output
标志用法来在build-solution
阶段中保存您的构建工件。
这更复杂,但在某些情况下可能是需要的。
也许我值得尝试将管道定义为有向无环图(DAG),因此dependencies
您可以定义needs
. 当使用关键字定义作业时,needs
仅当所有依赖的先前作业都成功时(除非允许它们失败),它才会启动。此处关键字的优点needs
是您可以显式配置从以前的作业下载工件的步骤
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
needs:
- job: restore-dependencies
artifacts: true
run-tests:
stage: run-tests
script:
- dotnet test --no-build --output build
needs:
- job: build-solution
artifacts: true
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
297 次 |
最近记录: |