From 497a31d54e5339b83e739dc9e24cf262563fe32e Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 4 May 2024 18:28:36 +0200 Subject: [PATCH 1/2] feat: use OS and working-directory as cache key --- README.md | 5 +++-- src/cache.ts | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e32a5c0172..5e498a32a7 100644 --- a/README.md +++ b/README.md @@ -308,10 +308,11 @@ Inside our action, we perform 3 steps: ### Caching internals 1. We save and restore the following directory: `~/.cache/golangci-lint`. -2. The primary caching key looks like `golangci-lint.cache-{interval_number}-{go.mod_hash}`. +2. The primary caching key looks like `golangci-lint.cache-{runner_os}-{working_directory}-{interval_number}-{go.mod_hash}`. Interval number ensures that we periodically invalidate our cache (every 7 days). `go.mod` hash ensures that we invalidate the cache early - as soon as dependencies have changed. -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}-`. +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-{runner_os}-{working_directory}-{interval_number}-`. GitHub matches keys by prefix if we have no exact match for the primary cache. This scheme is basic and needs improvements. Pull requests and ideas are welcome. diff --git a/src/cache.ts b/src/cache.ts index 314647d6a7..dc6aa06b14 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -38,15 +38,22 @@ const getIntervalKey = (invalidationIntervalDays: number): string => { async function buildCacheKeys(): Promise { const keys = [] - const invalidationIntervalDays = parseInt(core.getInput(`cache-invalidation-interval`, { required: true }).trim()) - - // Periodically invalidate a cache because a new code being added. - let cacheKey = `golangci-lint.cache-${getIntervalKey(invalidationIntervalDays)}-` - keys.push(cacheKey) + // Cache by OS. + let cacheKey = `golangci-lint.cache-${process.env?.RUNNER_OS}-` // Get working directory from input const workingDirectory = core.getInput(`working-directory`) + if (workingDirectory) { + cacheKey += `${workingDirectory}-` + } + + // Periodically invalidate a cache because a new code being added. + const invalidationIntervalDays = parseInt(core.getInput(`cache-invalidation-interval`, { required: true }).trim()) + cacheKey += `${getIntervalKey(invalidationIntervalDays)}-` + + keys.push(cacheKey) + // create path to go.mod prepending the workingDirectory if it exists const goModPath = path.join(workingDirectory, `go.mod`) From 430c825860294b9bb69f28b9750765e1e79262d3 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 4 May 2024 18:29:16 +0200 Subject: [PATCH 2/2] chore: generate --- dist/post_run/index.js | 13 +++++++++---- dist/run/index.js | 13 +++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/dist/post_run/index.js b/dist/post_run/index.js index 6f07a5f73c..23f6c001c0 100644 --- a/dist/post_run/index.js +++ b/dist/post_run/index.js @@ -88824,12 +88824,17 @@ const getIntervalKey = (invalidationIntervalDays) => { }; async function buildCacheKeys() { const keys = []; - const invalidationIntervalDays = parseInt(core.getInput(`cache-invalidation-interval`, { required: true }).trim()); - // Periodically invalidate a cache because a new code being added. - let cacheKey = `golangci-lint.cache-${getIntervalKey(invalidationIntervalDays)}-`; - keys.push(cacheKey); + // Cache by OS. + let cacheKey = `golangci-lint.cache-${process.env?.RUNNER_OS}-`; // Get working directory from input const workingDirectory = core.getInput(`working-directory`); + if (workingDirectory) { + cacheKey += `${workingDirectory}-`; + } + // Periodically invalidate a cache because a new code being added. + const invalidationIntervalDays = parseInt(core.getInput(`cache-invalidation-interval`, { required: true }).trim()); + cacheKey += `${getIntervalKey(invalidationIntervalDays)}-`; + keys.push(cacheKey); // create path to go.mod prepending the workingDirectory if it exists const goModPath = path_1.default.join(workingDirectory, `go.mod`); core.info(`Checking for go.mod: ${goModPath}`); diff --git a/dist/run/index.js b/dist/run/index.js index b67b479635..428acc2f77 100644 --- a/dist/run/index.js +++ b/dist/run/index.js @@ -88824,12 +88824,17 @@ const getIntervalKey = (invalidationIntervalDays) => { }; async function buildCacheKeys() { const keys = []; - const invalidationIntervalDays = parseInt(core.getInput(`cache-invalidation-interval`, { required: true }).trim()); - // Periodically invalidate a cache because a new code being added. - let cacheKey = `golangci-lint.cache-${getIntervalKey(invalidationIntervalDays)}-`; - keys.push(cacheKey); + // Cache by OS. + let cacheKey = `golangci-lint.cache-${process.env?.RUNNER_OS}-`; // Get working directory from input const workingDirectory = core.getInput(`working-directory`); + if (workingDirectory) { + cacheKey += `${workingDirectory}-`; + } + // Periodically invalidate a cache because a new code being added. + const invalidationIntervalDays = parseInt(core.getInput(`cache-invalidation-interval`, { required: true }).trim()); + cacheKey += `${getIntervalKey(invalidationIntervalDays)}-`; + keys.push(cacheKey); // create path to go.mod prepending the workingDirectory if it exists const goModPath = path_1.default.join(workingDirectory, `go.mod`); core.info(`Checking for go.mod: ${goModPath}`);