2019-06-07 04:00:41 -04:00
---
2020-05-29 14:08:26 -04:00
stage: Verify
2021-08-31 14:10:24 -04:00
group: Pipeline Authoring
2022-09-21 17:13:33 -04:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments
2019-06-07 04:00:41 -04:00
---
2021-06-21 20:07:53 -04:00
# Caching in GitLab CI/CD **(FREE)**
2017-06-15 05:30:23 -04:00
2022-05-10 08:07:47 -04:00
A cache is one or more files a job downloads and saves. Subsequent jobs that use
2021-05-26 02:10:25 -04:00
the same cache don't have to download the files again, so they execute more quickly.
2017-06-15 05:30:23 -04:00
2021-05-26 02:10:25 -04:00
To learn how to define the cache in your `.gitlab-ci.yml` file,
2021-06-28 17:10:13 -04:00
see the [`cache` reference ](../yaml/index.md#cache ).
2017-06-15 05:30:23 -04:00
2021-05-26 02:10:25 -04:00
## How cache is different from artifacts
2017-06-15 05:30:23 -04:00
2021-05-26 02:10:25 -04:00
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 ](https://docs.gitlab.com/runner/configuration/autoscale.html#distributed-runners-caching ).
2017-06-15 05:30:23 -04:00
2021-05-26 02:10:25 -04:00
Use artifacts to pass intermediate build results between stages.
Artifacts are generated by a job, stored in GitLab, and can be downloaded.
2020-12-08 01:09:38 -05:00
2021-06-17 05:09:53 -04:00
Both artifacts and caches define their paths relative to the project directory, and
can't link to files outside it.
### Cache
2021-12-01 04:10:25 -05:00
- Define cache per job by using the `cache` keyword. Otherwise it is disabled.
2021-06-17 05:09:53 -04:00
- 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.
2022-05-13 23:09:09 -04:00
- By default, protected and non-protected branches [do not share the cache ](#cache-key-names ). However, you can [change this behavior ](#use-the-same-cache-for-all-branches ).
2021-06-17 05:09:53 -04:00
### Artifacts
- Define artifacts per job.
- Subsequent jobs in later stages of the same pipeline can use artifacts.
- Different projects cannot share artifacts.
2021-08-25 23:09:01 -04:00
- Artifacts expire after 30 days by default. You can define a custom [expiration time ](../yaml/index.md#artifactsexpire_in ).
- The latest artifacts do not expire if [keep latest artifacts ](../pipelines/job_artifacts.md#keep-artifacts-from-most-recent-successful-jobs ) is enabled.
- Use [dependencies ](../yaml/index.md#dependencies ) to control which jobs fetch the artifacts.
2019-12-10 19:08:25 -05:00
2017-06-15 05:30:23 -04:00
## Good caching practices
2021-06-17 05:09:53 -04:00
To ensure maximum availability of the cache, do one or more of the following:
2017-06-15 05:30:23 -04:00
2021-09-10 14:10:58 -04:00
- [Tag your runners ](../runners/configure_runners.md#use-tags-to-control-which-jobs-a-runner-can-run ) and use the tag on jobs
2021-06-17 05:09:53 -04:00
that share the cache.
2021-06-10 08:10:09 -04:00
- [Use runners that are only available to a particular project ](../runners/runners_scope.md#prevent-a-specific-runner-from-being-enabled-for-other-projects ).
2021-06-28 17:10:13 -04:00
- [Use a `key` ](../yaml/index.md#cachekey ) that fits your workflow. For example,
2021-06-17 05:09:53 -04:00
you can configure a different cache for each branch.
2017-06-15 05:30:23 -04:00
2020-12-08 01:09:38 -05:00
For runners to work with caches efficiently, you must do one of the following:
2017-06-15 05:30:23 -04:00
2020-09-14 23:09:24 -04:00
- Use a single runner for all your jobs.
2021-06-17 05:09:53 -04:00
- Use multiple runners that have
2017-06-15 05:30:23 -04:00
[distributed caching ](https://docs.gitlab.com/runner/configuration/autoscale.html#distributed-runners-caching ),
2021-06-17 05:09:53 -04:00
where the cache is stored in S3 buckets. Shared runners on GitLab.com behave this way. These runners can be in autoscale mode,
2021-06-17 14:10:22 -04:00
but they don't have to be.
2021-06-17 05:09:53 -04:00
- 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.
2021-06-17 14:10:22 -04:00
These runners must be in autoscale mode.
2017-06-15 05:30:23 -04:00
2021-06-17 14:10:22 -04:00
## Use multiple caches
2017-06-15 05:30:23 -04:00
2021-06-17 14:10:22 -04:00
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/32814) in GitLab 13.10.
> - [Feature Flag removed](https://gitlab.com/gitlab-org/gitlab/-/issues/321877), in GitLab 13.12.
2017-06-15 05:30:23 -04:00
2021-06-17 14:10:22 -04:00
You can have a maximum of four caches:
2017-06-15 05:30:23 -04:00
```yaml
2021-06-17 14:10:22 -04:00
test-job:
stage: build
cache:
- key:
files:
- Gemfile.lock
paths:
- vendor/ruby
- key:
files:
- yarn.lock
paths:
- .yarn-cache/
script:
2022-09-11 23:13:31 -04:00
- bundle config set --local path 'vendor/ruby'
- bundle install
2021-06-17 14:10:22 -04:00
- yarn install --cache-folder .yarn-cache
- echo Run tests...
2017-06-15 05:30:23 -04:00
```
2021-06-26 02:08:19 -04:00
If multiple caches are combined with a fallback cache key,
2021-06-17 14:10:22 -04:00
the fallback cache is fetched every time a cache is not found.
2017-06-15 05:30:23 -04:00
2021-06-26 02:08:19 -04:00
## Use a fallback cache key
2017-06-15 05:30:23 -04:00
2021-06-17 14:10:22 -04:00
> [Introduced](https://gitlab.com/gitlab-org/gitlab-runner/-/merge_requests/1534) in GitLab Runner 13.4.
2017-06-15 05:30:23 -04:00
2021-06-17 14:10:22 -04:00
You can use the `$CI_COMMIT_REF_SLUG` [predefined variable ](../variables/predefined_variables.md )
2021-06-28 17:10:13 -04:00
to specify your [`cache:key` ](../yaml/index.md#cachekey ). For example, if your
2021-06-26 02:08:19 -04:00
`$CI_COMMIT_REF_SLUG` is `test` , you can set a job to download cache that's tagged with `test` .
2017-06-15 05:30:23 -04:00
2021-06-17 14:10:22 -04:00
If a cache with this tag is not found, you can use `CACHE_FALLBACK_KEY` to
specify a cache to use when none exists.
2017-06-15 05:30:23 -04:00
2021-06-17 14:10:22 -04:00
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:
2017-06-15 05:30:23 -04:00
```yaml
2021-06-17 14:10:22 -04:00
variables:
CACHE_FALLBACK_KEY: fallback-key
job1:
script:
- echo
cache:
key: "$CI_COMMIT_REF_SLUG"
paths:
- binaries/
2017-06-15 05:30:23 -04:00
```
2021-06-17 14:10:22 -04:00
## Disable cache for specific jobs
2017-06-15 05:30:23 -04:00
2021-06-26 02:08:19 -04:00
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:
2017-06-15 05:30:23 -04:00
```yaml
job:
2021-08-11 08:10:59 -04:00
cache: []
2017-06-15 05:30:23 -04:00
```
2021-06-17 14:10:22 -04:00
## Inherit global configuration, but override specific settings per job
2019-08-27 02:10:44 -04:00
You can override cache settings without overwriting the global cache by using
2021-11-15 10:10:57 -05:00
[anchors ](../yaml/yaml_optimization.md#anchors ). For example, if you want to override the
2019-08-27 02:10:44 -04:00
`policy` for one job:
```yaml
cache: & global_cache
2021-06-17 14:10:22 -04:00
key: $CI_COMMIT_REF_SLUG
2020-09-08 14:08:48 -04:00
paths:
- node_modules/
- public/
- vendor/
policy: pull-push
2019-08-27 02:10:44 -04:00
job:
cache:
# inherit all global cache settings
< < : * global_cache
# override the policy
policy: pull
```
2021-06-28 17:10:13 -04:00
For more information, see [`cache: policy` ](../yaml/index.md#cachepolicy ).
2017-06-15 05:30:23 -04:00
2021-06-26 02:08:19 -04:00
## Common use cases for caches
2017-06-15 05:30:23 -04:00
2021-06-26 02:08:19 -04:00
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.
2017-06-15 05:30:23 -04:00
2021-06-26 02:08:19 -04:00
For examples, see the [GitLab CI/CD templates ](https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates ).
2017-06-15 05:30:23 -04:00
2021-06-17 14:10:22 -04:00
### Share caches between jobs in the same branch
2021-06-26 02:08:19 -04:00
To have jobs in each branch use the same cache, define a cache with the `key: $CI_COMMIT_REF_SLUG` :
2021-06-17 14:10:22 -04:00
```yaml
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:
```yaml
cache:
key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG"
```
To enable per-stage and per-branch caching:
```yaml
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:
```yaml
cache:
key: one-key-to-rule-them-all
```
2021-06-26 02:08:19 -04:00
To share a cache between branches, but have a unique cache for each job:
2021-06-17 14:10:22 -04:00
```yaml
cache:
key: $CI_JOB_NAME
```
2020-12-08 01:09:38 -05:00
### Cache Node.js dependencies
2017-06-15 05:30:23 -04:00
2021-06-26 02:08:19 -04:00
If your project uses [npm ](https://www.npmjs.com/ ) to install Node.js
2020-03-02 01:08:01 -05:00
dependencies, the following example defines `cache` globally so that all jobs inherit it.
2021-06-26 02:08:19 -04:00
By default, npm stores cache data in the home folder (`~/.npm`). However, you
2021-06-28 17:10:13 -04:00
[can't cache things outside of the project directory ](../yaml/index.md#cachepaths ).
2021-06-26 02:08:19 -04:00
Instead, tell npm to use `./.npm` , and cache it per-branch:
2017-06-15 05:30:23 -04:00
```yaml
#
2021-06-08 14:10:23 -04:00
# https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates/Nodejs.gitlab-ci.yml
2017-06-15 05:30:23 -04:00
#
image: node:latest
# Cache modules in between jobs
cache:
2021-06-17 14:10:22 -04:00
key: $CI_COMMIT_REF_SLUG
2017-06-15 05:30:23 -04:00
paths:
2020-07-03 05:08:53 -04:00
- .npm/
2017-06-15 05:30:23 -04:00
before_script:
2020-03-02 01:08:01 -05:00
- npm ci --cache .npm --prefer-offline
2017-06-15 05:30:23 -04:00
test_async:
script:
2020-07-03 05:08:53 -04:00
- node ./specs/start.js ./specs/async.spec.js
2017-06-15 05:30:23 -04:00
```
2022-01-10 22:16:07 -05:00
#### Compute the cache key from the lock file
You can use [`cache:key:files` ](../yaml/index.md#cachekeyfiles ) to compute the cache
key from a lock file like `package-lock.json` or `yarn.lock` , and reuse it in many jobs.
```yaml
# Cache modules using lock file
cache:
key:
files:
- package-lock.json
paths:
- .npm/
```
If you're using [Yarn ](https://yarnpkg.com/ ), you can use [`yarn-offline-mirror` ](https://classic.yarnpkg.com/blog/2016/11/24/offline-mirror/ )
to cache the zipped `node_modules` tarballs. The cache generates more quickly, because
fewer files have to be compressed:
```yaml
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/
```
2021-05-26 02:10:25 -04:00
### Cache PHP dependencies
2017-06-15 05:30:23 -04:00
2021-06-26 02:08:19 -04:00
If your project uses [Composer ](https://getcomposer.org/ ) to install
PHP dependencies, the following example defines `cache` globally so that
2017-06-15 05:30:23 -04:00
all jobs inherit it. PHP libraries modules are installed in `vendor/` and
are cached per-branch:
```yaml
#
2021-06-08 14:10:23 -04:00
# https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates/PHP.gitlab-ci.yml
2017-06-15 05:30:23 -04:00
#
image: php:7.2
# Cache libraries in between jobs
cache:
2021-06-17 14:10:22 -04:00
key: $CI_COMMIT_REF_SLUG
2017-06-15 05:30:23 -04:00
paths:
2020-07-03 05:08:53 -04:00
- vendor/
2017-06-15 05:30:23 -04:00
before_script:
2020-07-03 05:08:53 -04:00
# Install and run Composer
2020-12-08 01:09:38 -05:00
- curl --show-error --silent "https://getcomposer.org/installer" | php
2020-07-03 05:08:53 -04:00
- php composer.phar install
2017-06-15 05:30:23 -04:00
test:
script:
2020-07-03 05:08:53 -04:00
- vendor/bin/phpunit --configuration phpunit.xml --coverage-text --colors=never
2017-06-15 05:30:23 -04:00
```
2021-05-26 02:10:25 -04:00
### Cache Python dependencies
2017-06-15 05:30:23 -04:00
2021-06-26 02:08:19 -04:00
If your project uses [pip ](https://pip.pypa.io/en/stable/ ) to install
Python dependencies, the following example defines `cache` globally so that
2022-08-22 02:09:36 -04:00
all jobs inherit it. pip's cache is defined under `.cache/pip/` and is cached per-branch:
2017-06-15 05:30:23 -04:00
```yaml
#
2021-06-08 14:10:23 -04:00
# https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates/Python.gitlab-ci.yml
2017-06-15 05:30:23 -04:00
#
image: python:latest
# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
2020-09-08 14:08:48 -04:00
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
2017-06-15 05:30:23 -04:00
# Pip's cache doesn't store the python packages
# https://pip.pypa.io/en/stable/reference/pip_install/#caching
cache:
paths:
2018-10-09 01:49:15 -04:00
- .cache/pip
2017-06-15 05:30:23 -04:00
before_script:
- python -V # Print out python version for debugging
- pip install virtualenv
- virtualenv venv
- source venv/bin/activate
test:
script:
2020-07-03 05:08:53 -04:00
- python setup.py test
- pip install flake8
- flake8 .
2017-06-15 05:30:23 -04:00
```
2021-05-26 02:10:25 -04:00
### Cache Ruby dependencies
2017-06-15 05:30:23 -04:00
2021-06-26 02:08:19 -04:00
If your project uses [Bundler ](https://bundler.io ) to install
2017-06-15 05:30:23 -04:00
gem dependencies, the following example defines `cache` globally so that all
jobs inherit it. Gems are installed in `vendor/ruby/` and are cached per-branch:
```yaml
#
2021-06-08 14:10:23 -04:00
# https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates/Ruby.gitlab-ci.yml
2017-06-15 05:30:23 -04:00
#
2019-05-06 06:35:35 -04:00
image: ruby:2.6
2017-06-15 05:30:23 -04:00
# Cache gems in between builds
cache:
2021-06-17 14:10:22 -04:00
key: $CI_COMMIT_REF_SLUG
2017-06-15 05:30:23 -04:00
paths:
- vendor/ruby
before_script:
2020-01-20 01:08:35 -05:00
- ruby -v # Print out ruby version for debugging
2022-09-11 23:13:31 -04:00
- bundle config set --local path 'vendor/ruby' # The location to install the specified gems to
- bundle install -j $(nproc) # Install dependencies into ./vendor/ruby
2017-06-15 05:30:23 -04:00
rspec:
script:
2020-07-03 05:08:53 -04:00
- rspec spec
2017-06-15 05:30:23 -04:00
```
2021-06-26 02:08:19 -04:00
If you have jobs that need different gems, use the `prefix`
2020-12-16 22:10:36 -05:00
keyword in the global `cache` definition. This configuration generates a different
cache for each job.
For example, a testing job might not need the same gems as a job that deploys to
production:
```yaml
cache:
key:
files:
- Gemfile.lock
2021-06-17 14:10:22 -04:00
prefix: $CI_JOB_NAME
2020-12-16 22:10:36 -05:00
paths:
- vendor/ruby
test_job:
stage: test
before_script:
2022-09-11 23:13:31 -04:00
- bundle config set --local path 'vendor/ruby'
- bundle install --without production
2020-12-16 22:10:36 -05:00
script:
- bundle exec rspec
deploy_job:
stage: production
before_script:
2022-09-11 23:13:31 -04:00
- bundle config set --local path 'vendor/ruby' # The location to install the specified gems to
- bundle install --without test
2020-12-16 22:10:36 -05:00
script:
- bundle exec deploy
```
2021-05-26 02:10:25 -04:00
### Cache Go dependencies
2020-04-07 05:09:13 -04:00
2021-06-26 02:08:19 -04:00
If your project uses [Go Modules ](https://github.com/golang/go/wiki/Modules ) to install
2020-04-07 05:09:13 -04:00
Go dependencies, the following example defines `cache` in a `go-cache` template, that
any job can extend. Go modules are installed in `${GOPATH}/pkg/mod/` and
are cached for all of the `go` projects:
```yaml
.go-cache:
variables:
GOPATH: $CI_PROJECT_DIR/.go
before_script:
- mkdir -p .go
cache:
paths:
- .go/pkg/mod/
test:
image: golang:1.13
extends: .go-cache
script:
2020-07-03 05:08:53 -04:00
- go test ./... -v -short
2020-04-07 05:09:13 -04:00
```
2017-06-15 05:30:23 -04:00
## Availability of the cache
2021-06-26 02:08:19 -04:00
Caching is an optimization, but it isn't guaranteed to always work. You might need
to regenerate cached files in each job that needs them.
2017-06-15 05:30:23 -04:00
2021-06-28 17:10:13 -04:00
After you define a [cache in `.gitlab-ci.yml` ](../yaml/index.md#cache ),
2020-12-08 01:09:38 -05:00
the availability of the cache depends on:
2021-06-26 02:08:19 -04:00
- The runner's executor type.
2020-12-08 01:09:38 -05:00
- Whether different runners are used to pass the cache between jobs.
2017-06-15 05:30:23 -04:00
### Where the caches are stored
2021-06-26 02:08:19 -04:00
All caches defined for a job are archived in a single `cache.zip` file.
The runner configuration defines where the file is stored. By default, the cache
is stored on the machine where GitLab Runner is installed. The location also depends on the type of executor.
2017-06-15 05:30:23 -04:00
2021-06-26 02:08:19 -04:00
| Runner executor | Default path of the cache |
2017-06-15 05:30:23 -04:00
| ---------------------- | ------------------------- |
2021-06-26 02:08:19 -04:00
| [Shell ](https://docs.gitlab.com/runner/executors/shell.html ) | Locally, under the `gitlab-runner` user's home directory: `/home/gitlab-runner/cache/<user>/<project>/<cache-key>/cache.zip` . |
| [Docker ](https://docs.gitlab.com/runner/executors/docker.html ) | Locally, under [Docker volumes ](https://docs.gitlab.com/runner/executors/docker.html#the-builds-and-cache-storage ): `/var/lib/docker/volumes/<volume-id>/_data/<user>/<project>/<cache-key>/cache.zip` . |
| [Docker Machine ](https://docs.gitlab.com/runner/executors/docker_machine.html ) (autoscale runners) | The same as the Docker executor. |
2017-06-15 05:30:23 -04:00
2021-05-26 02:10:25 -04:00
If you use cache and artifacts to store the same path in your jobs, the cache might
be overwritten because caches are restored before artifacts.
2022-05-03 05:07:47 -04:00
#### Cache key names
2022-05-02 20:08:25 -04:00
2022-05-03 05:07:47 -04:00
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/330047) in GitLab 15.0.
2022-05-02 20:08:25 -04:00
A suffix is added to the cache key, with the exception of the [fallback cache key ](#use-a-fallback-cache-key ).
As an example, assuming that `cache.key` is set to `$CI_COMMIT_REF_SLUG` , and that we have two branches `main`
and `feature` , then the following table represents the resulting cache keys:
| Branch name | Cache key |
|-------------|-----------|
| `main` | `main-protected` |
| `feature` | `feature-non_protected` |
2022-05-13 23:09:09 -04:00
##### Use the same cache for all branches
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/361643) in GitLab 15.0.
If you do not want to use [cache key names ](#cache-key-names ),
you can have all branches (protected and unprotected) use the same cache.
The cache separation with [cache key names ](#cache-key-names ) is a security feature
and should only be disabled in an environment where all users with Developer role are highly trusted.
To use the same cache for all branches:
2022-09-14 14:12:48 -04:00
1. On the top bar, select **Main menu > Projects** and find your project.
2022-05-13 23:09:09 -04:00
1. On the left sidebar, select **Settings > CI/CD** .
1. Expand **General pipelines** .
1. Clear the **Use separate caches for protected branches** checkbox.
1. Select **Save changes** .
2017-06-15 05:30:23 -04:00
### How archiving and extracting works
2021-06-26 02:08:19 -04:00
This example shows two jobs in two consecutive stages:
2017-06-15 05:30:23 -04:00
```yaml
stages:
2020-07-03 05:08:53 -04:00
- build
- test
2017-06-15 05:30:23 -04:00
before_script:
2020-07-03 05:08:53 -04:00
- echo "Hello"
2017-06-15 05:30:23 -04:00
job A:
stage: build
script:
2020-07-03 05:08:53 -04:00
- mkdir vendor/
- echo "build" > vendor/hello.txt
2017-06-15 05:30:23 -04:00
cache:
key: build-cache
paths:
2020-07-03 05:08:53 -04:00
- vendor/
2017-06-15 05:30:23 -04:00
after_script:
2020-07-03 05:08:53 -04:00
- echo "World"
2017-06-15 05:30:23 -04:00
job B:
stage: test
script:
2020-07-03 05:08:53 -04:00
- cat vendor/hello.txt
2017-06-15 05:30:23 -04:00
cache:
key: build-cache
2020-11-05 10:09:04 -05:00
paths:
- vendor/
2017-06-15 05:30:23 -04:00
```
2021-06-26 02:08:19 -04:00
If one machine has one runner installed, then all jobs for your project
2020-12-08 01:09:38 -05:00
run on the same host:
2017-06-15 05:30:23 -04:00
2018-09-06 23:01:20 -04:00
1. Pipeline starts.
1. `job A` runs.
1. `before_script` is executed.
1. `script` is executed.
1. `after_script` is executed.
2017-06-15 05:30:23 -04:00
1. `cache` runs and the `vendor/` directory is zipped into `cache.zip` .
2019-08-12 05:53:30 -04:00
This file is then saved in the directory based on the
2020-09-14 23:09:24 -04:00
[runner's setting ](#where-the-caches-are-stored ) and the `cache: key` .
2018-09-06 23:01:20 -04:00
1. `job B` runs.
1. The cache is extracted (if found).
1. `before_script` is executed.
1. `script` is executed.
1. Pipeline finishes.
2017-06-15 05:30:23 -04:00
2020-11-18 10:09:08 -05:00
By using a single runner on a single machine, you don't have the issue where
2020-12-08 01:09:38 -05:00
`job B` might execute on a runner different from `job A` . This setup guarantees the
cache can be reused between stages. It only works if the execution goes from the `build` stage
to the `test` stage in the same runner/machine. Otherwise, the cache [might not be available ](#cache-mismatch ).
2017-06-15 05:30:23 -04:00
During the caching process, there's also a couple of things to consider:
- If some other job, with another cache configuration had saved its
cache in the same zip file, it is overwritten. If the S3 based shared cache is
used, the file is additionally uploaded to S3 to an object based on the cache
2020-11-18 10:09:08 -05:00
key. So, two jobs with different paths, but the same cache key, overwrites
2017-06-15 05:30:23 -04:00
their cache.
- When extracting the cache from `cache.zip` , everything in the zip file is
extracted in the job's working directory (usually the repository which is
2020-09-14 23:09:24 -04:00
pulled down), and the runner doesn't mind if the archive of `job A` overwrites
2017-06-15 05:30:23 -04:00
things in the archive of `job B` .
2020-12-08 01:09:38 -05:00
It works this way because the cache created for one runner
often isn't valid when used by a different one. A different runner may run on a
different architecture (for example, when the cache includes binary files). Also,
because the different steps might be executed by runners running on different
2017-06-15 05:30:23 -04:00
machines, it is a safe default.
2021-06-26 02:08:19 -04:00
## Clearing the cache
2021-06-28 17:10:13 -04:00
Runners use [cache ](../yaml/index.md#cache ) to speed up the execution
2021-06-26 02:08:19 -04:00
of your jobs by reusing existing data. This can sometimes lead to
inconsistent behavior.
There are two ways to start with a fresh copy of the cache.
### Clear the cache by changing `cache:key`
Change the value for `cache: key` in your `.gitlab-ci.yml` file.
The next time the pipeline runs, the cache is stored in a different location.
### Clear the cache manually
You can clear the cache in the GitLab UI:
2022-09-14 14:12:48 -04:00
1. On the top bar, select **Main menu > Projects** and find your project.
2022-05-10 08:07:47 -04:00
1. On the left sidebar, select **CI/CD > Pipelines** .
2021-06-26 02:08:19 -04:00
1. In the top right, select **Clear runner caches** .
On the next commit, your CI/CD jobs use a new cache.
NOTE:
Each time you clear the cache manually, the [internal cache name ](#where-the-caches-are-stored ) is updated. The name uses the format `cache-<index>` , and the index increments by one. The old cache is not deleted. You can manually delete these files from the runner storage.
## Troubleshooting
2017-06-15 05:30:23 -04:00
### Cache mismatch
2021-06-26 02:08:19 -04:00
If you have a cache mismatch, follow these steps to troubleshoot.
2017-06-15 05:30:23 -04:00
2021-06-26 02:08:19 -04:00
| Reason for a cache mismatch | How to fix it |
| --------------------------- | ------------- |
| You use multiple standalone runners (not in autoscale mode) attached to one project without a shared cache. | Use only one runner for your project or use multiple runners with distributed cache enabled. |
| You use runners in autoscale mode without a distributed cache enabled. | Configure the autoscale runner to use a distributed cache. |
| The machine the runner is installed on is low on disk space or, if you've set up distributed cache, the S3 bucket where the cache is stored doesn't have enough space. | Make sure you clear some space to allow new caches to be stored. There's no automatic way to do this. |
2022-05-10 08:07:47 -04:00
| You use the same `key` for jobs where they cache different paths. | Use different cache keys so that the cache archive is stored to a different location and doesn't overwrite wrong caches. |
2017-06-15 05:30:23 -04:00
2021-06-26 02:08:19 -04:00
#### Cache mismatch example 1
2017-06-15 05:30:23 -04:00
2021-06-26 02:08:19 -04:00
If you have only one runner assigned to your project, the cache
is stored on the runner's machine by default.
2017-06-15 05:30:23 -04:00
2021-06-26 02:08:19 -04:00
If two jobs have the same cache key but a different path, the caches can be overwritten.
For example:
2017-06-15 05:30:23 -04:00
```yaml
stages:
2020-07-03 05:08:53 -04:00
- build
- test
2017-06-15 05:30:23 -04:00
job A:
stage: build
script: make build
cache:
key: same-key
paths:
2020-07-03 05:08:53 -04:00
- public/
2017-06-15 05:30:23 -04:00
job B:
stage: test
script: make test
cache:
key: same-key
paths:
2020-07-03 05:08:53 -04:00
- vendor/
2017-06-15 05:30:23 -04:00
```
2018-09-06 23:01:20 -04:00
1. `job A` runs.
1. `public/` is cached as cache.zip.
1. `job B` runs.
1. The previous cache, if any, is unzipped.
1. `vendor/` is cached as cache.zip and overwrites the previous one.
2020-11-18 10:09:08 -05:00
1. The next time `job A` runs it uses the cache of `job B` which is different
and thus isn't effective.
2017-06-15 05:30:23 -04:00
2021-06-26 02:08:19 -04:00
To fix this issue, use different `keys` for each job.
#### Cache mismatch example 2
In this example, you have more than one runner assigned to your
project, and distributed cache is not enabled.
2017-06-15 05:30:23 -04:00
2021-06-26 02:08:19 -04:00
The second time the pipeline runs, you want `job A` and `job B` to re-use their cache (which in this case
2020-11-18 10:09:08 -05:00
is different):
2017-06-15 05:30:23 -04:00
```yaml
stages:
2020-07-03 05:08:53 -04:00
- build
- test
2017-06-15 05:30:23 -04:00
job A:
stage: build
script: build
cache:
key: keyA
paths:
2020-07-03 05:08:53 -04:00
- vendor/
2017-06-15 05:30:23 -04:00
job B:
stage: test
script: test
cache:
key: keyB
paths:
2020-07-03 05:08:53 -04:00
- vendor/
2017-06-15 05:30:23 -04:00
```
2020-12-08 01:09:38 -05:00
Even if the `key` is different, the cached files might get "cleaned" before each
2021-06-26 02:08:19 -04:00
stage if the jobs run on different runners in subsequent pipelines.