Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'doc/ci/caching/index.md')
-rw-r--r--doc/ci/caching/index.md367
1 files changed, 204 insertions, 163 deletions
diff --git a/doc/ci/caching/index.md b/doc/ci/caching/index.md
index 0778d598d32..0ed1e978168 100644
--- a/doc/ci/caching/index.md
+++ b/doc/ci/caching/index.md
@@ -5,13 +5,13 @@ info: To determine the technical writer assigned to the Stage/Group associated w
type: index, concepts, howto
---
-# Caching in GitLab CI/CD
+# Caching in GitLab CI/CD **(FREE)**
A cache is one or more files that 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](../yaml/README.md#cache).
+see the [`cache` reference](../yaml/index.md#cache).
## How cache is different from artifacts
@@ -19,114 +19,127 @@ 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).
-- You can define it per job by using the `cache:` keyword. Otherwise it is disabled.
-- You can define it per job so that:
- - Subsequent pipelines can use it.
- - Subsequent jobs in the same pipeline can use it, if the dependencies are identical.
-- You cannot share it between projects.
-
Use artifacts to pass intermediate build results between stages.
Artifacts are generated by a job, stored in GitLab, and can be downloaded.
-- You can define artifacts per job. Subsequent jobs in later stages of the same
- pipeline can use them.
-- You can't use the artifacts in a different pipeline.
-
-Artifacts expire after 30 days unless you define an [expiration time](../yaml/README.md#artifactsexpire_in).
-Use [dependencies](../yaml/README.md#dependencies) to control which jobs fetch the artifacts.
-
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.
+
+### 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 unless you define an [expiration time](../yaml/index.md#artifactsexpire_in).
+Use [dependencies](../yaml/index.md#dependencies) to control which jobs fetch the artifacts.
+
## Good caching practices
-To ensure maximum availability of the cache, when you declare `cache` in your jobs,
-use one or more of the following:
+To ensure maximum availability of the cache, do one or more of the following:
- [Tag your runners](../runners/configure_runners.md#use-tags-to-limit-the-number-of-jobs-using-the-runner) and use the tag on jobs
- that share their cache.
+ that share the cache.
- [Use runners that are only available to a particular project](../runners/runners_scope.md#prevent-a-specific-runner-from-being-enabled-for-other-projects).
-- [Use a `key`](../yaml/README.md#cachekey) that fits your workflow (for example,
- different caches on each branch). For that, you can take advantage of the
- [predefined CI/CD variables](../variables/README.md#predefined-cicd-variables).
+- [Use a `key`](../yaml/index.md#cachekey) that fits your workflow. For example,
+ you can configure a different cache for each branch.
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 (in autoscale mode or not) that use
+- Use multiple runners that have
[distributed caching](https://docs.gitlab.com/runner/configuration/autoscale.html#distributed-runners-caching),
- where the cache is stored in S3 buckets (like shared runners on GitLab.com).
-- Use multiple runners (not in autoscale mode) of the same architecture that
- share a common network-mounted directory (using NFS or something similar)
- where the cache is stored.
+ 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.
-Read about the [availability of the cache](#availability-of-the-cache)
-to learn more about the internals and get a better idea how cache works.
+## Use multiple caches
-### Share caches between jobs in the same branch
+> - [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.
-Define a cache with the `key: ${CI_COMMIT_REF_SLUG}` so that jobs of each
-branch always use the same cache:
+You can have a maximum of four caches:
```yaml
-cache:
- key: ${CI_COMMIT_REF_SLUG}
+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...
```
-This configuration is safe from accidentally overwriting the cache, but merge requests
-get slow first pipelines. The next time a new commit is pushed to the branch, the
-cache is re-used and jobs run faster.
+If multiple caches are combined with a fallback cache key,
+the fallback cache is fetched every time a cache is not found.
-To enable per-job and per-branch caching:
+## Use a fallback cache key
-```yaml
-cache:
- key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG"
-```
+> [Introduced](https://gitlab.com/gitlab-org/gitlab-runner/-/merge_requests/1534) in GitLab Runner 13.4.
-To enable per-stage and per-branch caching:
-
-```yaml
-cache:
- key: "$CI_JOB_STAGE-$CI_COMMIT_REF_SLUG"
-```
+You can use the `$CI_COMMIT_REF_SLUG` [predefined variable](../variables/predefined_variables.md)
+to specify your [`cache:key`](../yaml/index.md#cachekey). For example, if your
+`$CI_COMMIT_REF_SLUG` is `test`, you can set a job to download cache that's tagged with `test`.
-### Share caches across jobs in different branches
+If a cache with this tag is not found, you can use `CACHE_FALLBACK_KEY` to
+specify a cache to use when none exists.
-To share a cache across all branches and all jobs, use the same key for everything:
+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:
```yaml
-cache:
- key: one-key-to-rule-them-all
-```
-
-To share caches between branches, but have a unique cache for each job:
+variables:
+ CACHE_FALLBACK_KEY: fallback-key
-```yaml
-cache:
- key: ${CI_JOB_NAME}
+job1:
+ script:
+ - echo
+ cache:
+ key: "$CI_COMMIT_REF_SLUG"
+ paths:
+ - binaries/
```
-### Disable cache for specific jobs
+## 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.
-If you have defined the cache globally, it means that each job uses the
-same definition. You can override this behavior per-job, and if you want to
-disable it completely, use an empty hash:
+To disable it completely for a job, use an empty hash:
```yaml
job:
cache: {}
```
-### Inherit global configuration, but override specific settings per job
+## Inherit global configuration, but override specific settings per job
You can override cache settings without overwriting the global cache by using
-[anchors](../yaml/README.md#anchors). For example, if you want to override the
+[anchors](../yaml/index.md#anchors). For example, if you want to override the
`policy` for one job:
```yaml
cache: &global_cache
- key: ${CI_COMMIT_REF_SLUG}
+ key: $CI_COMMIT_REF_SLUG
paths:
- node_modules/
- public/
@@ -141,24 +154,66 @@ job:
policy: pull
```
-For more fine tuning, read also about the
-[`cache: policy`](../yaml/README.md#cachepolicy).
+For more information, see [`cache: policy`](../yaml/index.md#cachepolicy).
+
+## 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](https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/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`:
+
+```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.
-## Common use cases
+To enable per-job and per-branch caching:
-The most common use case of caching is to avoid downloading content like dependencies
-or libraries repeatedly between subsequent runs of jobs. Node.js packages,
-PHP packages, Ruby gems, Python libraries, and others can all be cached.
+```yaml
+cache:
+ key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG"
+```
-For more examples, check out our [GitLab CI/CD templates](https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates).
+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
+```
+
+To share a cache between branches, but have a unique cache for each job:
+
+```yaml
+cache:
+ key: $CI_JOB_NAME
+```
### Cache Node.js dependencies
-If your project is using [npm](https://www.npmjs.com/) to install the Node.js
+If your project uses [npm](https://www.npmjs.com/) 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` but you
-[can't cache things outside of the project directory](../yaml/README.md#cachepaths).
-Instead, we tell npm to use `./.npm`, and cache it per-branch:
+By default, npm stores cache data in the home folder (`~/.npm`). However, you
+[can't cache things outside of the project directory](../yaml/index.md#cachepaths).
+Instead, tell npm to use `./.npm`, and cache it per-branch:
```yaml
#
@@ -168,7 +223,7 @@ image: node:latest
# Cache modules in between jobs
cache:
- key: ${CI_COMMIT_REF_SLUG}
+ key: $CI_COMMIT_REF_SLUG
paths:
- .npm/
@@ -182,8 +237,8 @@ test_async:
### Cache PHP dependencies
-Assuming your project is using [Composer](https://getcomposer.org/) to install
-the PHP dependencies, the following example defines `cache` globally so that
+If your project uses [Composer](https://getcomposer.org/) 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:
@@ -195,7 +250,7 @@ image: php:7.2
# Cache libraries in between jobs
cache:
- key: ${CI_COMMIT_REF_SLUG}
+ key: $CI_COMMIT_REF_SLUG
paths:
- vendor/
@@ -211,9 +266,9 @@ test:
### Cache Python dependencies
-Assuming your project is using [pip](https://pip.pypa.io/en/stable/) to install
-the Python dependencies, the following example defines `cache` globally so that
-all jobs inherit it. Python libraries are installed in a virtual environment under `venv/`,
+If your project uses [pip](https://pip.pypa.io/en/stable/) 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:
```yaml
@@ -252,7 +307,7 @@ test:
### Cache Ruby dependencies
-Assuming your project is using [Bundler](https://bundler.io) to install the
+If your project uses [Bundler](https://bundler.io) to install
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:
@@ -264,7 +319,7 @@ image: ruby:2.6
# Cache gems in between builds
cache:
- key: ${CI_COMMIT_REF_SLUG}
+ key: $CI_COMMIT_REF_SLUG
paths:
- vendor/ruby
@@ -277,7 +332,7 @@ rspec:
- rspec spec
```
-If you have jobs that each need a different selection of gems, use the `prefix`
+If you have jobs that need different gems, use the `prefix`
keyword in the global `cache` definition. This configuration generates a different
cache for each job.
@@ -289,7 +344,7 @@ cache:
key:
files:
- Gemfile.lock
- prefix: ${CI_JOB_NAME}
+ prefix: $CI_JOB_NAME
paths:
- vendor/ruby
@@ -310,7 +365,7 @@ deploy_job:
### Cache Go dependencies
-Assuming your project is using [Go Modules](https://github.com/golang/go/wiki/Modules) to install
+If your project uses [Go Modules](https://github.com/golang/go/wiki/Modules) to install
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:
@@ -334,35 +389,33 @@ test:
## Availability of the cache
-Caching is an optimization, but it isn't guaranteed to always work. You need to
-be prepared to regenerate any cached files in each job that needs them.
+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.
-After you have defined a [cache in `.gitlab-ci.yml`](../yaml/README.md#cache),
+After you define a [cache in `.gitlab-ci.yml`](../yaml/index.md#cache),
the availability of the cache depends on:
-- The runner's executor type
+- The runner's executor type.
- Whether different runners are used to pass the cache between jobs.
### Where the caches are stored
-The runner is responsible for storing the cache, so it's essential
-to know **where** it's stored. All the cache paths defined under a job in
-`.gitlab-ci.yml` are archived in a single `cache.zip` file and stored in the
-runner's configured cache location. By default, they are stored locally in the
-machine where the runner is installed and depends on the type of the executor.
+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.
-| GitLab Runner executor | Default path of the cache |
+| Runner executor | Default path of the cache |
| ---------------------- | ------------------------- |
-| [Shell](https://docs.gitlab.com/runner/executors/shell.html) | Locally, stored 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, stored 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) | Behaves the same as the Docker executor. |
+| [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. |
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.
### How archiving and extracting works
-This example has two jobs that belong to two consecutive stages:
+This example shows two jobs in two consecutive stages:
```yaml
stages:
@@ -394,7 +447,7 @@ job B:
- vendor/
```
-If you have one machine with one runner installed, and all jobs for your project
+If one machine has one runner installed, then all jobs for your project
run on the same host:
1. Pipeline starts.
@@ -434,27 +487,54 @@ different architecture (for example, when the cache includes binary files). Also
because the different steps might be executed by runners running on different
machines, it is a safe default.
+## Clearing the cache
+
+Runners use [cache](../yaml/index.md#cache) to speed up the execution
+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
+
+> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/41249) in GitLab 10.4.
+
+You can clear the cache in the GitLab UI:
+
+1. On the top bar, select **Menu > Projects** and find your project.
+1. On the left sidebar, select **CI/CD > Pipelines** page.
+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
+
### Cache mismatch
-In the following table, you can see some reasons where you might hit a cache
-mismatch and a few ideas how to fix it.
+If you have a cache mismatch, follow these steps to troubleshoot.
-| Reason of 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. |
+| 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. |
| You use the same `key` for jobs where they cache different paths. | Use different cache keys to that the cache archive is stored to a different location and doesn't overwrite wrong caches. |
-Let's explore some examples.
-
-#### Examples
+#### Cache mismatch example 1
-Let's assume you have only one runner assigned to your project, so the cache
-is stored in the runner's machine by default.
+If you have only one runner assigned to your project, the cache
+is stored on the runner's machine by default.
-Two jobs could cause caches to be overwritten if they have the same cache key, but
-they cache a different path:
+If two jobs have the same cache key but a different path, the caches can be overwritten.
+For example:
```yaml
stages:
@@ -486,11 +566,14 @@ job B:
1. The next time `job A` runs it uses the cache of `job B` which is different
and thus isn't effective.
-To fix that, use different `keys` for each job.
+To fix this issue, use different `keys` for each job.
-In another case, let's assume you have more than one runner assigned to your
-project, but the distributed cache is not enabled. The second time the
-pipeline is run, we want `job A` and `job B` to re-use their cache (which in this case
+#### Cache mismatch example 2
+
+In this example, you have more than one runner assigned to your
+project, and distributed cache is not enabled.
+
+The second time the pipeline runs, you want `job A` and `job B` to re-use their cache (which in this case
is different):
```yaml
@@ -516,46 +599,4 @@ job B:
```
Even if the `key` is different, the cached files might get "cleaned" before each
-stage if the jobs run on different runners in the subsequent pipelines.
-
-## Clearing the cache
-
-Runners use [cache](../yaml/README.md#cache) to speed up the execution
-of your jobs by reusing existing data. This however, can sometimes lead to an
-inconsistent behavior.
-
-To start with a fresh copy of the cache, there are two ways to do that.
-
-### Clearing the cache by changing `cache:key`
-
-All you have to do is set a new `cache: key` in your `.gitlab-ci.yml`. In the
-next run of the pipeline, the cache is stored in a different location.
-
-### Clearing the cache manually
-
-> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/41249) in GitLab 10.4.
-
-If you want to avoid editing `.gitlab-ci.yml`, you can clear the cache
-via the GitLab UI:
-
-1. Navigate to your project's **CI/CD > Pipelines** page.
-1. Click on the **Clear runner caches** button to clean up the cache.
-
- ![Clear runner caches](img/clear_runners_cache.png)
-
-1. On the next push, your CI/CD job uses 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 each time. The old cache is not deleted. You can manually delete these files from the runner storage.
-
-<!-- ## Troubleshooting
-
-Include any troubleshooting steps that you can foresee. If you know beforehand what issues
-one might have when setting this up, or when something is changed, or on upgrading, it's
-important to describe those, too. Think of things that may go wrong and include them here.
-This is important to minimize requests for support, and to avoid doc comments with
-questions that you know someone might ask.
-
-Each scenario can be a third-level heading, e.g. `### Getting error message X`.
-If you have none to add when creating a doc, leave this section in place
-but commented out to help encourage others to add to it in the future. -->
+stage if the jobs run on different runners in subsequent pipelines.