Skip to content

Commit 21e9e6b

Browse files
authored
feat: use OS and working-directory as cache key (#1032)
1 parent dbe4fc2 commit 21e9e6b

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,11 @@ Inside our action, we perform 3 steps:
308308
### Caching internals
309309

310310
1. We save and restore the following directory: `~/.cache/golangci-lint`.
311-
2. The primary caching key looks like `golangci-lint.cache-{interval_number}-{go.mod_hash}`.
311+
2. The primary caching key looks like `golangci-lint.cache-{runner_os}-{working_directory}-{interval_number}-{go.mod_hash}`.
312312
Interval number ensures that we periodically invalidate our cache (every 7 days).
313313
`go.mod` hash ensures that we invalidate the cache early - as soon as dependencies have changed.
314-
3. We use [restore keys](https://help.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows#matching-a-cache-key): `golangci-lint.cache-{interval_number}-`.
314+
3. We use [restore keys](https://help.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows#matching-a-cache-key):
315+
`golangci-lint.cache-{runner_os}-{working_directory}-{interval_number}-`.
315316
GitHub matches keys by prefix if we have no exact match for the primary cache.
316317

317318
This scheme is basic and needs improvements. Pull requests and ideas are welcome.

dist/post_run/index.js

+9-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/run/index.js

+9-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cache.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,22 @@ const getIntervalKey = (invalidationIntervalDays: number): string => {
3838
async function buildCacheKeys(): Promise<string[]> {
3939
const keys = []
4040

41-
const invalidationIntervalDays = parseInt(core.getInput(`cache-invalidation-interval`, { required: true }).trim())
42-
43-
// Periodically invalidate a cache because a new code being added.
44-
let cacheKey = `golangci-lint.cache-${getIntervalKey(invalidationIntervalDays)}-`
45-
keys.push(cacheKey)
41+
// Cache by OS.
42+
let cacheKey = `golangci-lint.cache-${process.env?.RUNNER_OS}-`
4643

4744
// Get working directory from input
4845
const workingDirectory = core.getInput(`working-directory`)
4946

47+
if (workingDirectory) {
48+
cacheKey += `${workingDirectory}-`
49+
}
50+
51+
// Periodically invalidate a cache because a new code being added.
52+
const invalidationIntervalDays = parseInt(core.getInput(`cache-invalidation-interval`, { required: true }).trim())
53+
cacheKey += `${getIntervalKey(invalidationIntervalDays)}-`
54+
55+
keys.push(cacheKey)
56+
5057
// create path to go.mod prepending the workingDirectory if it exists
5158
const goModPath = path.join(workingDirectory, `go.mod`)
5259

0 commit comments

Comments
 (0)