|
| 1 | +# Cache |
| 2 | + |
| 3 | +* [Registry cache](#registry-cache) |
| 4 | +* [GitHub cache](#github-cache) |
| 5 | + |
| 6 | +> More info about buildx cache: https://github.com/docker/buildx#--cache-fromnametypetypekeyvalue |
| 7 | +
|
| 8 | +## Registry cache |
| 9 | + |
| 10 | +You can import/export cache from a cache manifest or (special) image configuration on the registry. |
| 11 | + |
| 12 | +```yaml |
| 13 | +name: ci |
| 14 | + |
| 15 | +on: |
| 16 | + push: |
| 17 | + branches: |
| 18 | + - 'master' |
| 19 | + |
| 20 | +jobs: |
| 21 | + docker: |
| 22 | + runs-on: ubuntu-latest |
| 23 | + steps: |
| 24 | + - |
| 25 | + name: Checkout |
| 26 | + uses: actions/checkout@v2 |
| 27 | + - |
| 28 | + name: Set up Docker Buildx |
| 29 | + uses: docker/setup-buildx-action@v1 |
| 30 | + - |
| 31 | + name: Login to DockerHub |
| 32 | + uses: docker/login-action@v1 |
| 33 | + with: |
| 34 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 35 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 36 | + - |
| 37 | + name: Build and push |
| 38 | + uses: docker/build-push-action@v2 |
| 39 | + with: |
| 40 | + context: . |
| 41 | + push: true |
| 42 | + tags: user/app:latest |
| 43 | + cache-from: type=registry,ref=user/app:latest |
| 44 | + cache-to: type=inline |
| 45 | +``` |
| 46 | +
|
| 47 | +## GitHub cache |
| 48 | +
|
| 49 | +> :warning: At the moment caches are copied over the existing cache so it [keeps growing](https://github.com/docker/build-push-action/issues/252). |
| 50 | +> The `Move cache` step is used as a temporary fix (see https://github.com/moby/buildkit/issues/1896). |
| 51 | + |
| 52 | +> :rocket: There is a new cache backend using GitHub cache being developed that will lighten your workflow. |
| 53 | +> More info: https://github.com/docker/buildx/pull/535 |
| 54 | + |
| 55 | +You can leverage [GitHub cache](https://docs.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows) |
| 56 | +using [actions/cache](https://github.com/actions/cache) with this action: |
| 57 | + |
| 58 | +```yaml |
| 59 | +name: ci |
| 60 | +
|
| 61 | +on: |
| 62 | + push: |
| 63 | + branches: |
| 64 | + - 'master' |
| 65 | +
|
| 66 | +jobs: |
| 67 | + docker: |
| 68 | + runs-on: ubuntu-latest |
| 69 | + steps: |
| 70 | + - |
| 71 | + name: Checkout |
| 72 | + uses: actions/checkout@v2 |
| 73 | + - |
| 74 | + name: Set up Docker Buildx |
| 75 | + uses: docker/setup-buildx-action@v1 |
| 76 | + - |
| 77 | + name: Cache Docker layers |
| 78 | + uses: actions/cache@v2 |
| 79 | + with: |
| 80 | + path: /tmp/.buildx-cache |
| 81 | + key: ${{ runner.os }}-buildx-${{ github.sha }} |
| 82 | + restore-keys: | |
| 83 | + ${{ runner.os }}-buildx- |
| 84 | + - |
| 85 | + name: Login to DockerHub |
| 86 | + uses: docker/login-action@v1 |
| 87 | + with: |
| 88 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 89 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 90 | + - |
| 91 | + name: Build and push |
| 92 | + uses: docker/build-push-action@v2 |
| 93 | + with: |
| 94 | + context: . |
| 95 | + push: true |
| 96 | + tags: user/app:latest |
| 97 | + cache-from: type=local,src=/tmp/.buildx-cache |
| 98 | + cache-to: type=local,dest=/tmp/.buildx-cache-new |
| 99 | + - |
| 100 | + # Temp fix |
| 101 | + # https://github.com/docker/build-push-action/issues/252 |
| 102 | + # https://github.com/moby/buildkit/issues/1896 |
| 103 | + name: Move cache |
| 104 | + run: | |
| 105 | + rm -rf /tmp/.buildx-cache |
| 106 | + mv /tmp/.buildx-cache-new /tmp/.buildx-cache |
| 107 | +``` |
0 commit comments