summaryrefslogtreecommitdiff
path: root/doc/ci/caching/index.md
diff options
context:
space:
mode:
Diffstat (limited to 'doc/ci/caching/index.md')
-rw-r--r--doc/ci/caching/index.md188
1 files changed, 82 insertions, 106 deletions
diff --git a/doc/ci/caching/index.md b/doc/ci/caching/index.md
index dfa92d469bc..e8b22a24017 100644
--- a/doc/ci/caching/index.md
+++ b/doc/ci/caching/index.md
@@ -1,7 +1,7 @@
---
stage: Verify
group: Continuous Integration
-info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#designated-technical-writers
+info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
type: index, concepts, howto
---
@@ -11,48 +11,49 @@ GitLab CI/CD provides a caching mechanism that can be used to save time
when your jobs are running.
Caching is about speeding the time a job is executed by reusing the same
-content of a previous job. It can be particularly useful when you are
+content of a previous job. Use caching when you are
developing software that depends on other libraries which are fetched via the
internet during build time.
If caching is enabled, it's shared between pipelines and jobs at the project
-level by default, starting from GitLab 9.0. Caches are not shared across
-projects.
+level by default. Caches are not shared across projects.
Make sure you read the [`cache` reference](../yaml/README.md#cache) to learn
how it is defined in `.gitlab-ci.yml`.
## Cache vs artifacts
-Be careful if you use cache and artifacts to store the same path in your jobs
-as **caches are restored before artifacts** and the content could be overwritten.
+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.
Don't use caching for passing artifacts between stages, as it is designed to store
runtime dependencies needed to compile the project:
- `cache`: **For storing project dependencies**
- Caches are used to speed up runs of a given job in **subsequent pipelines**, by
- storing downloaded dependencies so that they don't have to be fetched from the
- internet again (like npm packages, Go vendor packages, etc.) While the cache could
- be configured to pass intermediate build results between stages, this should be
- done with artifacts instead.
+ Caches can increase the speed of a given job in subsequent pipelines. You can
+ store downloaded dependencies so that they don't have to be fetched from the
+ internet again. Dependencies include things like npm packages, Go vendor packages, and so on.
+ You can configure a cache to pass intermediate build results between stages,
+ but you should use artifacts instead.
-- `artifacts`: **Use for stage results that will be passed between stages.**
+- `artifacts`: **Use for stage results that are passed between stages.**
- Artifacts are files generated by a job which are stored and uploaded, and can then
- be fetched and used by jobs in later stages of the **same pipeline**. In other words,
- [you can't create an artifact in job-A in stage-1, and then use this artifact in job-B in stage-1](https://gitlab.com/gitlab-org/gitlab/-/issues/25837).
- This data will not be available in different pipelines, but is available to be downloaded
+ Artifacts are files that are generated by a job so they can be stored and uploaded. You can
+ fetch and use artifacts in jobs in later stages of the same pipeline. You can't
+ create an artifact in a job in one stage, and use this artifact in a different job in
+ the same stage. This data is not available in different pipelines, but can be downloaded
from the UI.
-The name `artifacts` sounds like it's only useful outside of the job, like for downloading
-a final image, but artifacts are also available in later stages within a pipeline.
-So if you build your application by downloading all the required modules, you might
-want to declare them as artifacts so that subsequent stages can use them. There are
-some optimizations like declaring an [expiry time](../yaml/README.md#artifactsexpire_in)
-so you don't keep artifacts around too long, or using [dependencies](../yaml/README.md#dependencies)
-to control which jobs fetch the artifacts.
+ If you download modules while building your application, you can declare them as
+ artifacts and subsequent stage jobs can use them.
+
+ You can define an [expiry time](../yaml/README.md#artifactsexpire_in) so artifacts
+ are deleted after a defined time. Use [dependencies](../yaml/README.md#dependencies)
+ to control which jobs fetch the artifacts.
+
+ Artifacts can also be used to make files available for download after a pipeline
+ completes, like a build image.
Caches:
@@ -68,7 +69,7 @@ Artifacts:
- Are disabled if not defined per job (using `artifacts:`).
- Can only be enabled per job, not globally.
-- Are created during a pipeline and can be used by the subsequent jobs of that currently active pipeline.
+- Are created during a pipeline and can be used by subsequent jobs in the same pipeline.
- Are always uploaded to GitLab (known as coordinator).
- Can have an expiration value for controlling disk usage (30 days by default).
@@ -77,28 +78,18 @@ can't link to files outside it.
## Good caching practices
-We have the cache from the perspective of the developers (who consume a cache
-within the job) and the cache from the perspective of the runner. Depending on
-which type of runner you are using, cache can act differently.
-
-From the perspective of the developer, to ensure maximum availability of the
-cache, when declaring `cache` in your jobs, use one or a mix of the following:
+To ensure maximum availability of the cache, when you declare `cache` in your jobs,
+use one or more of the following:
- [Tag your runners](../runners/README.md#use-tags-to-limit-the-number-of-jobs-using-the-runner) and use the tag on jobs
that share their cache.
- [Use sticky runners](../runners/README.md#prevent-a-specific-runner-from-being-enabled-for-other-projects)
- that will be only available to a particular project.
+ that are only available to a particular project.
- [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
[CI/CD predefined variables](../variables/README.md#predefined-environment-variables).
-TIP: **Tip:**
-Using the same runner for your pipeline, is the most simple and efficient way to
-cache files in one stage or pipeline, and pass this cache to subsequent stages
-or pipelines in a guaranteed manner.
-
-From the perspective of the runner, in order for cache to work effectively, one
-of the following must be true:
+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
@@ -106,13 +97,12 @@ of the following must be true:
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 will be stored.
+ where the cache is stored.
-TIP: **Tip:**
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.
-### Sharing caches across the same branch
+### Share caches across the same branch
Define a cache with the `key: ${CI_COMMIT_REF_SLUG}` so that jobs of each
branch always use the same cache:
@@ -122,10 +112,9 @@ cache:
key: ${CI_COMMIT_REF_SLUG}
```
-While this feels like it might be safe from accidentally overwriting the cache,
-it means merge requests get slow first pipelines, which might be a bad
-developer experience. The next time a new commit is pushed to the branch, the
-cache will be re-used.
+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.
To enable per-job and per-branch caching:
@@ -134,33 +123,32 @@ cache:
key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG"
```
-To enable per-branch and per-stage caching:
+To enable per-stage and per-branch caching:
```yaml
cache:
key: "$CI_JOB_STAGE-$CI_COMMIT_REF_SLUG"
```
-### Sharing caches across different branches
+### Share caches across different branches
-If the files you are caching need to be shared across all branches and all jobs,
-you can use the same key for all of them:
+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 the same cache between branches, but separate them by job:
+To share caches between branches, but have a unique cache for each job:
```yaml
cache:
key: ${CI_JOB_NAME}
```
-### Disabling cache on specific jobs
+### Disable cache on specific jobs
-If you have defined the cache globally, it means that each job will use the
+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:
@@ -169,7 +157,7 @@ job:
cache: {}
```
-### Inherit global config, 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
@@ -197,20 +185,19 @@ For more fine tuning, read also about the
## Common use cases
-The most common use case of cache is to preserve contents between subsequent
-runs of jobs for things like dependencies and commonly used libraries
-(Node.js packages, PHP packages, rubygems, Python libraries, etc.),
-so they don't have to be re-fetched from the public internet.
+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.
For more examples, check out our [GitLab CI/CD templates](https://gitlab.com/gitlab-org/gitlab/tree/master/lib/gitlab/ci/templates).
-### Caching Node.js dependencies
+### Cache Node.js dependencies
-Assuming your project is using [npm](https://www.npmjs.com/) to install the Node.js
+If your project is using [npm](https://www.npmjs.com/) to install the 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 since
-[you can't cache things outside of the project directory](../yaml/README.md#cachepaths),
-we tell npm to use `./.npm` instead, and it is cached per-branch:
+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:
```yaml
#
@@ -253,7 +240,7 @@ cache:
before_script:
# Install and run Composer
- - curl --show-error --silent https://getcomposer.org/installer | php
+ - curl --show-error --silent "https://getcomposer.org/installer" | php
- php composer.phar install
test:
@@ -355,17 +342,18 @@ test:
## Availability of the cache
-Caching is an optimization, but isn't guaranteed to always work, so you need to
+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.
-Assuming you have properly [defined `cache` in `.gitlab-ci.yml`](../yaml/README.md#cache)
-according to your workflow, the availability of the cache ultimately depends on
-how the runner has been configured (the executor type and whether different
-runners are used for passing the cache between jobs).
+After you have defined a [cache in `.gitlab-ci.yml`](../yaml/README.md#cache),
+the availability of the cache depends on:
+
+- The runner's executor type
+- Whether different runners are used to pass the cache between jobs.
### Where the caches are stored
-Since the runner is the one responsible for storing the cache, it's essential
+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
@@ -379,11 +367,7 @@ machine where the runner is installed and depends on the type of the executor.
### How archiving and extracting works
-In the most simple scenario, consider that you use only one machine where the
-runner is installed, and all jobs of your project run on the same host.
-
-Let's see the following example of two jobs that belong to two consecutive
-stages:
+This example has two jobs that belong to two consecutive stages:
```yaml
stages:
@@ -415,7 +399,8 @@ job B:
- vendor/
```
-Here's what happens behind the scenes:
+If you have one machine with one runner installed, and all jobs for your project
+run on the same host:
1. Pipeline starts.
1. `job A` runs.
@@ -431,28 +416,27 @@ Here's what happens behind the scenes:
1. `script` is executed.
1. Pipeline finishes.
-By using a single runner on a single machine, you'll not have the issue where
-`job B` might execute on a runner different from `job A`, thus guaranteeing the
-cache between stages. That will only work if the build goes from stage `build`
-to `test` in the same runner/machine, otherwise, you [might not have the cache
-available](#cache-mismatch).
+By using a single runner on a single machine, you don't have the issue where
+`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).
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
- key. So, two jobs with different paths, but the same cache key, will overwrite
+ key. So, two jobs with different paths, but the same cache key, overwrites
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
pulled down), and the runner doesn't mind if the archive of `job A` overwrites
things in the archive of `job B`.
-The reason why it works this way is because the cache created for one runner
-often will not be valid when used by a different one which can run on a
-**different architecture** (e.g., when the cache includes binary files). And
-since the different steps might be executed by runners running on different
+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
machines, it is a safe default.
### Cache mismatch
@@ -464,7 +448,7 @@ mismatch and a few ideas 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. Currently, there's no automatic way to do this. |
+| 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.
@@ -472,12 +456,10 @@ Let's explore some examples.
#### Examples
Let's assume you have only one runner assigned to your project, so the cache
-will be stored in the runner's machine by default. If two jobs, A and B,
-have the same cache key, but they cache different paths, cache B would overwrite
-cache A, even if their `paths` don't match:
+is stored in the runner's machine by default.
-We want `job A` and `job B` to re-use their
-cache when the pipeline is run for a second time.
+Two jobs could cause caches to be overwritten if they have the same cache key, but
+they cache a different path:
```yaml
stages:
@@ -506,15 +488,15 @@ job B:
1. `job B` runs.
1. The previous cache, if any, is unzipped.
1. `vendor/` is cached as cache.zip and overwrites the previous one.
-1. The next time `job A` runs it will use the cache of `job B` which is different
- and thus will be ineffective.
+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.
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
-will be different):
+is different):
```yaml
stages:
@@ -538,9 +520,8 @@ job B:
- vendor/
```
-In that case, even if the `key` is different (no fear of overwriting), you
-might experience that the cached files "get cleaned" before each stage if the
-jobs run on different runners in the subsequent pipelines.
+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
@@ -553,26 +534,21 @@ 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 will be stored in a different location.
+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 easily clear the cache
-via GitLab's UI:
+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 will use a new cache.
-
-Behind the scenes, this works by increasing a counter in the database, and the
-value of that counter is used to create the key for the cache by appending an
-integer to it: `-1`, `-2`, etc. After a push, a new key is generated and the
-old cache is not valid anymore.
+1. On the next push, your CI/CD job uses a new cache.
<!-- ## Troubleshooting