如何在 Gitlab CI 中将 docker 构建和发布脚本分离到不同的阶段

发布于 2025-01-11 01:47:34 字数 691 浏览 2 评论 0原文

我想在两个单独的阶段构建和发布 docker 镜像:

  • 构建
  • 发布

不幸的是,Gitlab 在不同的工作线程中运行这些阶段 - ci-runner-worker11ci-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぶ宁プ宁ぶ 2025-01-18 01:47:34

为什么需要这个?也许有更好的解决方案。

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:

  • Use a temp tag
  • Push it to the repository
  • Test the image (with the temp tag)
  • If all tests pass, change the tag and push it again to the repository
  • If the test fails, remove the test image.

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.

Saygoodbye 2025-01-18 01:47:34

在构建阶段将 docker 镜像保存/导出*作为 tarball:

docker save ${IMAGE} | gzip > saved-docker-image.tgz

并将 tarball 添加到工件列表中:

container_images:
  stage: build
  artifacts:
    paths:
      - saved-docker-image.tgz
  image: ...
...

然后在发布/部署阶段加载/导入* tarball:

cat saved-docker-image.tgz | gunzip | docker load

然后您可以像平常一样将映像推送到容器注册表(例如):

docker push ${IMAGE}

* 请注意,保存/加载和导出/导入之间存在差异。如果您想保留图层和 Docker 上下文的所有其余部分,请使用保存/加载,如果您只想要平面图像,请使用导出/导入。

In the build phase save/export* the docker image as a tarball:

docker save ${IMAGE} | gzip > saved-docker-image.tgz

and add the tarball to the artifacts list:

container_images:
  stage: build
  artifacts:
    paths:
      - saved-docker-image.tgz
  image: ...
...

Then in the publish/deploy phase load/import* the tarball:

cat saved-docker-image.tgz | gunzip | docker load

You can then push the image to your container registry like normal (for example):

docker push ${IMAGE}

* 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文