【问题标题】:How to cache a docker image from a GitHub Action如何从 GitHub 操作缓存 docker 图像
【发布时间】:2022-05-20 16:47:12
【问题描述】:

我有一个场景,我在 GitHub 工作流上使用 maven 和 docker 构建应用程序。

然后在构建应用程序(带有 docker 映像)时,应用程序的集成测试失败。

在重新运行 GitHub 工作流程之前,我经常需要更改现有的集成测试而不更改应用程序本身。这会导致使用 java 和 docker 构建(并在本地测试)操作。

构建过程已经完成,一个 docker 镜像已经上传到 GitHub Packages。如何检查此应用程序是否已有 docker 映像?

我可以使用 actions/cache@v2 (https://github.com/actions/cache) 吗?如何?在它可以缓存的语言中没有提到 Docker...

【问题讨论】:

  • 如果没有代码更改,您的目标是加快构建过程吗?
  • 是的...这是为了加快构建过程。当构建过程已经执行此步骤时,等待带有 junit 测试的 maven 构建非常烦人...

标签: java docker github-actions


【解决方案1】:

我建议为此使用Docker's Build Push action。通过build-push-action,您可以使用内联缓存、注册表缓存或实验性缓存后端 API 来缓存您的容器镜像:

内联缓存

name: Build and push
uses: docker/build-push-action@v2
with:
    context: .
    push: true
    tags: user/app:latest
    cache-from: type=registry,ref=user/app:latest
    cache-to: type=inline

请参阅Buildkit docs

注册表缓存

name: Build and push
uses: docker/build-push-action@v2
with:
    context: .
    push: true
    tags: user/app:latest
    cache-from: type=registry,ref=user/app:buildcache
    cache-to: type=registry,ref=user/app:buildcache,mode=max

请参阅Buildkit docs

缓存后端 API

name: Build and push
uses: docker/build-push-action@v2
with:
    context: .
    push: true
    tags: user/app:latest
    cache-from: type=gha
    cache-to: type=gha,mode=max

请参阅Buildkit docs

我个人更喜欢使用缓存后端 API,因为它易于设置,并且可以大大缩短整体 CI 管道运行时间。

您不需要使用cache action,因为上述工作流本质上实现了它并为我们抽象了工作流。

这里是一个例子:https://github.com/moja-global/FLINT.Reporting/blob/d7504909f8f101054e503a2993f4f70ca92c2577/.github/workflows/docker.yml#L54

【讨论】:

    猜你喜欢
    • 2019-08-02
    • 1970-01-01
    • 2020-03-26
    • 2020-10-10
    • 2023-03-02
    • 2022-12-14
    • 2021-02-27
    • 2021-01-09
    • 2015-01-01
    相关资源
    最近更新 更多