add outputs python-version and python-cache-hit (#728)

This commit splits up the "normal" cache containing the dependencies and
the "python" cache containing the python binaries. This will lead to a
one-time invalidation of caches.

Closes: #713
This commit is contained in:
Kevin Stillhammer
2026-01-06 18:06:29 +01:00
committed by GitHub
parent 11050edb83
commit 61cb8a9741
10 changed files with 302 additions and 239 deletions

104
dist/setup/index.js generated vendored
View File

@@ -91502,46 +91502,52 @@ var __importStar = (this && this.__importStar) || (function () {
};
})();
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.STATE_CACHE_MATCHED_KEY = exports.STATE_CACHE_KEY = void 0;
exports.STATE_PYTHON_CACHE_MATCHED_KEY = exports.STATE_CACHE_MATCHED_KEY = exports.STATE_CACHE_KEY = void 0;
exports.restoreCache = restoreCache;
const cache = __importStar(__nccwpck_require__(5116));
const core = __importStar(__nccwpck_require__(7484));
const exec = __importStar(__nccwpck_require__(5236));
const hash_files_1 = __nccwpck_require__(9660);
const inputs_1 = __nccwpck_require__(9612);
const platforms_1 = __nccwpck_require__(8361);
exports.STATE_CACHE_KEY = "cache-key";
exports.STATE_CACHE_MATCHED_KEY = "cache-matched-key";
exports.STATE_PYTHON_CACHE_MATCHED_KEY = "python-cache-matched-key";
const CACHE_VERSION = "2";
async function restoreCache() {
const cacheKey = await computeKeys();
async function restoreCache(pythonVersion) {
const cacheKey = await computeKeys(pythonVersion);
core.saveState(exports.STATE_CACHE_KEY, cacheKey);
core.setOutput("cache-key", cacheKey);
if (!inputs_1.restoreCache) {
core.info("restore-cache is false. Skipping restore cache step.");
core.setOutput("python-cache-hit", false);
return;
}
let matchedKey;
core.info(`Trying to restore uv cache from GitHub Actions cache with key: ${cacheKey}`);
if (inputs_1.cacheLocalPath === undefined) {
throw new Error("cache-local-path is not set. Cannot restore cache without a valid cache path.");
}
const cachePaths = [inputs_1.cacheLocalPath.path];
await restoreCacheFromKey(cacheKey, inputs_1.cacheLocalPath.path, exports.STATE_CACHE_MATCHED_KEY, "cache-hit");
if (inputs_1.cachePython) {
cachePaths.push(inputs_1.pythonDir);
await restoreCacheFromKey(`${cacheKey}-python`, inputs_1.pythonDir, exports.STATE_PYTHON_CACHE_MATCHED_KEY, "python-cache-hit");
}
else {
core.setOutput("python-cache-hit", false);
}
}
async function restoreCacheFromKey(cacheKey, cachePath, stateKey, outputKey) {
core.info(`Trying to restore cache from GitHub Actions cache with key: ${cacheKey}`);
let matchedKey;
try {
matchedKey = await cache.restoreCache(cachePaths, cacheKey);
matchedKey = await cache.restoreCache([cachePath], cacheKey);
}
catch (err) {
const message = err.message;
core.warning(message);
core.setOutput("cache-hit", false);
core.setOutput(outputKey, false);
return;
}
handleMatchResult(matchedKey, cacheKey);
handleMatchResult(matchedKey, cacheKey, stateKey, outputKey);
}
async function computeKeys() {
async function computeKeys(pythonVersion) {
let cacheDependencyPathHash = "-";
if (inputs_1.cacheDependencyGlob !== "") {
core.info(`Searching files using cache dependency glob: ${inputs_1.cacheDependencyGlob.split("\n").join(",")}`);
@@ -91554,50 +91560,22 @@ async function computeKeys() {
cacheDependencyPathHash = "-no-dependency-glob";
}
const suffix = inputs_1.cacheSuffix ? `-${inputs_1.cacheSuffix}` : "";
const pythonVersion = await getPythonVersion();
const version = pythonVersion ?? "unknown";
const platform = await (0, platforms_1.getPlatform)();
const osNameVersion = (0, platforms_1.getOSNameVersion)();
const pruned = inputs_1.pruneCache ? "-pruned" : "";
const python = inputs_1.cachePython ? "-py" : "";
return `setup-uv-${CACHE_VERSION}-${(0, platforms_1.getArch)()}-${platform}-${osNameVersion}-${pythonVersion}${pruned}${python}${cacheDependencyPathHash}${suffix}`;
return `setup-uv-${CACHE_VERSION}-${(0, platforms_1.getArch)()}-${platform}-${osNameVersion}-${version}${pruned}${python}${cacheDependencyPathHash}${suffix}`;
}
async function getPythonVersion() {
if (inputs_1.pythonVersion !== "") {
return inputs_1.pythonVersion;
}
let output = "";
const options = {
listeners: {
stdout: (data) => {
output += data.toString();
},
},
silent: !core.isDebug(),
};
try {
const execArgs = ["python", "find", "--directory", inputs_1.workingDirectory];
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;
core.debug(`Failed to get python version from uv. Error: ${err.message}`);
return "unknown";
}
}
function handleMatchResult(matchedKey, primaryKey) {
function handleMatchResult(matchedKey, primaryKey, stateKey, outputKey) {
if (!matchedKey) {
core.info(`No GitHub Actions cache found for key: ${primaryKey}`);
core.setOutput("cache-hit", false);
core.setOutput(outputKey, false);
return;
}
core.saveState(exports.STATE_CACHE_MATCHED_KEY, matchedKey);
core.info(`uv cache restored from GitHub Actions cache with key: ${matchedKey}`);
core.setOutput("cache-hit", true);
core.saveState(stateKey, matchedKey);
core.info(`cache restored from GitHub Actions cache with key: ${matchedKey}`);
core.setOutput(outputKey, true);
}
@@ -96267,6 +96245,34 @@ const constants_1 = __nccwpck_require__(6156);
const inputs_1 = __nccwpck_require__(9612);
const platforms_1 = __nccwpck_require__(8361);
const resolve_1 = __nccwpck_require__(6772);
async function getPythonVersion() {
if (inputs_1.pythonVersion !== "") {
return inputs_1.pythonVersion;
}
let output = "";
const options = {
listeners: {
stdout: (data) => {
output += data.toString();
},
},
silent: !core.isDebug(),
};
try {
const execArgs = ["python", "find", "--directory", inputs_1.workingDirectory];
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;
core.debug(`Failed to get python version from uv. Error: ${err.message}`);
return "unknown";
}
}
async function run() {
detectEmptyWorkdir();
const platform = await (0, platforms_1.getPlatform)();
@@ -96290,8 +96296,10 @@ async function run() {
core.setOutput("uv-version", setupResult.version);
core.saveState(constants_1.STATE_UV_VERSION, setupResult.version);
core.info(`Successfully installed uv version ${setupResult.version}`);
const pythonVersion = await getPythonVersion();
core.setOutput("python-version", pythonVersion);
if (inputs_1.enableCache) {
await (0, restore_cache_1.restoreCache)();
await (0, restore_cache_1.restoreCache)(pythonVersion);
}
// https://github.com/nodejs/node/issues/56645#issuecomment-3077594952
await new Promise((resolve) => setTimeout(resolve, 50));