Nx Tools
Guides/Advanced/Cache

Cache

Speed up your builds by caching layers.

This page contains examples on using the cache storage backends

Note

See Cache storage backends for more details about cache storage backends.

Inline cache:

In most cases you want to use the inline cache exporter. However, note that the inline cache exporter only supports min cache mode. To use max cache mode, push the image and the cache separately using the registry cache exporter with the cache-to option, as shown in the registry cache example.

"container": {
  "executor": "@nx-tools/nx-container:build",
  "options": {
    "push": true,
    "tags": ["user/app:latest"],
    "cache-from": ["type=registry,ref=user/app:latest"],
    "cache-to": ["type=inline"]
  }
}

Registry cache

You can import/export cache from a cache manifest or (special) image configuration on the registry with the registry cache exporter.

"container": {
  "executor": "@nx-tools/nx-container:build",
  "options": {
    "push": true,
    "tags": ["user/app:latest"],
    "cache-from": ["type=registry,ref=user/app:buildcache"],
    "cache-to": ["type=registry,ref=user/app:buildcache,mode=max"]
  }
}

GitHub cache

Cache backend API

This is experimental

The GitHub Actions cache exporter backend uses the GitHub Cache service API to fetch and upload cache blobs. That's why you should only use this cache backend in a GitHub Action workflow, as the url ($ACTIONS_RESULTS_URL) and token ($ACTIONS_RUNTIME_TOKEN) attributes only get populated in a workflow context.

"container": {
  "executor": "@nx-tools/nx-container:build",
  "options": {
    "push": true,
    "tags": ["user/app:latest"],
    "cache-from": ["type=gha"],
    "cache-to": ["type=gha,mode=max"]
  }
}

Local cache

At the moment caches are copied over the existing cache so it keeps growing.

You can also leverage GitHub cache using actions/cache and type=local cache exporter with this action:

"container": {
  "executor": "@nx-tools/nx-container:build",
  "options": {
    "push": true,
    "tags": ["user/app:latest"],
    "cache-from": ["type=local,src=/tmp/.buildx-cache"],
    "cache-to": ["type=local,dest=/tmp/.buildx-cache-new,mode=max"]
  }
}

Last updated on