如何在GitHub Action中缓存docker-compose构建

19

有没有办法缓存docker-compose,使其不会一遍又一遍地构建? 这是我的action工作流文件:

name: Github Action
on:
  push:
    branches:
      - staging
jobs:
  test:
    runs-on: ubuntu-18.04

    steps:
      - uses: actions/checkout@v1

      - name: Bootstrap app on Ubuntu
        uses: actions/setup-node@v1
        with:
          node-version: '12'


      - name: Install global packages
        run: npm install -g yarn prisma


      - name: Install project deps
        if: steps.cache-yarn.outputs.cache-hit != 'true'
        run: yarn


      - name: Build docker-compose
        run: docker-compose -f docker-compose.test.prisma.yml up --build -d

我希望缓存 Docker 构建步骤。我尝试使用 if: steps.cache-docker.outputs.cache-hit != 'true' then only build,但没有生效。

4个回答

12

12

那么,现在无法使用 GitHub Action 完成这个任务吗? - Ashik
https://github.com/satackey/action-docker-layer-caching 怎么样? - Lucas Bustamante
我试过那个,但它无法处理图像的sha。 - Vaal
docker的build-push-action不直接支持docker-compose的层缓存:https://github.com/docker/build-push-action/issues/128但是有一个解决方法,我还没有测试过:https://github.com/docker/build-push-action/issues/150 - Scott McAllister

7
这个问题虽然有些陈旧,但我发现自己正试图解决同样的问题。在阅读了许多不同的答案并花费了大量时间后,我终于找到了一个不错的解决方案。
我的工作流文件现在看起来像这样:
jobs:
  build:
    name: Integration tests
    runs-on: ubuntu-22.04
    # I need "packages: write" to access GHCR.
    # More about permissions here: https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs
    permissions: write-all
    steps:
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Login to Docker Registry
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/checkout@v2

      - name: Build docker-compose
        # Before the cache, it was "run: docker compose build".
        run: docker buildx bake --file docker-compose.yml --file docker-compose-cache.json

...

docker-compose-cache.json 文件中,我有以下内容:

{
  "target": {
    "service-name": {
      "cache-from": [
        "type=registry,ref=ghcr.io/MY_GITHUB_ORG_NAME/service-name:cache"
      ],
      "cache-to": [
        "type=registry,ref=ghcr.io/MY_GITHUB_ORG_NAME/service-name:cache"
      ],
      "output": [
        "type=docker"
      ]
    }
}

docker-compose.yml中,对于每个服务,我会在docker-compose-cache.json中添加一个targetdocker buildx bakedocker-compose.yml获取构建指令,并从docker-compose-cache.json获取缓存指令。
这样,我仍然可以像平常一样在本地使用docker-compose up --build
值得注意的是,我正在使用GitHub容器注册表而不是GitHub操作缓存,因为GHCR没有缓存大小限制。

太完美了,正是我所需要的!这是最好的答案。 - Anthony Garcia-Labiad

2
如果使用docker/bake-actiondocker/build-push-action并且希望在后续步骤中访问缓存的镜像-
  • 使用load:true保存镜像
  • 在各个步骤中使用与缓存镜像相同的镜像名称,以跳过重新构建。

示例:

...
        name: Build and push
        uses: docker/bake-action@master
        with:
          push: false
          load: true
        set: |
            web.cache-from=type=gha
            web.cache-to=type=gha
      -
        name: Test via compose
        command: docker compose run web tests
...

services:
  web:
    build:
      context: .
    image: username/imagename
    command: echo "Test run successful!"

查看 docker 团队的回复;


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