Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,18 @@ jobs:
bundler-cache: true
- run: bundle --version | grep -F "Bundler version 2.7.2"

testBundlerCachePrune:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: ./
with:
ruby-version: '4.0'
bundler: 4.1.0
bundler-cache: true
cache-version: prune-${{ hashFiles('bundler.js') }}
- run: ruby -e 'abort "Found cached .gem files" unless Dir["vendor/bundle/*/*/cache/*.gem"].empty?'

testBundlerPre:
name: "Test with a Bundler pre/rc version"
runs-on: ubuntu-latest
Expand Down Expand Up @@ -464,6 +476,7 @@ jobs:
- testMinorBundlerVersion
- testExactBundlerVersion
- testOlderBundlerVersion
- testBundlerCachePrune
- testBundlerPre
- testBundlerDev
- testDependencyOnBundler1
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ You might need to `"`-quote the environment variable name in YAML if it has unus
By default, `bundle install` is run with `--jobs` set to the number of available processors (up to 8).
This can be changed by setting `BUNDLE_JOBS`.

With Bundler 4.1 or later, this action defaults to `BUNDLE_PRUNE=cache` when saving a new cache
to omit downloaded `.gem` files. Set `BUNDLE_PRUNE=false` to keep them.

To perform caching, this action will use `bundle config --local path $PWD/vendor/bundle`.
Therefore, the Bundler `path` should not be changed in your workflow for the cache to work (no `bundle config path`).

Expand Down
32 changes: 27 additions & 5 deletions bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ function readBundledWithFromGemfileLock(lockFile) {
if (nextLine) {
const bundlerVersion = nextLine.trim()
if (isValidBundlerVersion(bundlerVersion)) {
console.log(`Using Bundler ${bundlerVersion} from ${lockFile} BUNDLED WITH ${bundlerVersion}`)
return bundlerVersion
} else {
console.log(`Could not parse BUNDLED WITH version as a valid Bundler release, ignoring it: ${bundlerVersion}`)
Expand All @@ -64,6 +63,7 @@ export async function installBundler(bundlerVersionInput, rubygemsInputSet, lock

if (bundlerVersionFromGemfileLock) {
bundlerVersion = bundlerVersionFromGemfileLock
console.log(`Using Bundler ${bundlerVersion} from ${lockFile} BUNDLED WITH ${bundlerVersion}`)
} else {
bundlerVersion = 'default'
}
Expand Down Expand Up @@ -157,6 +157,27 @@ function bundlerConfigSetArgs(bundlerVersion, key, value) {
}
}

async function bundlePruneOptions(lockFile) {
if (process.env.BUNDLE_PRUNE !== undefined) {
return {}
}

// Older Bundler versions do not support pruning the download cache.
const version = process.env.BUNDLER_VERSION || readBundledWithFromGemfileLock(lockFile)
if (parseFloat(version) < 4.1) {
return {}
}

// Keep any local or global Bundler setting, including an explicitly empty value.
const { stdout, exitCode } = await exec.getExecOutput('ruby',
['-rbundler', '-e', 'print Bundler.settings.all.include?("prune")'],
{ silent: true, ignoreReturnCode: true })
if (exitCode === 0 && stdout === 'false') {
return { env: { ...process.env, BUNDLE_PRUNE: 'cache' } }
}
return {}
}

export async function bundleInstall(gemfile, lockFile, platform, engine, rubyVersion, bundlerVersion, cacheVersion) {
if (gemfile === null) {
console.log('Could not determine gemfile path, skipping "bundle install" and caching')
Expand Down Expand Up @@ -221,12 +242,13 @@ export async function bundleInstall(gemfile, lockFile, platform, engine, rubyVer
const jobs = Math.min(os.availableParallelism(), 8)
installArgs.push('--jobs', `${jobs}`)
}
// @actions/cache only allows saving an unused key, and merge_group events should not save caches.
const saveCache = !common.isExactCacheKeyMatch(key, cachedKey) && process.env['GITHUB_EVENT_NAME'] !== 'merge_group'

// Always run 'bundle install' to list the gems
await exec.exec('bundle', installArgs)
await exec.exec('bundle', installArgs, saveCache ? await bundlePruneOptions(lockFile) : {})

// @actions/cache only allows to save for non-existing keys
// Also, skip saving cache for merge_group event
if (!common.isExactCacheKeyMatch(key, cachedKey) && process.env['GITHUB_EVENT_NAME'] !== 'merge_group') {
if (saveCache) {
if (cachedKey) { // existing cache but Gemfile.lock differs, clean old gems
await exec.exec('bundle', ['clean'])
}
Expand Down
32 changes: 27 additions & 5 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.