Skip to content

Commit

Permalink
fix: find cache key
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnowack committed Apr 28, 2023
1 parent 1a87442 commit 5264a1d
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 18 deletions.
2 changes: 1 addition & 1 deletion dist/cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ async function saveCache(paths, key) {
// eslint-disable-next-line max-len
'Path Validation Error: Path(s) specified in the action for caching do(es) not exist, hence no cache is being saved.');
}
const archiveFolder = (0, local_1.getLocalArchiveFolder)(key);
const archiveFolder = await (0, local_1.getLocalArchiveFolder)(key);
await io.mkdirP(archiveFolder);
const archivePath = path.join(archiveFolder, utils.getCacheFileName(compressionMethod));
core.debug(`Archive Path: ${archivePath}`);
Expand Down
3 changes: 2 additions & 1 deletion dist/cache/local.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ interface CacheResult {
archiveLocation: string;
}
export declare function getLocalCacheEntry(keys: string[], compressionMethod: CompressionMethod): Promise<CacheResult | undefined>;
export declare function getLocalArchiveFolder(key: string): string;
export declare function getLocalArchiveFolder(key: string): Promise<string>;
export declare function getLocalArchiveFolder(key: string, findKey?: boolean): Promise<string | undefined>;
export {};
28 changes: 23 additions & 5 deletions dist/cache/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,46 @@ async function getLocalCacheEntry(keys, compressionMethod) {
const memo = await asyncMemo;
if (memo)
return memo;
const cacheDir = getLocalArchiveFolder(key);
if (!await (0, io_util_1.exists)(cacheDir))
const cacheDir = await getLocalArchiveFolder(key, true);
if (!cacheDir || !await (0, io_util_1.exists)(cacheDir))
return undefined;
const cacheKey = path.basename(cacheDir);
const archiveLocation = path.join(cacheDir, cacheFileName);
if (!await (0, io_util_1.exists)(archiveLocation))
return undefined;
return {
cacheKey: key,
cacheKey,
archiveLocation,
};
}, Promise.resolve(undefined));
return result;
}
exports.getLocalCacheEntry = getLocalCacheEntry;
function getLocalArchiveFolder(key) {
// eslint-disable-next-line max-len
async function getLocalArchiveFolder(key, findKey = false) {
const { GITHUB_REPOSITORY, RUNNER_TOOL_CACHE } = process.env;
if (!RUNNER_TOOL_CACHE) {
throw new TypeError('Expected RUNNER_TOOL_CACHE environment variable to be defined.');
}
if (!GITHUB_REPOSITORY) {
throw new TypeError('Expected GITHUB_REPOSITORY environment variable to be defined.');
}
return path.join(RUNNER_TOOL_CACHE, GITHUB_REPOSITORY, key);
const cachePath = path.join(RUNNER_TOOL_CACHE, GITHUB_REPOSITORY);
const primaryCacheKey = path.join(cachePath, key);
if (!findKey || await (0, io_util_1.exists)(primaryCacheKey))
return primaryCacheKey;
const files = await (0, io_util_1.readdir)(cachePath);
const cacheKey = await files.reduce(async (memo, file) => {
await memo;
if (!file.startsWith(key))
return memo;
const stats = await (0, io_util_1.lstat)(path.join(cachePath, file));
if (!stats.isDirectory())
return memo;
return file;
}, Promise.resolve(undefined));
if (!cacheKey)
return undefined;
return path.join(cachePath, cacheKey);
}
exports.getLocalArchiveFolder = getLocalArchiveFolder;
1 change: 0 additions & 1 deletion dist/restoreImpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ async function restoreImpl(stateProvider) {
}
// Store the matched cache key in states
stateProvider.setState(constants_1.State.CacheMatchedKey, cacheKey);
console.log(utils.isExactKeyMatch);
const isExactKeyMatch = utils.isExactKeyMatch(core.getInput(constants_1.Inputs.Key, { required: true }), cacheKey);
core.setOutput(constants_1.Outputs.CacheHit, isExactKeyMatch.toString());
if (lookupOnly) {
Expand Down
1 change: 0 additions & 1 deletion dist/utils/actionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ function isGhes() {
}
exports.isGhes = isGhes;
function isExactKeyMatch(key, cacheKey) {
console.log('isExactKeyMatch', key, cacheKey);
return !!(cacheKey
&& cacheKey.localeCompare(key, undefined, {
sensitivity: 'accent',
Expand Down
2 changes: 1 addition & 1 deletion src/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export async function saveCache(
)
}

const archiveFolder = getLocalArchiveFolder(key)
const archiveFolder = await getLocalArchiveFolder(key)
await io.mkdirP(archiveFolder)
const archivePath = path.join(
archiveFolder,
Expand Down
30 changes: 24 additions & 6 deletions src/cache/local.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from 'path'
import { exists } from '@actions/io/lib/io-util'
import { exists, lstat, readdir } from '@actions/io/lib/io-util'
import { CompressionMethod } from './constants'
import { getCacheFileName } from './tar'

Expand All @@ -16,19 +16,24 @@ export async function getLocalCacheEntry(
const result = await keys.reduce<Promise<CacheResult | undefined>>(async (asyncMemo, key) => {
const memo = await asyncMemo
if (memo) return memo
const cacheDir = getLocalArchiveFolder(key)
if (!await exists(cacheDir)) return undefined
const cacheDir = await getLocalArchiveFolder(key, true)
if (!cacheDir || !await exists(cacheDir)) return undefined
const cacheKey = path.basename(cacheDir)
const archiveLocation = path.join(cacheDir, cacheFileName)
if (!await exists(archiveLocation)) return undefined
return {
cacheKey: key,
cacheKey,
archiveLocation,
}
}, Promise.resolve(undefined))
return result
}

export function getLocalArchiveFolder(key: string) {
export async function getLocalArchiveFolder(key: string): Promise<string>
// eslint-disable-next-line max-len
export async function getLocalArchiveFolder(key: string, findKey?: boolean): Promise<string | undefined>
// eslint-disable-next-line max-len
export async function getLocalArchiveFolder(key: string, findKey = false): Promise<string | undefined> {
const { GITHUB_REPOSITORY, RUNNER_TOOL_CACHE } = process.env
if (!RUNNER_TOOL_CACHE) {
throw new TypeError('Expected RUNNER_TOOL_CACHE environment variable to be defined.')
Expand All @@ -37,6 +42,19 @@ export function getLocalArchiveFolder(key: string) {
if (!GITHUB_REPOSITORY) {
throw new TypeError('Expected GITHUB_REPOSITORY environment variable to be defined.')
}
const cachePath = path.join(RUNNER_TOOL_CACHE, GITHUB_REPOSITORY)
const primaryCacheKey = path.join(cachePath, key)
if (!findKey || await exists(primaryCacheKey)) return primaryCacheKey

return path.join(RUNNER_TOOL_CACHE, GITHUB_REPOSITORY, key)
const files = await readdir(cachePath)
const cacheKey = await files.reduce<Promise<string | undefined>>(async (memo, file) => {
await memo
if (!file.startsWith(key)) return memo
const stats = await lstat(path.join(cachePath, file))
if (!stats.isDirectory()) return memo
return file
}, Promise.resolve(undefined))
if (!cacheKey) return undefined

return path.join(cachePath, cacheKey)
}
1 change: 0 additions & 1 deletion src/restoreImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ async function restoreImpl(
// Store the matched cache key in states
stateProvider.setState(State.CacheMatchedKey, cacheKey)

console.log(utils.isExactKeyMatch)
const isExactKeyMatch = utils.isExactKeyMatch(
core.getInput(Inputs.Key, { required: true }),
cacheKey,
Expand Down
1 change: 0 additions & 1 deletion src/utils/actionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export function isGhes(): boolean {
}

export function isExactKeyMatch(key: string, cacheKey?: string): boolean {
console.log('isExactKeyMatch', key, cacheKey)
return !!(
cacheKey
&& cacheKey.localeCompare(key, undefined, {
Expand Down

0 comments on commit 5264a1d

Please sign in to comment.