@@ -11,48 +11,49 @@ GitLab CI/CD provides a caching mechanism that can be used to save time
...
@@ -11,48 +11,49 @@ GitLab CI/CD provides a caching mechanism that can be used to save time
when your jobs are running.
when your jobs are running.
Caching is about speeding the time a job is executed by reusing the same
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
developing software that depends on other libraries which are fetched via the
internet during build time.
internet during build time.
If caching is enabled, it's shared between pipelines and jobs at the project
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
level by default. Caches are not shared across projects.
projects.
Make sure you read the [`cache` reference](../yaml/README.md#cache) to learn
Make sure you read the [`cache` reference](../yaml/README.md#cache) to learn
how it is defined in `.gitlab-ci.yml`.
how it is defined in `.gitlab-ci.yml`.
## Cache vs artifacts
## Cache vs artifacts
Be careful if you use cache and artifacts to store the same path in your jobs
If you use cache and artifacts to store the same path in your jobs, the cache might
as **caches are restored before artifacts** and the content could be overwritten.
be overwritten because caches are restored before artifacts.
Don't use caching for passing artifacts between stages, as it is designed to store
Don't use caching for passing artifacts between stages, as it is designed to store
runtime dependencies needed to compile the project:
runtime dependencies needed to compile the project:
-`cache`: **For storing project dependencies**
-`cache`: **For storing project dependencies**
Caches are used to speed up runs of a given job in **subsequent pipelines**, by
Caches can increase the speed of a given job in subsequent pipelines. You can
storing downloaded dependencies so that they don't have to be fetched from the
store 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
internet again. Dependencies include things like npm packages, Go vendor packages, and so on.
be configured to pass intermediate build results between stages, this should be
You can configure a cache to pass intermediate build results between stages,
done with artifacts instead.
but you should use artifacts instead.
-`artifacts`: **Use for stage results that are 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
Artifacts are files that are generated by a job so they can be stored and uploaded. You can
be fetched and used by jobs in later stages of the **same pipeline**. In other words,
fetch and use artifacts in jobs in later stages of the same pipeline. You can't
[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).
create an artifact in a job in one stage, and use this artifact in a different job in
This data is be available in different pipelines, but is available to be downloaded
the same stage. This data is not available in different pipelines, but can be downloaded
from the UI.
from the UI.
The name `artifacts` sounds like it's only useful outside of the job, like for downloading
If you download modules while building your application, you can declare them as
a final image, but artifacts are also available in later stages within a pipeline.
artifacts and subsequent stage jobs can use them.
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
You can define an [expiry time](../yaml/README.md#artifactsexpire_in) so artifacts
some optimizations like declaring an [expiry time](../yaml/README.md#artifactsexpire_in)
are deleted after a defined time. Use [dependencies](../yaml/README.md#dependencies)
so you don't keep artifacts around too long, or using [dependencies](../yaml/README.md#dependencies)
to control which jobs fetch the artifacts.
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:
Caches:
...
@@ -68,7 +69,7 @@ Artifacts:
...
@@ -68,7 +69,7 @@ Artifacts:
- Are disabled if not defined per job (using `artifacts:`).
- Are disabled if not defined per job (using `artifacts:`).
- Can only be enabled per job, not globally.
- 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).
- Are always uploaded to GitLab (known as coordinator).
- Can have an expiration value for controlling disk usage (30 days by default).
- Can have an expiration value for controlling disk usage (30 days by default).
...
@@ -77,12 +78,8 @@ can't link to files outside it.
...
@@ -77,12 +78,8 @@ can't link to files outside it.
## Good caching practices
## Good caching practices
We have the cache from the perspective of the developers (who consume a cache
To ensure maximum availability of the cache, when you declare `cache` in your jobs,
within the job) and the cache from the perspective of the runner. Depending on
use one or more of the following:
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:
-[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/README.md#use-tags-to-limit-the-number-of-jobs-using-the-runner) and use the tag on jobs
that share their cache.
that share their cache.
...
@@ -92,13 +89,7 @@ cache, when declaring `cache` in your jobs, use one or a mix of the following:
...
@@ -92,13 +89,7 @@ cache, when declaring `cache` in your jobs, use one or a mix of the following:
different caches on each branch). For that, you can take advantage of the
different caches on each branch). For that, you can take advantage of the
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.
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)
After you have defined a [cache in `.gitlab-ci.yml`](../yaml/README.md#cache),
according to your workflow, the availability of the cache ultimately depends on
the availability of the cache depends on:
how the runner has been configured (the executor type and whether different
runners are used for passing the cache between jobs).
- The runner's executor type
- Whether different runners are used to pass the cache between jobs.
### Where the caches are stored
### 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
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
`.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
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.
...
@@ -379,11 +367,7 @@ machine where the runner is installed and depends on the type of the executor.
### How archiving and extracting works
### How archiving and extracting works
In the most simple scenario, consider that you use only one machine where the
This example has two jobs that belong to two consecutive stages:
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:
```yaml
```yaml
stages:
stages:
...
@@ -415,7 +399,8 @@ job B:
...
@@ -415,7 +399,8 @@ job B:
-vendor/
-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. Pipeline starts.
1.`job A` runs.
1.`job A` runs.
...
@@ -432,10 +417,9 @@ Here's what happens behind the scenes:
...
@@ -432,10 +417,9 @@ Here's what happens behind the scenes:
1. Pipeline finishes.
1. Pipeline finishes.
By using a single runner on a single machine, you don't have the issue where
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`, thus guaranteeing the
`job B` might execute on a runner different from `job A`. This setup guarantees the
cache between stages. That only works if the build goes from stage `build`
cache can be reused between stages. It only works if the execution goes from the `build` stage
to `test` in the same runner/machine, otherwise, you [might not have the cache
to the `test` stage in the same runner/machine. Otherwise, the cache [might not be available](#cache-mismatch).
available](#cache-mismatch).
During the caching process, there's also a couple of things to consider:
During the caching process, there's also a couple of things to consider:
...
@@ -449,10 +433,10 @@ During the caching process, there's also a couple of things to consider:
...
@@ -449,10 +433,10 @@ During the caching process, there's also a couple of things to consider:
pulled down), and the runner doesn't mind if the archive of `job A` overwrites
pulled down), and the runner doesn't mind if the archive of `job A` overwrites
things in the archive of `job B`.
things in the archive of `job B`.
The reason why it works this way is because the cache created for one runner
It works this way because the cache created for one runner
often isn't valid when used by a different one which can run on a
often isn't valid when used by a different one. A different runner may run on a
**different architecture** (e.g., when the cache includes binary files). And
different architecture (for example, when the cache includes binary files). Also,
since the different steps might be executed by runners running on different
because the different steps might be executed by runners running on different
machines, it is a safe default.
machines, it is a safe default.
### Cache mismatch
### Cache mismatch
...
@@ -464,7 +448,7 @@ mismatch and a few ideas how to fix it.
...
@@ -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 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 |
| 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. |
| 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.
Let's explore some examples.
...
@@ -472,12 +456,10 @@ Let's explore some examples.
...
@@ -472,12 +456,10 @@ Let's explore some examples.
#### Examples
#### Examples
Let's assume you have only one runner assigned to your project, so the cache
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 two jobs, A and B,
is stored in the runner's machine by default.
have the same cache key, but they cache different paths, cache B would overwrite
cache A, even if their `paths` don't match:
We want `job A` and `job B` to re-use their
Two jobs could cause caches to be overwritten if they have the same cache key, but
cache when the pipeline is run for a second time.
they cache a different path:
```yaml
```yaml
stages:
stages:
...
@@ -538,9 +520,8 @@ job B:
...
@@ -538,9 +520,8 @@ job B:
-vendor/
-vendor/
```
```
In that case, even if the `key` is different (no fear of overwriting), you
Even if the `key` is different, the cached files might get "cleaned" before each
might experience that the cached files "get cleaned" before each stage if the
stage if the jobs run on different runners in the subsequent pipelines.
jobs run on different runners in the subsequent pipelines.
## Clearing the cache
## Clearing the cache
...
@@ -559,7 +540,7 @@ next run of the pipeline, the cache is stored in a different location.
...
@@ -559,7 +540,7 @@ next run of the pipeline, the cache is stored in a different location.
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/41249) in GitLab 10.4.
> [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
If you want to avoid editing `.gitlab-ci.yml`, you can clear the cache
via GitLab's UI:
via GitLab's UI:
1. Navigate to your project's **CI/CD > Pipelines** page.
1. Navigate to your project's **CI/CD > Pipelines** page.
...
@@ -569,11 +550,6 @@ via GitLab's UI:
...
@@ -569,11 +550,6 @@ via GitLab's UI:
1. On the next push, your CI/CD job uses a new cache.
1. On the next push, your CI/CD job uses 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.
<!-- ## Troubleshooting
<!-- ## Troubleshooting
Include any troubleshooting steps that you can foresee. If you know beforehand what issues
Include any troubleshooting steps that you can foresee. If you know beforehand what issues