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.md114
1 files changed, 39 insertions, 75 deletions
diff --git a/doc/ci/caching/index.md b/doc/ci/caching/index.md
index f2cb9500b2c..0778d598d32 100644
--- a/doc/ci/caching/index.md
+++ b/doc/ci/caching/index.md
@@ -1,77 +1,39 @@
---
stage: Verify
-group: Continuous Integration
+group: Pipeline Execution
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
---
-# Cache dependencies in GitLab CI/CD
+# Caching in GitLab CI/CD
-GitLab CI/CD provides a caching mechanism that can be used to save time
-when your jobs are running.
+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.
-Caching is about speeding the time a job is executed by reusing the same
-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.
+To learn how to define the cache in your `.gitlab-ci.yml` file,
+see the [`cache` reference](../yaml/README.md#cache).
-If caching is enabled, it's shared between pipelines and jobs at the project
-level by default. Caches are not shared across projects.
+## How cache is different from artifacts
-Make sure you read the [`cache` reference](../yaml/README.md#cache) to learn
-how it is defined in `.gitlab-ci.yml`.
+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).
-## Cache vs artifacts
+- 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.
-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 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 are passed between stages.**
-
- 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.
-
- If you download modules while building your application, you can declare them as
- artifacts and subsequent stage jobs can use them.
+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 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.
+- 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 can also be used to make files available for download after a pipeline
- completes, like a build image.
-
-Caches:
-
-- Are disabled if not defined globally or per job (using `cache:`).
-- Are available for all jobs in your `.gitlab-ci.yml` if enabled globally.
-- Can be used in subsequent pipelines by the same job in which the cache was created (if not defined globally).
-- Are 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).
-- If defined per job, are used:
- - By the same job in a subsequent pipeline.
- - By subsequent jobs in the same pipeline, if they have identical dependencies.
-
-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 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).
+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.
@@ -81,10 +43,9 @@ can't link to files outside it.
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
+- [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.
-- [Use sticky runners](../runners/README.md#prevent-a-specific-runner-from-being-enabled-for-other-projects)
- that are only available to a particular project.
+- [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).
@@ -102,7 +63,7 @@ For runners to work with caches efficiently, you must do one of the following:
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.
-### Share caches across the same branch
+### Share caches between jobs in the same branch
Define a cache with the `key: ${CI_COMMIT_REF_SLUG}` so that jobs of each
branch always use the same cache:
@@ -130,7 +91,7 @@ cache:
key: "$CI_JOB_STAGE-$CI_COMMIT_REF_SLUG"
```
-### Share caches across different branches
+### Share caches across jobs in different branches
To share a cache across all branches and all jobs, use the same key for everything:
@@ -146,7 +107,7 @@ cache:
key: ${CI_JOB_NAME}
```
-### Disable cache on specific jobs
+### Disable cache for specific jobs
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
@@ -189,7 +150,7 @@ The most common use case of caching is to avoid downloading content like depende
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).
+For more examples, check out our [GitLab CI/CD templates](https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates).
### Cache Node.js dependencies
@@ -201,7 +162,7 @@ Instead, we tell npm to use `./.npm`, and cache it per-branch:
```yaml
#
-# https://gitlab.com/gitlab-org/gitlab/tree/master/lib/gitlab/ci/templates/Nodejs.gitlab-ci.yml
+# https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates/Nodejs.gitlab-ci.yml
#
image: node:latest
@@ -219,7 +180,7 @@ test_async:
- node ./specs/start.js ./specs/async.spec.js
```
-### Caching PHP dependencies
+### 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
@@ -228,7 +189,7 @@ are cached per-branch:
```yaml
#
-# https://gitlab.com/gitlab-org/gitlab/tree/master/lib/gitlab/ci/templates/PHP.gitlab-ci.yml
+# https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates/PHP.gitlab-ci.yml
#
image: php:7.2
@@ -248,7 +209,7 @@ test:
- vendor/bin/phpunit --configuration phpunit.xml --coverage-text --colors=never
```
-### Caching Python dependencies
+### 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
@@ -257,7 +218,7 @@ pip's cache is defined under `.cache/pip/` and both are cached per-branch:
```yaml
#
-# https://gitlab.com/gitlab-org/gitlab/tree/master/lib/gitlab/ci/templates/Python.gitlab-ci.yml
+# https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates/Python.gitlab-ci.yml
#
image: python:latest
@@ -289,7 +250,7 @@ test:
- flake8 .
```
-### Caching Ruby dependencies
+### Cache Ruby dependencies
Assuming your project is using [Bundler](https://bundler.io) to install the
gem dependencies, the following example defines `cache` globally so that all
@@ -297,7 +258,7 @@ jobs inherit it. Gems are installed in `vendor/ruby/` and are cached per-branch:
```yaml
#
-# https://gitlab.com/gitlab-org/gitlab/tree/master/lib/gitlab/ci/templates/Ruby.gitlab-ci.yml
+# https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates/Ruby.gitlab-ci.yml
#
image: ruby:2.6
@@ -347,7 +308,7 @@ deploy_job:
- bundle exec deploy
```
-### Caching Go dependencies
+### Cache Go dependencies
Assuming your project is using [Go Modules](https://github.com/golang/go/wiki/Modules) to install
Go dependencies, the following example defines `cache` in a `go-cache` template, that
@@ -396,6 +357,9 @@ machine where the runner is installed and depends on the type of the executor.
| [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. |
+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: