GitLab CI/CDGitLab ArtifactsGitLab CachePipeline Optimization

Article & Tutorial

GitLab Artifacts vs Cache Explained

Understand when to use GitLab artifacts, cache, or both, including dependency flow, retention, security, cache keys, reports, storage, and pipeline performance.

CI artifacts preserved between stages while dependency cache accelerates future jobs
CI artifacts preserved between stages while dependency cache accelerates future jobs
Contents

Artifacts and cache solve different pipeline problems. Artifacts preserve job outputs for later jobs, reports, downloads, or deployments. Cache reuses dependency data to reduce repeated downloads or computation. A pipeline often uses both, but never for the same purpose.

Quick comparison

CharacteristicArtifactsCache
Primary purposePreserve outputs and reportsReuse dependencies or intermediate reusable data
Typical examplesPackages, binaries, test reports, logsPackage-manager downloads, compiler cache
Reliability expectationRequired output for downstream workPerformance optimization that may be missing
RetentionExplicit expiry and project storageCache policy and backend lifecycle
Security concernSensitive output and download accessCache poisoning and cross-branch reuse

Use artifacts for required outputs

If a downstream job must consume exactly what an upstream job produced, use artifacts or an artifact repository. Preserve immutable packages, test reports, coverage data, and deployment manifests with appropriate expiry and access controls.

Use cache for acceleration

Cache package-manager downloads or compiler state that can be regenerated safely. A job should still work when the cache is empty. Choose keys that prevent incompatible branches, architectures, lock files, or tool versions from sharing data.

A common combined pattern

  1. Restore a dependency cache using a lock-file-based key.
  2. Install dependencies and update the cache if policy allows.
  3. Build the application.
  4. Publish the build output as an artifact.
  5. Download that artifact in test or deployment jobs.
  6. Promote release packages to a durable repository when needed.

Security controls

  • Do not cache credentials or decrypted secret files.
  • Separate protected and untrusted branch caches.
  • Avoid executable cache content from unknown pipelines.
  • Set artifact access and expiry deliberately.
  • Scan release artifacts and preserve checksums.
  • Do not treat short-lived job artifacts as a permanent release repository.

Performance diagnosis

Measure download time, archive creation, extraction, cache hit rate, artifact size, and storage growth. A cache can slow a pipeline when large archives are uploaded and downloaded even though dependencies change frequently.

The rule is simple: artifacts carry required results forward; cache makes repeatable preparation faster. If losing the data would make the pipeline incorrect, it should not exist only in cache.

A working npm pipeline: cache dependencies, artifact the build

This illustrative .gitlab-ci.yml assumes package-lock.json is committed and npm run build creates a dist/ directory. Cache the npm download directory, not node_modules; npm ci creates a clean installation. The build is a required output, so pass dist/ as an artifact to the downstream verification job.

example..gitlab-ci.yml
stages:
  - build
  - verify

build_app:
  image: node:22
  stage: build
  cache:
    key:
      files:
        - package-lock.json
    paths:
      - .npm/
  script:
    - npm ci --cache .npm --prefer-offline
    - npm run build
  artifacts:
    paths:
      - dist/
    expire_in: 7 days

verify_build:
  image: alpine:3.20
  stage: verify
  needs:
    - job: build_app
      artifacts: true
  script:
    - test -d dist
    - find dist -type f | head

What to verify: On a fresh runner, the build must succeed even when no cache exists. In the next pipeline, inspect the job log for cache retrieval, while verify_build must receive dist/ from the build_app artifact. If dist/ is missing, verify that your build actually writes to that directory and that artifacts:paths matches it. Adjust the example to your project's output path.

Three mistakes that cause broken pipelines

  • Putting dist/ only in cache: cache entries can be missing or evicted; use artifacts for required build output.
  • Using a cache key shared across incompatible dependencies or platforms: prefer lock-file-based keys and verify runner isolation.
  • Assuming needs automatically downloads all previous artifacts: when using needs, explicitly include the producing job with artifacts: true.

Retention, costs and secure defaults

Set artifact expiry to the minimum that supports review and deployment; the latest successful pipeline's artifacts may be kept according to project settings. Do not place secrets, credentials, private keys or production .env files in caches or downloadable artifacts. Measure archive upload and extraction time: a large cache can make a pipeline slower.

Related Melochip reading and official source

Continue with the GitLab CI/CD troubleshooting guide at https://melochip.com/posts/gitlab-ci-cd-troubleshooting-guide and the GitLab pipeline fundamentals guide at https://melochip.com/posts/gitlab-ci-cd-pipelines-stages-jobs-runners-guide. Official reference: https://docs.gitlab.com/ci/caching/ and https://docs.gitlab.com/ci/yaml/#artifacts.

GitLab CI/CD troubleshooting on Melochip

GitLab CI/CD fundamentals on Melochip

GitLab official caching reference

GitLab job artifacts documentation

Did this help?

Yes No

Join the discussion

0 Comments

Ask questions, add context, or share what worked.

Sign in to join the discussion

No comments yet. Start the discussion.

More from Melochip Editorial

Related articles

Stay in the loop. Get new Melochip articles

Receive Melochip article updates when new articles are available. Explicit opt-in only.

No pre-checked boxes. Unsubscribe support is part of the subscriber foundation.