Skip to content

Commit fbac75a

Browse files
Add COURSIER_CACHE_ACTION_CACHE_VERSION env var for manual cache invalidation (#808)
* Initial plan * Add COURSIER_CACHE_VERSION env var for manual cache invalidation Co-authored-by: alexarchambault <7063723+alexarchambault@users.noreply.github.com> * Rename COURSIER_CACHE_VERSION to COURSIER_CACHE_ACTION_CACHE_VERSION Co-authored-by: alexarchambault <7063723+alexarchambault@users.noreply.github.com> * README: add step-level env var example alongside workflow-level example Co-authored-by: alexarchambault <7063723+alexarchambault@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: alexarchambault <7063723+alexarchambault@users.noreply.github.com> Co-authored-by: Alexandre Archambault <alexarchambault@users.noreply.github.com>
1 parent 31aa6a3 commit fbac75a

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,37 @@ Default: `false`
124124

125125
Set `true` to skip saving and restoring the Ammonite cache, regardless of whether the repository contains `.sc` scripts.
126126

127+
## Cache invalidation
128+
129+
To manually invalidate the cache without changing your build files, set the `COURSIER_CACHE_ACTION_CACHE_VERSION`
130+
environment variable in your workflow. Changing its value produces a different cache key across all
131+
cache types, so the old cache is ignored and a fresh one is created.
132+
133+
The variable can be set at the workflow level so that it applies to every step:
134+
135+
```yaml
136+
env:
137+
COURSIER_CACHE_ACTION_CACHE_VERSION: 1 # increment this value to invalidate the cache
138+
139+
steps:
140+
- uses: actions/checkout@v5
141+
- uses: coursier/cache-action@v7
142+
```
143+
144+
Or it can be scoped to only the `coursier/cache-action` step:
145+
146+
```yaml
147+
steps:
148+
- uses: actions/checkout@v5
149+
- uses: coursier/cache-action@v7
150+
env:
151+
COURSIER_CACHE_ACTION_CACHE_VERSION: 1 # increment this value to invalidate the cache
152+
```
153+
154+
This is equivalent to passing the version as `extraHashedContent`, but more convenient because it
155+
can be set globally for the whole workflow (or even as a repository variable / secret) without
156+
touching the individual step configuration.
157+
127158
## Outputs
128159

129160
* `cache-hit-coursier` - A boolean value to indicate a match was found for the coursier cache

src/restore.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,18 @@ async function run(): Promise<void> {
323323
const extraMillHashedContent = readExtraKeys('extraMillHashedContent')
324324
const extraAmmoniteHashedContent = readExtraKeys('extraAmmoniteHashedContent')
325325

326+
const cacheVersion = process.env['COURSIER_CACHE_ACTION_CACHE_VERSION'] ?? ''
327+
if (cacheVersion.length > 0) {
328+
core.info(
329+
`Cache invalidation: COURSIER_CACHE_ACTION_CACHE_VERSION is set to '${cacheVersion}', appending to all cache keys.`
330+
)
331+
}
332+
333+
const effectiveExtraHashedContent =
334+
cacheVersion.length > 0
335+
? `${extraHashedContent}\n${cacheVersion}`
336+
: extraHashedContent
337+
326338
const extraKey = readExtraKeys('extraKey')
327339
const extraCoursierKey = readExtraKeys('extraCoursierKey')
328340
const extraSbtKey = readExtraKeys('extraSbtKey')
@@ -379,7 +391,7 @@ async function run(): Promise<void> {
379391
const coursierHashedContent: Record<string, string> = {
380392
sbt: extraSbtHashedContent,
381393
mill: extraMillHashedContent,
382-
other: extraHashedContent,
394+
other: effectiveExtraHashedContent,
383395
coursier: extraCoursierHashedContent
384396
}
385397
if (!ignoreAmmonite) {
@@ -405,7 +417,7 @@ async function run(): Promise<void> {
405417
matrix,
406418
JSON.stringify({
407419
sbt: extraSbtHashedContent,
408-
other: extraHashedContent
420+
other: effectiveExtraHashedContent
409421
}),
410422
disableFallback
411423
)
@@ -420,7 +432,7 @@ async function run(): Promise<void> {
420432
matrix,
421433
JSON.stringify({
422434
mill: extraMillHashedContent,
423-
other: extraHashedContent
435+
other: effectiveExtraHashedContent
424436
}),
425437
disableFallback
426438
)
@@ -443,7 +455,7 @@ async function run(): Promise<void> {
443455
matrix,
444456
JSON.stringify({
445457
amm: extraAmmoniteHashedContent,
446-
other: extraHashedContent
458+
other: effectiveExtraHashedContent
447459
}),
448460
disableFallback
449461
)

0 commit comments

Comments
 (0)