Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use OS and working-directory as cache key #1032

Merged
merged 2 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 9 additions & 4 deletions dist/post_run/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions dist/run/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 12 additions & 5 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,22 @@ const getIntervalKey = (invalidationIntervalDays: number): string => {
async function buildCacheKeys(): Promise<string[]> {
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`)

Expand Down