9

I execute some tests in GitHub Actions using Testcontainers.

Testcontainers pulls the images which are used in my tests. Unfortunately the images are pulled again at every build.

How can I cache the images in GitHub Actions?

Oliver
  • 353
  • 1
  • 4
  • 16

1 Answers1

8

There's no official support from GitHub Actions (yet) to support caching pulled Docker images (see this and this issue).

What you can do is to pull the Docker images, save them as a .tar archive and store them in a folder for the GitHub Actions cache action to pick it up.

A sample workflow can look like the following:

 build-java-project:
    runs-on: ubuntu-latest
    steps:
        - uses: actions/checkout@v2

        - run: mkdir -p ~/image-cache

        - id: image-cache
          uses: actions/cache@v1
          with:
              path: ~/image-cache
              key: image-cache-${{ runner.os }}

        - if: steps.image-cache.outputs.cache-hit != 'true'
          run: |
              docker pull postgres:13
              docker save -o ~/image-cache/postgres.tar alpine

        - if: steps.image-cache.outputs.cache-hit == 'true'
          run: docker load -i ~/image-cache/postgres.tar

        - name: 'Run tests'
          run: ./mvnw verify

While this is a little bit noisy, you'd need to adjust the pipeline every time you're depending on a new Docker image for your tests. Also be aware of how to do cache invalidation as I guess if you plan to use a :latest tag, the solution above won't recognize changes to the image.

The current GitHub Actions cache size is 10 GB which should be enough for a mid-size project relying on 5-10 Docker images for testing.

There's also the Docker GitHub Cache API but I'm not sure how well this integrates with Testcontainers.

rieckpil
  • 10,470
  • 3
  • 32
  • 56