Bundler and the cache
Keep the bundle in the project
By default Bundler installs gems into the user gem folder, next to every other project. A pipeline can cache that folder, but it is shared and the cache key becomes guesswork. The reliable way is to put the bundle inside the project:
bundle config set --local path vendor/bundle
--local writes the setting into the project, not into the user configuration, so the next person who clones the repository does not inherit your paths. The command prints nothing on success.
What bundler wrote
The setting lives in one small file. Read it and commit it:
cat .bundle/config
---
BUNDLE_PATH: "vendor/bundle"
One key, one value. A CI cache that keeps vendor/bundle but loses this file installs the gems somewhere else and restores nothing, which is the classic half-working cache.
What to cache
Install again and look at the folder:
bundle install
Fetching gem metadata from https://rubygems.org/...
Resolving dependencies...
Fetching rake 13.4.2
Installing rake 13.4.2
Fetching minitest 5.27.0
Installing minitest 5.27.0
Bundle complete! 2 Gemfile dependencies, 2 gems now installed.
Bundled gems are installed into `./vendor/bundle`
ls vendor/bundle/ruby/3.4.0/gems
minitest-5.27.0
rake-13.4.2
The folder name carries the Ruby version, which is why a cache built for 3.4 is useless for 3.3: the pipeline installs again there, and that is correct. Cache the whole vendor/bundle directory, keyed on Gemfile.lock.
Run from the local bundle
Nothing else changes: bundle exec finds the gems in the project folder.
bundle exec rake test
Run options: --seed 29433
# Running:
...
Finished in 0.001215s, 2469.1155 runs/s, 3292.1540 assertions/s.
3 runs, 4 assertions, 0 failures, 0 errors, 0 skips
What to watch for
A cache is not an artifact. The cache exists to save seconds on the next
run and may be dropped at any time; the lock file and the gemspec are the
truth. If a pipeline passes only with a warm cache, it is hiding a
missing Gemfile entry.
Clear the cache once after editing the Gemfile and compare the log with
the cached run. If the two logs differ in more than timings, something
outside the lock file is being used.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
bundle config set --local path vendor/bundlechange?Answer
The project setting in .bundle/config, so the gems install inside the project
--local writes one project file and leaves the user configuration alone, which is why the file is committed with the repository.
-
What does this program print?
require "yaml" text = "---\nBUNDLE_PATH: \"vendor/bundle\"" p YAML.load(text)["BUNDLE_PATH"]Answer
"vendor/bundle"
The config file is YAML, so one key and one quoted value come back as a string, which p prints with quotes.
-
What does this program print?
p "vendor/bundle/ruby/#{RUBY_VERSION.split(".").first(2).join(".")}/gems"Answer
"vendor/bundle/ruby/3.4/gems"
The folder name is the major and minor version, which is why a cache built for 3.4 does not serve a 3.3 job.
-
What does this program print?
p Gem::Version.new("5.27.0") > Gem::Version.new("5.9.0")Answer
true
Gem::Version compares each part as a number, so 27 is greater than 9 even though the string is longer.
-
What is the difference between a cache and an artifact for Bundler?
Answer
The cache only saves seconds and may be dropped, while the lock file and the gemspec are the truth
A pipeline that needs a warm cache to pass is hiding a Gemfile entry, because a cold run must work too.
-
What does this program print?
p File.join("vendor", "bundle", "ruby", "3.4.0", "gems")Answer
"vendor/bundle/ruby/3.4.0/gems"
File.join puts one separator between the parts, which is the portable way to build the path the cache saves.
-
Why is the cache key the lock file and not the Gemfile?
Answer
The lock file holds the exact versions, so it changes only when the installed set changes
An edited Gemfile without a new lock file installs the same gems, so the key must follow the lock file to stay stable.
-
What does this program print?
files = { "vendor/bundle" => "cache", "test-report.xml" => "artifact" } files.each { |path, kind| puts "#{path}: #{kind}" }Answer
vendor/bundle: cache / test-report.xml: artifact
The installed gems are restored between runs, while the test report travels from the run to the person reading it.
-
Why does a cache built for Ruby 3.4 not help a 3.3 job?
Answer
The installed folder carries the Ruby version in its path, so the 3.3 job looks elsewhere
The version is part of the path under vendor/bundle, so the other interpreter finds nothing and installs again, which is correct.
-
A pipeline passes only with a warm cache. What does that usually mean?
Answer
A gem the code needs is missing from the Gemfile, and the cache hides it
A cold run is the honest test, which is why the page suggests clearing the cache once after a Gemfile change.
Article author: Arthur Isaev