如何在 Gitlab CI 中将 docker 构建和发布脚本分离到不同的阶段
我想在两个单独的阶段构建和发布 docker 镜像:
- 构建
- 发布
不幸的是,Gitlab 在不同的工作线程中运行这些阶段 - ci-runner-worker11
和 ci-runner-worker15
。因此,在第一阶段中构建的图像在第二阶段中不可见。您是否知道一种干净的解决方案来实现这种分离并能够在阶段之间传递这些工件?
我尝试过这个解决方案:
container_images:
stage: build
artifacts:
untracked: true
image: ...
...
push_images:
stage: publish
dependencies:
- container_images
...
不幸的是,我收到一个错误:
untracked: found 170 files
ERROR: Uploading artifacts as "archive" to coordinator... too large archive id=15085670 responseStatus=413 Request Entity Too Large status=413
FATAL: too large
I would like to build and publish docker images in two separate stages:
- build
- publish
Unfortunately, Gitlab runs those stages inside different workers - ci-runner-worker11
and ci-runner-worker15
. Therefore images built in the first stage are not visible in the second stage. Do you know a clean solution to have this separation and be able to pass those artifacts between stages?
I've tried this solution:
container_images:
stage: build
artifacts:
untracked: true
image: ...
...
push_images:
stage: publish
dependencies:
- container_images
...
Unfortunately, I get an error:
untracked: found 170 files
ERROR: Uploading artifacts as "archive" to coordinator... too large archive id=15085670 responseStatus=413 Request Entity Too Large status=413
FATAL: too large
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么需要这个?也许有更好的解决方案。
untracked
工件是相对于项目根文件夹的,docker build 命令会将图像层存储在其他地方。如果您需要在设置最终标签之前测试容器,您可以:
您可以使用固定临时标签,但需要确保一次仅运行一个管道。或者,您可以使用基于
CI_PIPELINE_ID
环境变量的临时标记。Why do you need this? Maybe there is a better solution.
untracked
artifacts are relative to the project root folder, and the docker build command will store the image layers elsewhere.If you need to test the container before setting a final tag, you can:
You can use a fixed temp tag, but you need to be sure that you only run one pipeline at a time. Or, you can use a temp tag based on the
CI_PIPELINE_ID
environment variable.在构建阶段将 docker 镜像保存/导出*作为 tarball:
并将 tarball 添加到工件列表中:
然后在发布/部署阶段加载/导入* tarball:
然后您可以像平常一样将映像推送到容器注册表(例如):
* 请注意,保存/加载和导出/导入之间存在差异。如果您想保留图层和 Docker 上下文的所有其余部分,请使用保存/加载,如果您只想要平面图像,请使用导出/导入。
In the build phase save/export* the docker image as a tarball:
and add the tarball to the artifacts list:
Then in the publish/deploy phase load/import* the tarball:
You can then push the image to your container registry like normal (for example):
* Please note that there is a difference between save/load and export/import. If you want to keep the layers and all the rest of the docker context then use save/load, if you just want a flat image then use export/import.