Caching in GitLab CI/CD

A cache is one or more files a job downloads and saves. Subsequent jobs that use the same cache don’t have to download the files again, so they execute more quickly.

To learn how to define the cache in your .gitlab-ci.yml file, see the cache reference.

How cache is different from artifacts

Use cache for dependencies, like packages you download from the internet. Cache is stored where GitLab Runner is installed and uploaded to S3 if distributed cache is enabled.

Use artifacts to pass intermediate build results between stages. Artifacts are generated by a job, stored in GitLab, and can be downloaded.

Both artifacts and caches define their paths relative to the project directory, and can’t link to files outside it.

Cache

  • Define cache per job by using the cache keyword. Otherwise it is disabled.
  • Subsequent pipelines can use the cache.
  • Subsequent jobs in the same pipeline can use the cache, if the dependencies are identical.
  • Different projects cannot share the cache.
  • By default, protected and non-protected branches do not share the cache. However, you can change this behavior.

Artifacts

  • Define artifacts per job.
  • Subsequent jobs in later stages of the same pipeline can use artifacts.
  • Different projects cannot share artifacts.
  • Artifacts expire after 30 days by default. You can define a custom expiration time.
  • The latest artifacts do not expire if keep latest artifacts is enabled.
  • Use dependencies to control which jobs fetch the artifacts.

Good caching practices

To ensure maximum availability of the cache, do one or more of the following:

For runners to work with caches efficiently, you must do one of the following:

  • Use a single runner for all your jobs.
  • Use multiple runners that have distributed caching, where the cache is stored in S3 buckets. Shared runners on GitLab.com behave this way. These runners can be in autoscale mode, but they don’t have to be.
  • Use multiple runners with the same architecture and have these runners share a common network-mounted directory to store the cache. This directory should use NFS or something similar. These runners must be in autoscale mode.

Use multiple caches

Version history

You can have a maximum of four caches:

test-job:
  stage: build
  cache:
    - key:
        files:
          - Gemfile.lock
      paths:
        - vendor/ruby
    - key:
        files:
          - yarn.lock
      paths:
        - .yarn-cache/
  script:
    - bundle install --path=vendor
    - yarn install --cache-folder .yarn-cache
    - echo Run tests...

If multiple caches are combined with a fallback cache key, the fallback cache is fetched every time a cache is not found.

Use a fallback cache key

Introduced in GitLab Runner 13.4.

You can use the $CI_COMMIT_REF_SLUG predefined variable to specify your cache:key. For example, if your $CI_COMMIT_REF_SLUG is test, you can set a job to download cache that’s tagged with test.

If a cache with this tag is not found, you can use CACHE_FALLBACK_KEY to specify a cache to use when none exists.

In the following example, if the $CI_COMMIT_REF_SLUG is not found, the job uses the key defined by the CACHE_FALLBACK_KEY variable:

variables:
  CACHE_FALLBACK_KEY: fallback-key

job1:
  script:
    - echo
  cache:
    key: "$CI_COMMIT_REF_SLUG"
    paths:
      - binaries/

Disable cache for specific jobs

If you define the cache globally, each job uses the same definition. You can override this behavior for each job.

To disable it completely for a job, use an empty hash:

job:
  cache: []

Inherit global configuration, but override specific settings per job

You can override cache settings without overwriting the global cache by using anchors. For example, if you want to override the policy for one job:

cache: &global_cache
  key: $CI_COMMIT_REF_SLUG
  paths:
    - node_modules/
    - public/
    - vendor/
  policy: pull-push

job:
  cache:
    # inherit all global cache settings
    <<: *global_cache
    # override the policy
    policy: pull

For more information, see cache: policy.

Common use cases for caches

Usually you use caches to avoid downloading content, like dependencies or libraries, each time you run a job. Node.js packages, PHP packages, Ruby gems, Python libraries, and others can be cached.

For examples, see the GitLab CI/CD templates.

Share caches between jobs in the same branch

To have jobs in each branch use the same cache, define a cache with the key: $CI_COMMIT_REF_SLUG:

cache:
  key: $CI_COMMIT_REF_SLUG

This configuration prevents you from accidentally overwriting the cache. However, the first pipeline for a merge request is slow. The next time a commit is pushed to the branch, the cache is re-used and jobs run faster.

To enable per-job and per-branch caching:

cache:
  key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG"

To enable per-stage and per-branch caching:

cache:
  key: "$CI_JOB_STAGE-$CI_COMMIT_REF_SLUG"

Share caches across jobs in different branches

To share a cache across all branches and all jobs, use the same key for everything:

cache:
  key: one-key-to-rule-them-all

To share a cache between branches, but have a unique cache for each job:

cache:
  key: $CI_JOB_NAME

Cache Node.js dependencies

If your project uses npm to install Node.js dependencies, the following example defines cache globally so that all jobs inherit it. By default, npm stores cache data in the home folder (~/.npm). However, you can’t cache things outside of the project directory. Instead, tell npm to use ./.npm, and cache it per-branch:

#
# https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates/Nodejs.gitlab-ci.yml
#
image: node:latest

# Cache modules in between jobs
cache:
  key: $CI_COMMIT_REF_SLUG
  paths:
    - .npm/

before_script:
  - npm ci --cache .npm --prefer-offline

test_async:
  script:
    - node ./specs/start.js ./specs/async.spec.js

Compute the cache key from the lock file

You can use cache:key:files to compute the cache key from a lock file like package-lock.json or yarn.lock, and reuse it in many jobs.

# Cache modules using lock file
cache:
  key:
    files:
      - package-lock.json
  paths:
    - .npm/

If you’re using Yarn, you can use yarn-offline-mirror to cache the zipped node_modules tarballs. The cache generates more quickly, because fewer files have to be compressed:

job:
  script:
    - echo 'yarn-offline-mirror ".yarn-cache/"' >> .yarnrc
    - echo 'yarn-offline-mirror-pruning true' >> .yarnrc
    - yarn install --frozen-lockfile --no-progress
  cache:
    key:
      files:
        - yarn.lock
    paths:
      - .yarn-cache/

Cache PHP dependencies

If your project uses Composer to install PHP dependencies, the following example defines cache globally so that all jobs inherit it. PHP libraries modules are installed in vendor/ and are cached per-branch:

#
# https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates/PHP.gitlab-ci.yml
#
image: php:7.2

# Cache libraries in between jobs
cache:
  key: $CI_COMMIT_REF_SLUG
  paths:
    - vendor/

before_script:
  # Install and run Composer
  - curl --show-error --silent "https://getcomposer.org/installer" | php
  - php composer.phar install

test:
  script:
    - vendor/bin/phpunit --configuration phpunit.xml --coverage-text --colors=never

Cache Python dependencies

If your project uses pip to install Python dependencies, the following example defines cache globally so that all jobs inherit it. Python libraries are installed in a virtual environment under venv/. pip’s cache is defined under .cache/pip/ and both are cached per-branch:

#
# https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates/Python.gitlab-ci.yml
#
image: python:latest

# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

# Pip's cache doesn't store the python packages
# https://pip.pypa.io/en/stable/reference/pip_install/#caching
#
# If you want to also cache the installed packages, you have to install
# them in a virtualenv and cache it as well.
cache:
  paths:
    - .cache/pip
    - venv/

before_script:
  - python -V               # Print out python version for debugging
  - pip install virtualenv
  - virtualenv venv
  - source venv/bin/activate

test:
  script:
    - python setup.py test
    -