2021-02-18 01:53:15 +08:00
# Cache
2021-07-29 22:57:22 +08:00
* [Inline cache ](#inline-cache )
2021-02-18 01:53:15 +08:00
* [Registry cache ](#registry-cache )
* [GitHub cache ](#github-cache )
2021-07-29 22:57:22 +08:00
* [Cache backend API ](#cache-backend-api )
* [Local cache ](#local-cache )
2021-02-18 01:53:15 +08:00
2022-10-08 01:16:42 +08:00
> **Note**
>
> See [our guide](https://github.com/docker/buildx/blob/master/docs/guides/cache/index.md)
> for more details about cache storage backends.
2021-02-18 01:53:15 +08:00
2021-07-29 22:57:22 +08:00
## Inline cache
2021-02-18 01:53:15 +08:00
2022-10-08 01:16:42 +08:00
In most cases you want to use the [`type=inline` cache exporter ](https://github.com/docker/buildx/blob/master/docs/guides/cache/inline.md ).
2021-07-29 22:57:22 +08:00
However, note that the `inline` cache exporter only supports `min` cache mode. To enable `max` cache mode, push the
image and the cache separately by using the `registry` cache exporter as shown in the [next example ](#registry-cache ).
2021-02-18 01:53:15 +08:00
```yaml
name: ci
on:
push:
branches:
2022-01-18 21:57:27 +08:00
- 'main'
2021-02-18 01:53:15 +08:00
jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Checkout
2022-05-29 00:36:30 +08:00
uses: actions/checkout@v3
2021-02-18 01:53:15 +08:00
-
name: Set up Docker Buildx
2022-05-06 01:24:32 +08:00
uses: docker/setup-buildx-action@v2
2021-02-18 01:53:15 +08:00
-
2022-10-08 01:16:42 +08:00
name: Login to Docker Hub
2022-05-06 01:24:32 +08:00
uses: docker/login-action@v2
2021-02-18 01:53:15 +08:00
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
2022-05-06 01:24:32 +08:00
uses: docker/build-push-action@v3
2021-02-18 01:53:15 +08:00
with:
context: .
push: true
tags: user/app:latest
cache-from: type=registry,ref=user/app:latest
cache-to: type=inline
```
2021-07-29 22:57:22 +08:00
## Registry cache
You can import/export cache from a cache manifest or (special) image configuration on the registry with the
2022-10-08 01:16:42 +08:00
[`type=registry` cache exporter ](https://github.com/docker/buildx/blob/master/docs/guides/cache/registry.md ).
2021-07-29 22:57:22 +08:00
```yaml
name: ci
on:
push:
branches:
2022-01-18 21:57:27 +08:00
- 'main'
2021-07-29 22:57:22 +08:00
jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Checkout
2022-05-29 00:36:30 +08:00
uses: actions/checkout@v3
2021-07-29 22:57:22 +08:00
-
name: Set up Docker Buildx
2022-05-06 01:24:32 +08:00
uses: docker/setup-buildx-action@v2
2021-07-29 22:57:22 +08:00
-
2022-10-08 01:16:42 +08:00
name: Login to Docker Hub
2022-05-06 01:24:32 +08:00
uses: docker/login-action@v2
2021-07-29 22:57:22 +08:00
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
2022-05-06 01:24:32 +08:00
uses: docker/build-push-action@v3
2021-07-29 22:57:22 +08:00
with:
context: .
push: true
tags: user/app:latest
cache-from: type=registry,ref=user/app:buildcache
cache-to: type=registry,ref=user/app:buildcache,mode=max
```
2021-02-18 01:53:15 +08:00
## GitHub cache
2021-07-29 22:57:22 +08:00
### Cache backend API
2022-10-08 01:16:42 +08:00
> **Warning**
>
> This cache exporter is considered EXPERIMENTAL until further notice. Please
> provide feedback on [BuildKit repository](https://github.com/moby/buildkit)
> if you encounter any issues.
[GitHub Actions cache exporter ](https://github.com/docker/buildx/blob/master/docs/guides/cache/gha.md )
backend uses the [GitHub Cache API ](https://github.com/tonistiigi/go-actions-cache/blob/master/api.md )
to fetch and upload cache blobs. That's why this type of cache should be
exclusively used in a GitHub Action workflow as the `url` (`$ACTIONS_CACHE_URL`)
and `token` (`$ACTIONS_RUNTIME_TOKEN`) attributes are populated when a workflow
2021-07-29 22:57:22 +08:00
is started.
```yaml
name: ci
on:
push:
branches:
2022-01-18 21:57:27 +08:00
- 'main'
2021-07-29 22:57:22 +08:00
jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Checkout
2022-05-29 00:36:30 +08:00
uses: actions/checkout@v3
2021-07-29 22:57:22 +08:00
-
name: Set up Docker Buildx
2022-05-06 01:24:32 +08:00
uses: docker/setup-buildx-action@v2
2021-07-29 22:57:22 +08:00
-
2022-10-08 01:16:42 +08:00
name: Login to Docker Hub
2022-05-06 01:24:32 +08:00
uses: docker/login-action@v2
2021-07-29 22:57:22 +08:00
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
2022-05-06 01:24:32 +08:00
uses: docker/build-push-action@v3
2021-07-29 22:57:22 +08:00
with:
context: .
push: true
tags: user/app:latest
cache-from: type=gha
cache-to: type=gha,mode=max
```
### Local cache
2022-10-08 01:16:42 +08:00
> **Warning**
>
> At the moment caches are copied over the existing cache, so it [keeps growing](https://github.com/docker/build-push-action/issues/252).
> The `Move cache` step is used as a workaround (see [moby/buildkit#1896](https://github.com/moby/buildkit/issues/1896) for more info).
2021-02-18 01:53:15 +08:00
2022-10-08 01:16:42 +08:00
You can also leverage [GitHub cache ](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows )
using the [actions/cache ](https://github.com/actions/cache ) and [`type=local` cache exporter ](https://github.com/docker/buildx/blob/master/docs/guides/cache/local.md )
2021-07-29 22:57:22 +08:00
with this action:
2021-02-18 01:53:15 +08:00
```yaml
name: ci
on:
push:
branches:
2022-01-18 21:57:27 +08:00
- 'main'
2021-02-18 01:53:15 +08:00
jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Checkout
2022-05-29 00:36:30 +08:00
uses: actions/checkout@v3
2021-02-18 01:53:15 +08:00
-
name: Set up Docker Buildx
2022-05-06 01:24:32 +08:00
uses: docker/setup-buildx-action@v2
2021-02-18 01:53:15 +08:00
-
name: Cache Docker layers
2022-05-29 00:36:30 +08:00
uses: actions/cache@v3
2021-02-18 01:53:15 +08:00
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
-
2022-10-08 01:16:42 +08:00
name: Login to Docker Hub
2022-05-06 01:24:32 +08:00
uses: docker/login-action@v2
2021-02-18 01:53:15 +08:00
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
2022-05-06 01:24:32 +08:00
uses: docker/build-push-action@v3
2021-02-18 01:53:15 +08:00
with:
context: .
push: true
tags: user/app:latest
cache-from: type=local,src=/tmp/.buildx-cache
2021-07-29 22:57:22 +08:00
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
2021-02-18 01:53:15 +08:00
-
# 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
```