Files
setup-uv/src/cache/restore-cache.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

124 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-09-05 08:06:45 -04:00
import * as cache from "@actions/cache";
import * as core from "@actions/core";
2025-08-12 22:12:10 +02:00
import { hashFiles } from "../hash/hash-files";
2026-03-28 16:23:26 +01:00
import type { SetupInputs } from "../utils/inputs";
import * as log from "../utils/logging";
import { getArch, getOSNameVersion, getPlatform } from "../utils/platforms";
2024-08-23 23:58:26 +02:00
2024-09-05 08:06:45 -04:00
export const STATE_CACHE_KEY = "cache-key";
export const STATE_CACHE_MATCHED_KEY = "cache-matched-key";
export const STATE_PYTHON_CACHE_MATCHED_KEY = "python-cache-matched-key";
const CACHE_VERSION = "2";
2024-08-23 23:58:26 +02:00
2026-03-28 16:23:26 +01:00
export async function restoreCache(
inputs: SetupInputs,
pythonVersion?: string,
): Promise<void> {
const cacheKey = await computeKeys(inputs, pythonVersion);
core.saveState(STATE_CACHE_KEY, cacheKey);
core.setOutput("cache-key", cacheKey);
2026-03-28 16:23:26 +01:00
if (!inputs.restoreCache) {
log.info("restore-cache is false. Skipping restore cache step.");
core.setOutput("python-cache-hit", false);
return;
}
2024-08-23 23:58:26 +02:00
2026-03-28 16:23:26 +01:00
if (inputs.cacheLocalPath === undefined) {
throw new Error(
"cache-local-path is not set. Cannot restore cache without a valid cache path.",
);
}
await restoreCacheFromKey(
cacheKey,
2026-03-28 16:23:26 +01:00
inputs.cacheLocalPath.path,
STATE_CACHE_MATCHED_KEY,
"cache-hit",
);
2026-03-28 16:23:26 +01:00
if (inputs.cachePython) {
await restoreCacheFromKey(
`${cacheKey}-python`,
2026-03-28 16:23:26 +01:00
inputs.pythonDir,
STATE_PYTHON_CACHE_MATCHED_KEY,
"python-cache-hit",
);
} else {
core.setOutput("python-cache-hit", false);
2025-10-09 16:47:24 -04:00
}
}
async function restoreCacheFromKey(
cacheKey: string,
cachePath: string,
stateKey: string,
outputKey: string,
): Promise<void> {
log.info(
`Trying to restore cache from GitHub Actions cache with key: ${cacheKey}`,
);
let matchedKey: string | undefined;
2024-08-23 23:58:26 +02:00
try {
matchedKey = await cache.restoreCache([cachePath], cacheKey);
2024-08-23 23:58:26 +02:00
} catch (err) {
2024-09-05 08:06:45 -04:00
const message = (err as Error).message;
log.warning(message);
core.setOutput(outputKey, false);
2024-09-05 08:06:45 -04:00
return;
2024-08-23 23:58:26 +02:00
}
handleMatchResult(matchedKey, cacheKey, stateKey, outputKey);
2024-08-23 23:58:26 +02:00
}
2026-03-28 16:23:26 +01:00
async function computeKeys(
inputs: SetupInputs,
pythonVersion?: string,
): Promise<string> {
2024-09-05 08:06:45 -04:00
let cacheDependencyPathHash = "-";
2026-03-28 16:23:26 +01:00
if (inputs.cacheDependencyGlob !== "") {
log.info(
2026-03-28 16:23:26 +01:00
`Searching files using cache dependency glob: ${inputs.cacheDependencyGlob.split("\n").join(",")}`,
);
cacheDependencyPathHash += await hashFiles(
inputs.cacheDependencyGlob,
true,
);
2024-09-05 08:06:45 -04:00
if (cacheDependencyPathHash === "-") {
log.warning(
2026-03-28 16:23:26 +01:00
`No file matched to [${inputs.cacheDependencyGlob.split("\n").join(",")}]. The cache will never get invalidated. Make sure you have checked out the target repository and configured the cache-dependency-glob input correctly.`,
2024-09-05 08:06:45 -04:00
);
2024-08-23 23:58:26 +02:00
}
2024-12-20 11:42:38 +01:00
}
if (cacheDependencyPathHash === "-") {
cacheDependencyPathHash = "-no-dependency-glob";
2024-08-23 23:58:26 +02:00
}
const suffix = inputs.cacheSuffix
? `-${encodeURIComponent(inputs.cacheSuffix)}`
: "";
const version = encodeURIComponent(pythonVersion ?? "unknown");
2025-02-17 10:32:34 +01:00
const platform = await getPlatform();
const osNameVersion = getOSNameVersion();
2026-03-28 16:23:26 +01:00
const pruned = inputs.pruneCache ? "-pruned" : "";
const python = inputs.cachePython ? "-py" : "";
return `setup-uv-${CACHE_VERSION}-${getArch()}-${platform}-${osNameVersion}-${version}${pruned}${python}${cacheDependencyPathHash}${suffix}`;
2024-08-23 23:58:26 +02:00
}
function handleMatchResult(
matchedKey: string | undefined,
2024-09-05 08:06:45 -04:00
primaryKey: string,
stateKey: string,
outputKey: string,
2024-08-23 23:58:26 +02:00
): void {
if (!matchedKey) {
log.info(`No GitHub Actions cache found for key: ${primaryKey}`);
core.setOutput(outputKey, false);
2024-09-05 08:06:45 -04:00
return;
2024-08-23 23:58:26 +02:00
}
core.saveState(stateKey, matchedKey);
log.info(`cache restored from GitHub Actions cache with key: ${matchedKey}`);
core.setOutput(outputKey, true);
2024-08-23 23:58:26 +02:00
}