From c124ff0226b3fef10736e3dacd826525ac24bfa6 Mon Sep 17 00:00:00 2001
From: CrazyMax <crazy-max@users.noreply.github.com>
Date: Sat, 29 Aug 2020 16:10:05 +0200
Subject: [PATCH] Add GitHub cache example

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
---
 .github/workflows/ci.yml | 62 +++++++++++++++++++++++++++++++
 README.md                | 80 +++++++++++++++++++++++++++++++++++-----
 2 files changed, 132 insertions(+), 10 deletions(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index bb7ed2d..9a59d46 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -123,6 +123,68 @@ jobs:
         if: always()
         uses: crazy-max/ghaction-dump-context@v1
 
+  github-cache:
+    runs-on: ubuntu-latest
+    steps:
+      -
+        name: Run local registry
+        run: |
+          docker run -d -p 5000:5000 registry:2
+      -
+        name: Checkout
+        uses: actions/checkout@v2.3.1
+      -
+        name: Set up QEMU
+        uses: docker/setup-qemu-action@master
+        with:
+          platforms: all
+      -
+        name: Set up Docker Buildx
+        id: buildx
+        uses: docker/setup-buildx-action@master
+        with:
+          driver-opt: network=host
+          buildkitd-flags: --allow-insecure-entitlement security.insecure
+      -
+        name: Cache Docker layers
+        uses: actions/cache@v2
+        id: cache
+        with:
+          path: /tmp/.buildx-cache
+          key: ${{ runner.os }}-buildx-${{ github.sha }}
+          restore-keys: |
+            ${{ runner.os }}-buildx-
+      -
+        name: Build and push
+        id: docker_build
+        uses: ./
+        with:
+          context: ./test
+          file: ./test/Dockerfile-multi-golang
+          builder: ${{ steps.buildx.outputs.name }}
+          platforms: linux/amd64,linux/arm64
+          allow: network.host,security.insecure
+          push: true
+          tags: |
+            localhost:5000/name/app:latest
+            localhost:5000/name/app:1.0.0
+          cache-from: type=local,src=/tmp/.buildx-cache
+          cache-to: type=local,dest=/tmp/.buildx-cache
+      -
+        name: Inspect
+        run: |
+          docker buildx imagetools inspect localhost:5000/name/app:1.0.0
+      -
+        name: Image digest
+        run: echo ${{ steps.docker_build.outputs.digest }}
+      -
+        name: Cache hit
+        run: echo ${{ steps.cache.outputs.cache-hit }}
+      -
+        name: Dump context
+        if: always()
+        uses: crazy-max/ghaction-dump-context@v1
+
   git-context:
     runs-on: ubuntu-latest
     strategy:
diff --git a/README.md b/README.md
index 715b2aa..eda2c79 100644
--- a/README.md
+++ b/README.md
@@ -7,8 +7,10 @@
 
 GitHub Action to build and push Docker images.
 
-> :bulb: See also our [setup-buildx](https://github.com/docker/setup-buildx-action)
-> and [setup-qemu](https://github.com/docker/setup-qemu-action) actions
+> :bulb: See also:
+> * [login](https://github.com/docker/login-action) action
+> * [setup-buildx](https://github.com/docker/setup-buildx-action) action
+> * [setup-qemu](https://github.com/docker/setup-qemu-action) action
 
 ![Screenshot](.github/build-push-action.png)
 
@@ -18,6 +20,7 @@ ___
   * [Quick start](#quick-start)
   * [Multi-platform image](#multi-platform-image)
   * [Git context](#git-context)
+  * [Leverage GitHub cache](#leverage-github-cache)
   * [Complete workflow](#complete-workflow)
 * [Customizing](#customizing)
   * [inputs](#inputs)
@@ -51,13 +54,13 @@ jobs:
         uses: actions/checkout@v2
       -
         name: Set up QEMU
-        uses: docker/setup-qemu-action@v1
+        uses: docker/setup-qemu-action@master
         with:
           platforms: all
       -
         name: Set up Docker Buildx
         id: buildx
-        uses: docker/setup-buildx-action@v1
+        uses: docker/setup-buildx-action@master
       -
         name: Login to DockerHub
         uses: docker/login-action@v1 
@@ -95,13 +98,13 @@ jobs:
         uses: actions/checkout@v2
       -
         name: Set up QEMU
-        uses: docker/setup-qemu-action@v1
+        uses: docker/setup-qemu-action@master
         with:
           platforms: all
       -
         name: Set up Docker Buildx
         id: buildx
-        uses: docker/setup-buildx-action@v1
+        uses: docker/setup-buildx-action@master
       -
         name: Login to DockerHub
         uses: docker/login-action@v1 
@@ -140,13 +143,13 @@ jobs:
     steps:
       -
         name: Set up QEMU
-        uses: docker/setup-qemu-action@v1
+        uses: docker/setup-qemu-action@master
         with:
           platforms: all
       -
         name: Set up Docker Buildx
         id: buildx
-        uses: docker/setup-buildx-action@v1
+        uses: docker/setup-buildx-action@master
         with:
           version: latest
       -
@@ -170,6 +173,63 @@ jobs:
           GIT_AUTH_TOKEN: ${{ github.token }}
 ```
 
+### Leverage GitHub cache
+
+You can leverage [GitHub cache](https://docs.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows)
+using [@actions/cache](https://github.com/actions/cache) with this action.
+
+```yaml
+name: ci
+
+on:
+  push:
+    branches: master
+
+jobs:
+  main:
+    runs-on: ubuntu-latest
+    steps:
+      -
+        name: Checkout
+        uses: actions/checkout@v2
+      -
+        name: Set up QEMU
+        uses: docker/setup-qemu-action@master
+        with:
+          platforms: all
+      -
+        name: Set up Docker Buildx
+        id: buildx
+        uses: docker/setup-buildx-action@master
+      -
+        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: Login to DockerHub
+        uses: docker/login-action@v1 
+        with:
+          username: ${{ secrets.DOCKER_USERNAME }}
+          password: ${{ secrets.DOCKER_PASSWORD }}
+      -
+        name: Build and push
+        id: docker_build
+        uses: docker/build-push-action@v2
+        with:
+          builder: ${{ steps.buildx.outputs.name }}
+          push: true
+          tags: user/app:latest
+          cache-from: type=local,src=/tmp/.buildx-cache
+          cache-to: type=local,dest=/tmp/.buildx-cache
+      -
+        name: Image digest
+        run: echo ${{ steps.docker_build.outputs.digest }}
+```
+
 ### Complete workflow
 
 * On `pull_request` event, Docker image `name/app:edge` is **built**.
@@ -216,13 +276,13 @@ jobs:
           echo ::set-output name=tags::${TAGS}
       -
         name: Set up QEMU
-        uses: docker/setup-qemu-action@v1
+        uses: docker/setup-qemu-action@master
         with:
           platforms: all
       -
         name: Set up Docker Buildx
         id: buildx
-        uses: docker/setup-buildx-action@v1
+        uses: docker/setup-buildx-action@master
       -
         name: Login to DockerHub
         if: github.event_name != 'pull_request'