如何从 GitHub Action 缓存 docker 镜像

jac*_*ses 6 java docker github-actions

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

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

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

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

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

小智 2

我建议使用Docker 的 Build Push 操作来实现此目的。通过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
Run Code Online (Sandbox Code Playgroud)

请参阅Buildkit 文档

注册表缓存

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
Run Code Online (Sandbox Code Playgroud)

请参阅Buildkit 文档

缓存后端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
Run Code Online (Sandbox Code Playgroud)

请参阅Buildkit 文档

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

您不需要使用缓存操作,因为上述工作流程本质上实现了该操作并为我们抽象了工作流程。

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