r/gitlab • • 2d ago

GitLab CI: cache or artifacts? Three questions to decide, and the mistake of using one for the other

These two keywords look similar and are often mixed up. As I understand the docs, they solve different problems:

  • Artifacts: files a job produces that you want to keep or pass to later jobs. They're uploaded to GitLab and are meant to be reliable for that purpose.
  • Cache: files you want to reuse to speed up jobs, typically dependencies. It's best-effort, so a job should still work if the cache is missing.

Three questions to decide

  1. Does a later job in this pipeline need these files to work? (A compiled binary, test reports, a built package.) That's an artifact. A cache isn't guaranteed to be there.
  2. Is it something that could be downloaded or rebuilt, just slower? (node_modules, a package manager's download folder.) That's a cache.
  3. Do you want the file to be downloadable from the pipeline or merge request afterward? (Test results, coverage report, a build output.) That's an artifact.

Example: dependencies as cache, build output as artifact (a sketch, validate it in your project's CI Lint):

build:
  stage: build
  cache:
    key:
      files:
        - package-lock.json
    paths:
      - node_modules/
  script:
    - npm ci
    - npm run build
  artifacts:
    paths:
      - dist/
    expire_in: 1 week

What each part does:

  • cache:key:files ties the cache to the lockfile, so it changes when dependencies change.
  • artifacts:paths keeps dist/ for later jobs and for download.
  • expire_in limits how long artifacts are stored.

The mistakes I'd watch for

  • Passing build output between jobs through the cache. It may work most of the time, and then fail when a job lands on a runner without that cache, which is hard to debug.
  • Using artifacts for huge dependency folders. They're uploaded and downloaded on every run, which can be slow and use storage.
  • No expire_in on artifacts, so storage grows. Check your instance's default and settings.
  • A cache key shared by unrelated jobs, so they overwrite each other's contents.

Limits: I haven't run this YAML on a runner for this post, so please check it in CI Lint. Cache behavior depends on your runner setup (shared vs. per-runner caches, distributed cache storage), and the exact keywords and defaults can differ between GitLab versions, so check the docs for yours.

How do you handle this in your pipelines, and have you had a cache that was "usually there" until it wasn't?

0 Upvotes

7 comments sorted by

7

u/imaginethepassion 1d ago

Thanks Claude

1

u/fangisland 1d ago

real question: have people successfully used Gitlab caching? It always felt like a trap to me so I avoid it, but I do platform engineering and IaC primarily so maybe caching doesn't have as much as a use case in my product space.

2

u/Torutofu_Raeva 1d ago

works fine for us once the key is tied to the lockfile, the trap is runners not sharing cache so you get random misses unless you set up distributed s3 cache

1

u/fangisland 1d ago

Do you pull a lot of dependencies at job runtime? I tend to just bundle them all into container files and I use caching on container layers instead and just have Renovate regularly bump dependencies in the containers. Maybe that's the difference with people who use Gitlab caching more?

1

u/titexcj 1d ago

does it still make sense to use cache if you have a very fast internet connection ?
i mean you still need to download the cache and unzip it , is that faster than just getting the dependencies ?

1

u/imaginethepassion 1d ago

It can help if your upstream has rate limits or can be down.

2

u/Torutofu_Raeva 23h ago

depends on the deps, for a big node_modules or maven repo the cache is usually still quicker than resolving everything, but with a fast connection and a proxy/mirror close by it's pretty much a wash