如何在作业之间重用docker容器



我的gitlab yml中有以下代码:

stages:
- unit_test
- deploy
Test:
stage: unit_test
script:
- docker run --rm -d --name myimage widgets:0.1 bash -c "tail -f /dev/null"
- docker exec -w /opt/source-code/tests myimage pwsh -c "dotnet test --test-adapter-path:. --logger:"junit;LogFilePath=..TestResultstest-results.xml;MethodFormat=Class;FailureBodyFormat=Verbose""
- docker cp myimage:/opt/source-code/TestResults/test-results.xml ./ 
artifacts:
when: always
paths:
- ./test-results.xml
reports:
junit:
- ./test-results.xml
tags:
- docker-azure
deploy_to_dev:
stage: deploy
script:
- docker exec myimage pwsh -c "./mydeploymentscript.ps1"
only: 
- master
tags:
- docker-azure

团队想要的是a(单元测试总是在管道被触发时运行,但b(实际部署逻辑只有在分支是主分支时才触发。

当管道到达部署阶段时,它当前正在失败,并返回错误:

Error: No such container: myimage

我试图测试我是否可以在两个作业之间重复使用相同的容器,因为我没有明确地执行";码头停靠站";在单元测试作业中。但我想不是。

我知道我可以在部署阶段重复所有相同的命令/执行另一个docker运行,但不知道是否还有其他我不知道的方法。

谢谢

  1. 我不一定能理解你的问题。如果你想在创建合并请求时执行你的作业,你可以使用"规则";像这样
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'

并且你的测试结果将在你的MR中提供。如果你的工作失败了,你的管道也失败了,并且你的MR没有被合并。

  1. 对于这一部分,如果你的工作失败,你的管道也会失败

最新更新