diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2d53bff55..1ea307a68 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 @@ -464,6 +476,7 @@ jobs: - testMinorBundlerVersion - testExactBundlerVersion - testOlderBundlerVersion + - testBundlerCachePrune - testBundlerPre - testBundlerDev - testDependencyOnBundler1 diff --git a/README.md b/README.md index 27426ec8f..df79b87fb 100644 --- a/README.md +++ b/README.md @@ -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`). diff --git a/bundler.js b/bundler.js index cc16505b9..8eb2b827a 100644 --- a/bundler.js +++ b/bundler.js @@ -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}`) @@ -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' } @@ -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') @@ -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']) } diff --git a/dist/index.js b/dist/index.js index 5ec17d8c4..3acb32563 100644 --- a/dist/index.js +++ b/dist/index.js @@ -54,7 +54,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}`) @@ -78,6 +77,7 @@ async function installBundler(bundlerVersionInput, rubygemsInputSet, lockFile, p if (bundlerVersionFromGemfileLock) { bundlerVersion = bundlerVersionFromGemfileLock + console.log(`Using Bundler ${bundlerVersion} from ${lockFile} BUNDLED WITH ${bundlerVersion}`) } else { bundlerVersion = 'default' } @@ -171,6 +171,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 {} +} + 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') @@ -235,12 +256,13 @@ async function bundleInstall(gemfile, lockFile, platform, engine, rubyVersion, b 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']) }