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 13.5.
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
13.7, 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 13.7, 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.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which sentence separates a cache from an artifact?
Answer
A cache is for the next run and may be dropped, an artifact is for a person and outlives the run
The bundle is restored for speed, while the report is kept so someone can read why a run failed.
-
What does this program print?
lines = ["2.0M\tvendor/bundle", "8.0K\ttest/reports"] lines.each { |l| size, path = l.split("\t"); puts "#{path}: #{size}" }Answer
vendor/bundle: 2.0M / test/reports: 8.0K
Each line of du output is a size, a tab and a path, and the program swaps them into name: size.
-
What does this program print?
require "digest" p Digest::SHA256.hexdigest("minitest 5.27.0\n").lengthAnswer
64
A SHA-256 digest is 32 bytes, which is 64 hex characters; a CI cache key is a hash like this one.
-
Why does a pipeline set a retention on artifacts?
Answer
Storage is charged, and a report from a year ago is rarely the one someone opens
A week or two covers the usual failure investigation, and the rest is paid storage nobody reads.
-
What does this program print?
require "digest" a = Digest::SHA256.hexdigest("rake 13.4.2") b = Digest::SHA256.hexdigest("rake 13.4.2") p a == bAnswer
true
The same input gives the same digest, which is what makes the lock file a stable cache key.
-
Why is a cached
.gema bad idea for a release?Answer
A cached package can come from an older commit, so the release ships yesterday's code
A built package is an artifact and belongs to one run; a cache is reused, and reuse is exactly the danger here.
-
What does this program print?
sizes = { "vendor/bundle" => 2 * 1024, "test/reports" => 8 } p sizes["vendor/bundle"] / sizes["test/reports"]Answer
256
The bundle is about 256 times the report, which is why one is restored for speed and the other uploaded for reading.
-
What does this program print?
paths = ["test/reports/todo_test.xml", "test/reports/other.txt"] p paths.count { |p| p.end_with?(".xml") }Answer
1
The artifact glob takes only the XML reports, and one of the two paths matches.
-
When does an artifact of a failed job survive?
Answer
Only when the job asks for it, with
if: always()orwhen: alwaysThe default keeps artifacts of successful runs, and the failed run is exactly the one whose report matters.
-
What does this program print?
sizes = [2048, 8, 8] p sizes.sumAnswer
2064
The report total is tiny next to the bundle, and a sum is how a job budget is checked.
Article author: Arthur Isaev