nakamasato / github-actions-practice

Github Actions Practice
2 stars 1 forks source link

docker cache comparison #377

Open nakamasato opened 1 year ago

nakamasato commented 1 year ago

Summary

  1. max vs. min: mode - Specifies how many layers are exported with the cache. min on only exports layers already in the final build stage, max exports layers for all stages. Metadata is always exported for the whole build.
  2. cache destination
    1. registry type exports build cache to a cache manifest in the registry.
    2. local type exports cache to a local directory on the client.
    3. inline type writes the cache metadata into the image configuration.
    4. gha type exports cache through the GitHub Actions Cache service API <- Looks like this is the most effective way
    5. s3 type exports cache to a S3 bucket.

Docs

  1. depot.dev/blog/docker-layer-caching-in-github-actions
  2. docs.docker.com/build/ci/github-actions
  3. moby/buildkit@master/frontend/dockerfile/docs/reference.md#run---mounttypecache
  4. zenn.dev/masibw/articles/57a47a7381b9b3
  5. docs.docker.com/build/cache/#use-the-dedicated-run-cache
/opt/hostedtoolcache/go/1.19.6/x64/bin/go env GOMODCACHE
/opt/hostedtoolcache/go/1.19.6/x64/bin/go env GOCACHE
/home/runner/go/pkg/mod
/home/runner/.cache/go-build

naive (no cache): 2m 18s

      - name: docker build
        run: docker build --tag mysql-operator:latest .

docker build + github actions cache 🙅

action-docker-layer-caching Not working

      - name: docker layer cache
        uses: satackey/action-docker-layer-caching@v0.0.11

      - name: docker build
        run: docker build --tag mysql-operator:latest .
Screen Shot 2023-03-14 at 7 05 20

docker/build-push-action

inline (skip)

(skip) as registry covers everything that inline cache can do

registry

      - name: Build with registry cache
        uses: docker/build-push-action@v4
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: ${{ secrets.DOCKERHUB_USERNAME }}/mysql-operator:latest
          cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/mysql-operator:buildcache
          cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/mysql-operator:buildcache

type=registry with github packages 🙅

not fast

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}
...
    permissions:
      contents: read
      packages: write
...
      - name: Log in to the Container registry
        uses: docker/login-action@v2
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v4
        with:
          context: context
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

https://github.com/nakamasato/mysql-operator/blob/99ba619019e4ad7cd5c6e8a81d22379a0e93c3fe/.github/workflows/e2e.yml

Screen Shot 2023-03-13 at 7 02 39

type=registry with load 🙅

cannot push and load at the same time

https://github.com/nakamasato/mysql-operator/blob/7530489843fe4361ee004a77de2b18d951d3894a/.github/workflows/e2e.yml

Screen Shot 2023-03-13 at 7 06 41

type=gha ✅

      - name: Build with gha
        uses: docker/build-push-action@v4
        with:
          context: .
          push: false # a shorthand for --output=type=registry if set to true
          load: true # a shorthand for --output=type=docker if set to true
          tags: mysql-operator:latest
          cache-from: type=gha
          cache-to: type=gha,mode=max
Screen Shot 2023-03-13 at 6 43 02

https://github.com/nakamasato/mysql-operator/blob/783575c5894dcd8de71b09eed4a98055415d1838/.github/workflows/e2e.yml

type=local ✅

      - name: Cache Docker layers
        uses: actions/cache@v2
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-buildx-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-buildx-

      - name: Build with local cache
        uses: docker/build-push-action@v4
        with:
          context: .
          push: false
          load: true
          tags: mysql-operator:latest
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max

      - # Temp fix
        # https://github.com/docker/build-push-action/issues/252
        # https://github.com/moby/buildkit/issues/1896
        name: Move cache
        run: |
          rm -rf /tmp/.buildx-cache
          mv /tmp/.buildx-cache-new /tmp/.buildx-cache

https://github.com/nakamasato/mysql-operator/blob/aefb790469f3b645edba924612c743269cb2e41c/.github/workflows/e2e.yml

Screen Shot 2023-03-13 at 7 55 48

from https://github.com/docker/build-push-action/issues/252

nakamasato commented 1 year ago

ToDo1: Run cache

/opt/hostedtoolcache/go/1.19.6/x64/bin/go env GOMODCACHE
/opt/hostedtoolcache/go/1.19.6/x64/bin/go env GOCACHE
/home/runner/go/pkg/mod
/home/runner/.cache/go-build

ToDo2: skaffold cache

https://github.com/GoogleContainerTools/skaffold/issues/4842