icon

Multikey Restore Cache

Restores build cache using a cache keys. This Step needs to be used in combination with Multikey Save Cache or Save Cache.

Restores items from the cache based on a list of keys.

The format of the keys input is the following:

KEY1 || KEY1_ALTERNATIVE1 || KEY1_ALTERNATIVE2
KEY2
KEY3 || KEY3_ALTERNATIVE1

The number of keys and paths for each are limited to a number of 10. Commas (,) and equal signs (=) are not allowed in keys. See templates that can be used in the keys below.

Example (somewhat artificial):

multikey_0 || multikey_0_fallback
multikey_1
multikey_2 || multikey_2_fallback

This Step needs to be used in combination with Multikey Save Cache or Save Cache.

About key-based caching

Key-based caching is a concept where cache archives are saved and restored using a unique cache key. One Bitrise project can have multiple cache archives stored simultaneously, and the Multikey Restore Cache step downloads a cache archive associated with the key provided as a Step input.

Caches can become outdated across builds when something changes in the project (for example, a dependency gets upgraded to a new version). In this case, a new (unique) cache key is needed to save the new cache contents. This is possible if the cache key is dynamic and changes based on the project state (for example, a checksum of the dependency lockfile is part of the cache key). If you use the same dynamic cache key when restoring the cache, the Step will download the most relevant cache archive available.

Key-based caching is platform-agnostic and can be used to cache anything by carefully selecting the cache key and the files/folders to include in the cache.

Templates

The Step requires a string key to use when downloading a cache archive. In order to always download the most relevant cache archive for each build, the cache key input can contain template elements. The Step evaluates the key template at runtime and the final key value can change based on the build environment or files in the repo.

The following variables are supported in cache keys input:

  • cache-key-{{ .Branch }}: Current git branch the build runs on
  • cache-key-{{ .CommitHash }}: SHA-256 hash of the git commit the build runs on
  • cache-key-{{ .Workflow }}: Current Bitrise workflow name (eg. primary)
  • {{ .Arch }}-cache-key: Current CPU architecture (amd64 or arm64)
  • {{ .OS }}-cache-key: Current operating system (linux or darwin)

Functions available in a template:

checksum: This function takes one or more file paths and computes the SHA256 checksum of the file contents. This is useful for creating unique cache keys based on files that describe content to cache.

Examples of using checksum:

  • cache-key-{{ checksum "package-lock.json" }}
  • cache-key-{{ checksum "**/Package.resolved" }}
  • cache-key-{{ checksum "**/*.gradle*" "gradle.properties" }}

getenv: This function returns the value of an environment variable or an empty string if the variable is not defined.

Examples of getenv:

  • cache-key-{{ getenv "PR" }}
  • cache-key-{{ getenv "BITRISEIO_PIPELINE_ID" }}

Key matching and fallback keys

The most straightforward use case is that a cache archive is downloaded and restored if the provided key matches a cache archive uploaded previously using the Save Cache Step. Stored cache archives are scoped to the Bitrise project. Builds can restore caches saved by any previous Workflow run on any Bitrise Stack.

It's possible to define more than one key in the cache keys input. You can specify additional alternative keys by appending them with || in the same line as the key. The list is in priority order, so the Step will first try to find a match for the first key you provided, and if there is no cache stored for the key, it will move on to find a match for the second key (and so on).

In addition to listing multiple keys, each key can be a prefix of a saved cache key and still get a matching cache archive. For example, the key my-cache- can match an existing archive saved with the key my-cache-a6a102ff.

We recommend configuring the keys in a way that the first key is an exact match to a checksum key, and to use a more generic prefix key as a fallback:

inputs:
  key: |
    multikey_0 || multikey_0_fallback
    multikey

Related steps

Save cache

Similar steps

Saves items to the cache based on key-value pairs. This Step needs to be used in combination with Multikey Restore Cache or Restore Cache.

Restores build cache using a cache key. This Step needs to be used in combination with Save Cache.

Restores cached Carthage dependencies. This Step needs to be used in combination with Save Carthage Cache.

Restores cached Cocoapods dependencies. This Step needs to be used in combination with Save Cocoapods Cache.