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.

115 lines
3.4 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 * as exec from "@actions/exec";
import { hashFiles } from "../hash/hash-files";
2024-09-05 08:06:45 -04:00
import {
cacheDependencyGlob,
cacheLocalPath,
cacheSuffix,
2025-05-23 09:43:52 +02:00
pruneCache,
2024-12-13 20:52:12 +01:00
pythonVersion as pythonVersionInput,
restoreCache as shouldRestoreCache,
workingDirectory,
2024-09-05 08:06:45 -04:00
} from "../utils/inputs";
import { getArch, 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";
const CACHE_VERSION = "1";
2024-08-23 23:58:26 +02:00
2024-12-22 12:12:29 +01:00
export async function restoreCache(): Promise<void> {
const cacheKey = await computeKeys();
core.saveState(STATE_CACHE_KEY, cacheKey);
if (!shouldRestoreCache) {
core.info("restore-cache is false. Skipping restore cache step.");
return;
}
2024-08-23 23:58:26 +02:00
2024-09-05 08:06:45 -04:00
let matchedKey: string | undefined;
2024-08-23 23:58:26 +02:00
core.info(
2024-09-05 08:06:45 -04:00
`Trying to restore uv cache from GitHub Actions cache with key: ${cacheKey}`,
);
2024-08-23 23:58:26 +02:00
try {
2024-09-05 08:06:45 -04:00
matchedKey = await cache.restoreCache([cacheLocalPath], 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;
core.warning(message);
core.setOutput("cache-hit", false);
return;
2024-08-23 23:58:26 +02:00
}
2024-09-05 08:06:45 -04:00
handleMatchResult(matchedKey, cacheKey);
2024-08-23 23:58:26 +02:00
}
2024-12-22 12:12:29 +01:00
async function computeKeys(): Promise<string> {
2024-09-05 08:06:45 -04:00
let cacheDependencyPathHash = "-";
if (cacheDependencyGlob !== "") {
core.info(
`Searching files using cache dependency glob: ${cacheDependencyGlob.split("\n").join(",")}`,
);
2024-11-23 09:21:51 +01:00
cacheDependencyPathHash += await hashFiles(cacheDependencyGlob, true);
2024-09-05 08:06:45 -04:00
if (cacheDependencyPathHash === "-") {
core.warning(
`No file matched to [${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
}
2024-09-05 08:06:45 -04:00
const suffix = cacheSuffix ? `-${cacheSuffix}` : "";
2024-12-13 20:52:12 +01:00
const pythonVersion = await getPythonVersion();
2025-02-17 10:32:34 +01:00
const platform = await getPlatform();
2025-05-23 09:43:52 +02:00
const pruned = pruneCache ? "-pruned" : "";
return `setup-uv-${CACHE_VERSION}-${getArch()}-${platform}-${pythonVersion}${pruned}${cacheDependencyPathHash}${suffix}`;
2024-12-13 20:52:12 +01:00
}
async function getPythonVersion(): Promise<string> {
if (pythonVersionInput !== "") {
return pythonVersionInput;
}
let output = "";
const options: exec.ExecOptions = {
listeners: {
stdout: (data: Buffer) => {
output += data.toString();
},
},
2025-08-12 22:12:10 +02:00
silent: !core.isDebug(),
2024-12-13 20:52:12 +01:00
};
try {
const execArgs = ["python", "find", "--directory", workingDirectory];
2024-12-13 20:52:12 +01:00
await exec.exec("uv", execArgs, options);
const pythonPath = output.trim();
output = "";
await exec.exec(pythonPath, ["--version"], options);
// output is like "Python 3.8.10"
return output.split(" ")[1].trim();
} catch (error) {
const err = error as Error;
core.debug(`Failed to get python version from uv. Error: ${err.message}`);
return "unknown";
}
2024-08-23 23:58:26 +02:00
}
function handleMatchResult(
matchedKey: string | undefined,
2024-09-05 08:06:45 -04:00
primaryKey: string,
2024-08-23 23:58:26 +02:00
): void {
if (!matchedKey) {
2024-09-05 08:06:45 -04:00
core.info(`No GitHub Actions cache found for key: ${primaryKey}`);
core.setOutput("cache-hit", false);
return;
2024-08-23 23:58:26 +02:00
}
2024-09-05 08:06:45 -04:00
core.saveState(STATE_CACHE_MATCHED_KEY, matchedKey);
2024-08-23 23:58:26 +02:00
core.info(
2024-09-05 08:06:45 -04:00
`uv cache restored from GitHub Actions cache with key: ${matchedKey}`,
);
core.setOutput("cache-hit", true);
2024-08-23 23:58:26 +02:00
}