Skip to content

Commit

Permalink
Use hash for working-dir chache key
Browse files Browse the repository at this point in the history
The previous implementation would lead to nested folders for the cachel-local-storage-path option. A sideeffect is that all caches will be invalidated once with this commit.
  • Loading branch information
eifinger committed Feb 8, 2024
1 parent ec5113d commit 958ae1b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 35 deletions.
25 changes: 14 additions & 11 deletions dist/save-cache/index.js

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

2 changes: 1 addition & 1 deletion dist/save-cache/index.js.map

Large diffs are not rendered by default.

23 changes: 13 additions & 10 deletions dist/setup/index.js

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

2 changes: 1 addition & 1 deletion dist/setup/index.js.map

Large diffs are not rendered by default.

25 changes: 15 additions & 10 deletions src/restore-cache.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as crypto from 'crypto'
import * as cache from '@actions/cache'
import * as glob from '@actions/glob'
import * as core from '@actions/core'
Expand All @@ -7,9 +8,10 @@ import {exists} from '@actions/io/lib/io-util'
import {getLinuxInfo} from './utils'

export const STATE_CACHE_PRIMARY_KEY = 'cache-primary-key'
export const CACHE_MATCHED_KEY = 'cache-matched-key'
export const STATE_CACHE_MATCHED_KEY = 'cache-matched-key'
const CACHE_DEPENDENCY_PATH = 'requirements**.lock'
const workingDir = `/${core.getInput('working-directory')}` || ''
const workingDirInput = core.getInput('working-directory')
const workingDir = workingDirInput ? `/${workingDirInput}` : ''
const cachePath = `${process.env['GITHUB_WORKSPACE']}${workingDir}/.venv`
const cacheLocalStoragePath =
`${core.getInput('cache-local-storage-path')}` || ''
Expand Down Expand Up @@ -48,12 +50,13 @@ async function computeKeys(
version: string
): Promise<{primaryKey: string; restoreKey: string}> {
core.debug(`Computing cache key for ${cacheDependencyPath}`)
const hash = await glob.hashFiles(cacheDependencyPath)
let primaryKey = ''
let restoreKey = ''
const dependencyPathHash = await glob.hashFiles(cacheDependencyPath)
const workingDirHash = workingDir
? `-${crypto.createHash('sha256').update(workingDir).digest('hex')}`
: ''
const osInfo = await getLinuxInfo()
primaryKey = `${cachePrefix}-${process.env['RUNNER_OS']}-${osInfo.osVersion}-${osInfo.osName}-rye-${version}-${workingDir}-${hash}`
restoreKey = `${cachePrefix}-${process.env['RUNNER_OS']}-${osInfo.osVersion}-${osInfo.osName}-rye-${version}-${workingDir}`
const primaryKey = `${cachePrefix}-setup-rye-${process.env['RUNNER_OS']}-${osInfo.osVersion}-${osInfo.osName}-rye-${version}${workingDirHash}-${dependencyPathHash}`
const restoreKey = `${cachePrefix}-setup-rye-${process.env['RUNNER_OS']}-${osInfo.osVersion}-${osInfo.osName}-rye-${version}${workingDirHash}`
return {primaryKey, restoreKey}
}

Expand All @@ -62,19 +65,21 @@ function handleMatchResult(
primaryKey: string
): void {
if (matchedKey) {
core.saveState(CACHE_MATCHED_KEY, matchedKey)
core.saveState(STATE_CACHE_MATCHED_KEY, matchedKey)
core.info(`Cache restored from key: ${matchedKey}`)
} else {
core.info(`Cache is not found`)
}
core.setOutput('cache-hit', matchedKey === primaryKey)
}

async function restoreCacheLocal(primaryKey: string): Promise<string> {
async function restoreCacheLocal(
primaryKey: string
): Promise<string | undefined> {
const storedCache = `${cacheLocalStoragePath}/${primaryKey}`
if (!(await exists(storedCache))) {
core.info(`Local cache is not found: ${storedCache}`)
return ''
return
}
await cp(storedCache, cachePath, {
copySourceDirectory: false,
Expand Down
4 changes: 2 additions & 2 deletions src/save-cache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as cache from '@actions/cache'
import * as core from '@actions/core'
import {mkdirP, cp} from '@actions/io/'
import {CACHE_MATCHED_KEY, STATE_CACHE_PRIMARY_KEY} from './restore-cache'
import {STATE_CACHE_MATCHED_KEY, STATE_CACHE_PRIMARY_KEY} from './restore-cache'

const enableCache = core.getInput('enable-cache') === 'true'
const workingDir = `/${core.getInput('working-directory')}` || ''
Expand All @@ -22,7 +22,7 @@ export async function run(): Promise<void> {

async function saveCache(): Promise<void> {
const primaryKey = core.getState(STATE_CACHE_PRIMARY_KEY)
const matchedKey = core.getState(CACHE_MATCHED_KEY)
const matchedKey = core.getState(STATE_CACHE_MATCHED_KEY)

if (!primaryKey) {
core.warning('Error retrieving key from state.')
Expand Down

0 comments on commit 958ae1b

Please sign in to comment.