Does GitLab Runner Cache the Builds Directory?
In this article
No, GitLab Runner does not cache the builds directory by default. The builds directory (typically /builds on the runner, or $CI_BUILDS_DIR) is a temporary workspace where each job's source code is checked out and build artifacts are produced. It is created fresh for each job and cleaned up after the job finishes, unless you explicitly configure caching or use artifacts. Docker, when used as the executor, also does not persist this directory between jobs by default—each job runs in a new container with a fresh filesystem.
What the builds directory actually is
When a GitLab Runner picks up a job, it creates a dedicated directory on the runner host (or inside a Docker container) to store the cloned repository, any dependencies you download, and the output of your build commands. This directory is often referred to as $CI_BUILDS_DIR (also available as $CI_PROJECT_DIR). Its purpose is to isolate each job's workspace so that concurrent jobs do not interfere with each other.
By default, the runner cleans this directory after the job completes. This means that anything you generate during the job—compiled binaries, downloaded packages, temporary files—is gone unless you explicitly save it. The two built-in mechanisms for preserving data are artifacts (which are uploaded to GitLab and can be downloaded later) and caches (which are stored on the runner or in a distributed cache and reused in subsequent jobs).
How Docker executors handle the builds directory
When you use the Docker executor, GitLab Runner creates a new container for each job. The builds directory is mounted into that container as a volume, but it is a temporary volume that exists only for the duration of the job. Once the job finishes, the container is removed and the volume is deleted. This is true even if you use a docker or docker+machine executor.
If you want to share data between jobs on the same runner, you can configure a cache in your .gitlab-ci.yml file. The cache is stored in a separate location (by default on the runner's disk, under /cache or a path you specify) and is not part of the builds directory. It is persisted across jobs, subject to cache key rules and expiration policies.
What is cached by default, if anything
GitLab Runner does not cache anything by default. You must explicitly define a cache section in your CI configuration. For example:
cache:
paths:
- node_modules/
This tells the runner to save the node_modules directory after the job and restore it before the next job that uses the same cache key. Without such a definition, every job starts with a clean slate—no cached dependencies, no cached build output.
Docker itself does not cache the builds directory either. Docker's layer caching applies to image layers, not to files inside a running container's filesystem. When you build a Docker image, each RUN command creates a new layer, and those layers are cached if the base image and the command have not changed. But this is unrelated to the GitLab Runner builds directory.
How to persist data between jobs
If you need to reuse files across jobs, you have two main options:
- Artifacts: Upload files from one job and download them in a later job. Artifacts are stored in GitLab and can be kept for a defined period (e.g.,
expire_in: 1 week). They are ideal for passing build outputs like compiled binaries or test reports. - Cache: Store directories like package manager caches (
npm,pip,maven) to speed up dependency installation. Caches are stored on the runner or in a distributed cache (e.g., S3) and are keyed by branch, job, or any custom key you define.
For example, to cache npm dependencies and pass a built binary as an artifact:
build:
cache:
key: $CI_COMMIT_REF_SLUG
paths:
- node_modules/
artifacts:
paths:
- dist/
expire_in: 1 week
script:
- npm install
- npm run build
Common misconceptions about caching
A frequent misunderstanding is that the builds directory is cached automatically because it is on disk. It is not. The runner deletes it after each job unless you use the GIT_STRATEGY: none option, which tells the runner not to clean the directory. However, even with GIT_STRATEGY: none, the directory is still not cached in the sense of being preserved for future jobs—it is simply left in place on the runner's filesystem, which can lead to stale state and is not recommended for most use cases.
Another misconception is that Docker's --cache-from flag or BuildKit cache mounts affect the builds directory. They do not. Those mechanisms apply to Docker image builds, not to the GitLab Runner workspace.
When you might want to cache the builds directory
There are rare cases where you want to reuse the entire builds directory between jobs, such as when you have a very large monorepo and want to avoid re-cloning the repository. You can achieve this by setting GIT_STRATEGY: none and using a cache with a path that points to the project directory. However, this is fragile because the cache is a tar archive that gets extracted over the existing directory, and it can cause conflicts if the directory structure changes.
A safer approach is to use incremental builds within a single job—do all the work in one job and use artifacts to pass results forward. This avoids the complexity of caching the entire workspace.
Frequently Asked Questions
Does GitLab Runner clean the builds directory automatically?
Yes, by default the runner removes the builds directory after each job. The exact cleanup behavior depends on the executor: for the shell executor, the directory is removed and recreated; for the Docker executor, the container and its volumes are destroyed.
Can I make the builds directory persist across jobs?
Yes, you can set GIT_STRATEGY: none to prevent the runner from cleaning the directory, but this is not a true cache—it just leaves the directory on the runner's disk. For reliable persistence, use artifacts or cache.
Does Docker cache the builds directory in its image layers?
No. Docker caches image layers, not files inside a running container's filesystem. The builds directory is mounted as a temporary volume and is not part of any image layer.
What is the difference between cache and artifacts in GitLab CI?
Cache is for storing dependencies to speed up future jobs; it is not guaranteed to be available (it can be cleared or expire). Artifacts are for passing job outputs to later stages; they are uploaded to GitLab and can be downloaded or used in subsequent jobs.
My builds are slow because dependencies are re-downloaded every time. What should I do?
Define a cache section in your .gitlab-ci.yml with the paths to your package manager's cache directory (e.g., node_modules, ~/.m2, ~/.cache/pip). This will speed up subsequent jobs significantly.
Is it safe to use GIT_STRATEGY: none to keep the builds directory?
It can be safe for a single runner with a single project, but it risks stale state and is not recommended for parallel jobs or multiple runners. Prefer artifacts or cache for explicit data transfer.