Artifacts against caches

What a cache is

A cache carries files from one run to the next. It exists to save time and may be dropped by the CI at any moment, so a job that only works with a warm cache is a job that will fail on someone else.

The Ruby cache is the bundle from lesson 55. Nothing else in this project takes long enough to be worth caching.

What an artifact is

An artifact carries a result out of a run: the test report from lesson 57, a built gem, an image tag. It is kept for the person reading the result, and it survives a failed job when the job asks for it.

The difference in one sentence: a cache is for the next run, an artifact is for a human.

The two folders

The project has both folders after the earlier lessons. Their sizes show why they are treated differently:

du -sh vendor/bundle

2.0M vendor/bundle

du -sh test/reports

8.0K test/reports

Two megabytes of gems, eight kilobytes of report. The bundle is worth caching because reinstalling it costs a network round trip; the report is worth uploading because it is small and it is the thing a reader opens after a failure.

Both keys in one job

A GitHub job declares the cache and the artifact side by side:

steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: "3.4" bundler-cache: true - run: bundle exec rake test - uses: actions/upload-artifact@v4 if: always() with: name: test-reports path: test/reports/*.xml

GitLab writes the same two ideas as keys:

cache: key: files: - Gemfile.lock paths: - vendor/bundle artifacts: when: always paths: - test/reports/*.xml

The cache key is the lock file: new gems, new cache; a new test file, the same cache. The artifact path is the reporter output path from lesson 57, copied from the line the reporter prints.

What to watch for

Never cache what the pipeline builds. A cached .gem or a cached image from an older commit is how a release ships yesterday's code; those are artifacts, built fresh each run.

Set a retention on artifacts. A report from a year ago is rarely the one someone opens, and every CI plan charges for storage sooner or later. A week or two covers the usual "why did this fail" conversation.

Next: Automated releases

Article author: Arthur Isaev