GitHub Actions:如何为Testcontainers缓存Docker镜像?

10

我在GitHub Actions中使用Testcontainers执行一些测试。

Testcontainers会拉取我测试中使用的镜像。不幸的是,这些镜像在每次构建时都会被重新拉取。

我该如何在GitHub Actions中缓存这些镜像呢?

1个回答

9

目前 GitHub Actions 官方尚未提供支持缓存 Docker 镜像的功能(请参见 这个这个问题)。

你可以先拉取 Docker 镜像,将它们保存为一个 .tar 文件并将其存储在一个文件夹中,以便 GitHub Actions 缓存操作可以使用。

一个示例工作流程可以如下所示:

 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

虽然这样有点吵闹,但每次依赖于新的 Docker 映像进行测试时,您需要调整管道。此外,要注意如何进行缓存失效,因为如果计划使用 :latest 标签,上述解决方案将无法识别映像的更改。

当前的 GitHub Actions 缓存大小为10 GB,对于依靠5-10个 Docker 映像进行测试的中型项目来说应该足够了。

还有Docker GitHub 缓存 API,但我不确定它与 Testcontainers 的集成情况如何。


谢谢!那很有帮助,虽然我同意这并不是最佳选择。 - Oliver

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接