Skip to content

Commit fa2182b

Browse files
igneuseregon
authored andcommitted
cache-version input allowing a forced change of the cache key
1 parent 2eb6adb commit fa2182b

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ inputs:
2424
description: 'The working directory to use for resolving paths for .ruby-version, .tool-versions and Gemfile.lock.'
2525
required: false
2626
default: '.'
27+
cache-version:
28+
description: |
29+
Arbitrary string that will be added to the cache key of the bundler cache. Set or change it if you need
30+
to invalidate the cache.
31+
required: false
32+
default: '0'
2733
outputs:
2834
ruby-prefix:
2935
description: 'The prefix of the installed ruby'

bundler.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const exec = require('@actions/exec')
55
const cache = require('@actions/cache')
66
const common = require('./common')
77

8+
export const DEFAULT_CACHE_VERSION = '0'
9+
810
// The returned gemfile is guaranteed to exist, the lockfile might not exist
911
export function detectGemfiles() {
1012
const gemfilePath = process.env['BUNDLE_GEMFILE'] || 'Gemfile'
@@ -95,7 +97,7 @@ export async function installBundler(bundlerVersionInput, lockFile, platform, ru
9597
return bundlerVersion
9698
}
9799

98-
export async function bundleInstall(gemfile, lockFile, platform, engine, rubyVersion, bundlerVersion) {
100+
export async function bundleInstall(gemfile, lockFile, platform, engine, rubyVersion, bundlerVersion, cacheVersion) {
99101
if (gemfile === null) {
100102
console.log('Could not determine gemfile path, skipping "bundle install" and caching')
101103
return false
@@ -128,7 +130,7 @@ export async function bundleInstall(gemfile, lockFile, platform, engine, rubyVer
128130

129131
// cache key
130132
const paths = [cachePath]
131-
const baseKey = await computeBaseKey(platform, engine, rubyVersion, lockFile)
133+
const baseKey = await computeBaseKey(platform, engine, rubyVersion, lockFile, cacheVersion)
132134
const key = `${baseKey}-${await common.hashFile(lockFile)}`
133135
// If only Gemfile.lock changes we can reuse part of the cache, and clean old gem versions below
134136
const restoreKeys = [`${baseKey}-`]
@@ -177,8 +179,9 @@ export async function bundleInstall(gemfile, lockFile, platform, engine, rubyVer
177179
return true
178180
}
179181

180-
async function computeBaseKey(platform, engine, version, lockFile) {
181-
let key = `setup-ruby-bundler-cache-v3-${platform}-${engine}-${version}`
182+
async function computeBaseKey(platform, engine, version, lockFile, cacheVersion) {
183+
const cacheVersionSuffix = DEFAULT_CACHE_VERSION === cacheVersion ? '' : `-cachever:${cacheVersion}`
184+
let key = `setup-ruby-bundler-cache-v3-${platform}-${engine}-${version}${cacheVersionSuffix}`
182185

183186
if (engine !== 'jruby' && common.isHeadVersion(version)) {
184187
let revision = '';

dist/index.js

Lines changed: 10 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const inputDefaults = {
1212
'bundler': 'default',
1313
'bundler-cache': 'true',
1414
'working-directory': '.',
15+
'cache-version': bundler.DEFAULT_CACHE_VERSION,
1516
}
1617

1718
// entry point when this action is run on its own
@@ -67,7 +68,7 @@ export async function setupRuby(options = {}) {
6768

6869
if (inputs['bundler-cache'] === 'true') {
6970
await common.measure('bundle install', async () =>
70-
bundler.bundleInstall(gemfile, lockFile, platform, engine, version, bundlerVersion))
71+
bundler.bundleInstall(gemfile, lockFile, platform, engine, version, bundlerVersion, inputs['cache-version']))
7172
}
7273
}
7374

0 commit comments

Comments
 (0)