TExcelTAD

CI Caches vs Artifact Repositories

CI systems have two features that both "keep files between runs", and teams mix them up:

  • A CI cache (actions/cache, GitLab's cache:, CircleCI's save_cache) stores a directory under a key and restores it in a later job if available. It's a performance optimisation: if it's missing or stale, the job still works, just slower.
  • An artifact repository (and CI's own "artifacts" feature for passing files between jobs of one pipeline) stores named, versioned outputs that someone depends on. If it's missing, the consumer fails.
CI cache Artifact repository
Purpose speed up the same kind of job hand finished outputs to other jobs, teams, deployments
Content ccache dir, ~/.m2, ~/.npm, ~/.cargo/registry, Gradle caches, node_modules libraries, packages, installers, images, symbols
Identity a cache key (OS + lockfile hash + branch…) name + version (+ checksum)
If missing job is slower consumer fails
Retention short, evicted by size/age (GitHub: 10 GB per repository by default, unused entries evicted after 7 days) policy-driven, releases kept for years
Trust branch-scoped; PRs read the base branch's cache access-controlled, immutable releases, signed
Correctness must never change results: a cache hit and a miss must give the same output is the result

Both, in one session

This session plays a two-job pipeline twice. First with a CI cache: job 1 builds with ccache and saves the cache directory as a tarball; job 2 starts with an empty cache, restores the tarball and builds again. Then with an artifact: job 1 installs its library into a "repository" directory; job 2 consumes the installed package and doesn't compile the library at all:

bash · try it yourself
mkdir -p ws/core/src ws/app && cd ws
cat > core/CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Core VERSION 2.1.0 LANGUAGES CXX)
add_library(core STATIC src/a.cpp src/b.cpp src/c.cpp src/d.cpp)
target_include_directories(core PUBLIC 
lt;BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
lt;INSTALL_INTERFACE:include>) install(TARGETS core EXPORT CoreTargets ARCHIVE DESTINATION lib) install(FILES src/core.hpp DESTINATION include) install(EXPORT CoreTargets NAMESPACE core:: DESTINATION lib/cmake/Core FILE CoreConfig.cmake) include(CMakePackageConfigHelpers) write_basic_package_version_file(CoreConfigVersion.cmake COMPATIBILITY SameMajorVersion) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/CoreConfigVersion.cmake DESTINATION lib/cmake/Core) CM printf '#pragma once\nint a(); int b(); int c(); int d();\n' > core/src/core.hpp for n in a b c d; do printf '#include "core.hpp"\n#include <string>\nint %s() { return int(std::string("%s").size()); }\n' $n $n > core/src/$n.cpp; done cat > app/CMakeLists.txt <<'CM' cmake_minimum_required(VERSION 3.25) project(App CXX) find_package(Core 2.1 REQUIRED) add_executable(app main.cpp) target_link_libraries(app PRIVATE core::core) CM printf '#include <core.hpp>\nint main() { return a() + b() + c() + d() - 4; }\n' > app/main.cpp hits() { ccache --print-stats | awk -F'\t' '$1 == "direct_cache_hit" { h = $2 } $1 == "cache_miss" { m = $2 } END { printf "hits=%d misses=%d", h, m }'; } compiles() { grep -c 'Building CXX' "$1"; } export CCACHE_BASEDIR=$PWD echo "=== CI cache" export CCACHE_DIR=$HOME/job1-ccache cmake -S core -B job1 -G Ninja -DCMAKE_CXX_COMPILER_LAUNCHER=ccache > /dev/null ccache -z > /dev/null && cmake --build job1 > job1.log echo "job 1: $(compiles job1.log) compiles, $(hits)" tar -C $HOME -czf ccache-linux-gcc13.tgz job1-ccache # "save cache" export CCACHE_DIR=$HOME/job2-ccache mkdir -p $CCACHE_DIR && tar -C $CCACHE_DIR --strip-components=1 -xzf ccache-linux-gcc13.tgz # "restore cache" cmake -S core -B job2 -G Ninja -DCMAKE_CXX_COMPILER_LAUNCHER=ccache > /dev/null ccache -z > /dev/null && cmake --build job2 > job2.log echo "job 2: $(compiles job2.log) compiles, $(hits) <- still 4 compiler invocations, all served from the cache" echo "=== artifact repository" cmake --install job1 --prefix $HOME/repo/core/2.1.0 > /dev/null # "publish" cmake -S app -B appbuild -G Ninja -DCMAKE_PREFIX_PATH=$HOME/repo/core/2.1.0 > /dev/null cmake --build appbuild > app.log && ./appbuild/app && echo "app built against core 2.1.0 from the repository" echo "consumer: $(compiles app.log) compile (its own main.cpp); core's sources are not even present in its build"
=== CI cache
job 1: 4 compiles, hits=0 misses=4
job 2: 4 compiles, hits=4 misses=0  <- still 4 compiler invocations, all served from the cache
=== artifact repository
app built against core 2.1.0 from the repository
consumer: 1 compile (its own main.cpp); core's sources are not even present in its build

The cache made job 2 cheap, but job 2 still built core: it needed the sources, ran CMake, hashed every input, and a single changed flag or compiler would have meant a full rebuild. The consumer of the artifact didn't build core at all; it depended on a version (find_package(Core 2.1)), resolved from a repository. That's the difference between "rebuild faster" and "don't rebuild".

Choosing

  • Same repository, same build, run often (every push of a C++ project): CI cache for ccache, package-manager caches for downloads.
  • Different repositories or teams (an engine used by three games, a protocol library used by ten services): publish versioned packages to an artifact repository (Conan remotes, vcpkg binary caching, Maven, npm), and let consumers pin versions.
  • Within one pipeline (build job → test jobs → deploy job): CI artifacts, so tests run on the exact binaries that will be deployed, then promote them.
  • Monorepo at scale: a remote build cache keyed by action (Remote Execution, Bazel, Gradle's build cache) blurs the line: every target's outputs are cached artifacts, keyed by content instead of version.

Pitfalls

  • Caching outputs that aren't deterministic: if a cached directory contains timestamps or absolute paths, a "hit" can produce a different result than a fresh run. See Field Notes: Cache Correctness.
  • Using a cache as a release channel: "the deploy job takes the binary from the cache" works until the cache is evicted. Releases go to a repository.
  • Restoring a giant cache that's mostly stale: downloading 8 GB to get 200 MB of hits is slower than building. Watch hit rates and cache sizes, and key caches narrowly (per compiler, per configuration).
  • Cross-branch poisoning: on GitHub, caches from the default branch are readable by other branches, but not the other way round; don't relax that.

Test yourself

What happens to a CI job when its cache entry is missing?

  1. It fails
  2. It still works, just slower
  3. It uses the artifact repository
  4. It skips the build

In the session, why did the consumer compile only 1 file while job 2 compiled 4 (from cache)?

  1. The consumer used ccache too
  2. The consumer depended on an installed, versioned package and never built core
  3. The consumer had fewer sources by accident
  4. Ninja skipped them

Where should the binary that a deploy job ships come from?

  1. The CI cache
  2. A CI artifact or artifact repository, promoted after testing
  3. A developer's machine
  4. A fresh rebuild in the deploy job