jactor-rises
jactor-rises

Reputation: 3089

How to cache a docker image from a GitHub Action

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

Upvotes: 7

Views: 1426

Answers (1)

Harsh Mishra
Harsh Mishra

Reputation: 901

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

Upvotes: 3

Related Questions