如何在GitHub Action中缓存Docker镜像

huangapple go评论95阅读模式
英文:

How to cache a docker image from a GitHub Action

问题

我有一个情景,在GitHub工作流中使用Maven和Docker构建应用程序。

然后,当应用程序(带有Docker镜像)构建完成后,应用程序的集成测试失败。

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

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

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

英文:

I am having a scenario where I build an application using maven and docker on a GitHub workflow.

Then when the application (with docker image) is build, the integration testing of the application fails.

I often need to change existing integration tests without making changes in the application itself before rerunning The GitHub workflow. This causes the action to be build (and tested locally) with java and docker.

The build process is already done and a docker image uploaded to GitHub Packages. How can I check if this application already have a docker image?

Can I use actions/cache@v2 (https://github.com/actions/cache)? How? Docker is not mentioned in the languages it can cache...

答案1

得分: 2

我建议使用Docker的Build Push操作来实现这个目的。通过build-push-action,你可以使用内联缓存、注册表缓存或实验性缓存后端API来缓存你的容器映像:

内联缓存

name: 构建和推送
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文档

注册表缓存

name: 构建和推送
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文档

缓存后端API

name: 构建和推送
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文档

我个人更喜欢使用缓存后端API,因为它易于设置并显著减少了整个CI流水线的运行时间。

你不需要使用缓存操作,因为上述工作流已经内在地实现了它并为我们抽象了工作流程。

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

英文:

I would suggest using the Docker's Build Push action for this purpose. Through the build-push-action, you can cache your container images by using the inline cache, registry cache or the experimental cache backend API:

Inline cache

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

Refer to the Buildkit docs.

Registry Cache

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

Refer to Buildkit docs.

Cache backend 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

Refer to Buildkit docs.

I personally prefer using the Cache backend API as its easy to setup and provides a great boost in reducing the overall CI pipeline run duration.

You would not need to use the cache action since the above workflows intrinsically implements that and abstracts the workflow for us.

Here is an example: https://github.com/moja-global/FLINT.Reporting/blob/d7504909f8f101054e503a2993f4f70ca92c2577/.github/workflows/docker.yml#L54

huangapple
  • 本文由 发表于 2020年8月9日 02:12:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63318776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定