2024-09-05 08:06:45 -04:00
|
|
|
import * as cache from "@actions/cache";
|
|
|
|
import * as core from "@actions/core";
|
|
|
|
import * as exec from "@actions/exec";
|
|
|
|
import {
|
|
|
|
STATE_CACHE_MATCHED_KEY,
|
|
|
|
STATE_CACHE_KEY,
|
|
|
|
} from "./cache/restore-cache";
|
2024-10-25 08:11:32 -04:00
|
|
|
import {
|
|
|
|
cacheLocalPath,
|
|
|
|
enableCache,
|
|
|
|
pruneCache as shouldPruneCache,
|
|
|
|
} from "./utils/inputs";
|
2024-08-23 23:58:26 +02:00
|
|
|
|
|
|
|
export async function run(): Promise<void> {
|
|
|
|
try {
|
|
|
|
if (enableCache) {
|
2024-09-05 08:06:45 -04:00
|
|
|
await saveCache();
|
2024-08-23 23:58:26 +02:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2024-09-05 08:06:45 -04:00
|
|
|
const err = error as Error;
|
|
|
|
core.setFailed(err.message);
|
2024-08-23 23:58:26 +02:00
|
|
|
}
|
2024-11-23 11:58:21 +01:00
|
|
|
// node will stay alive if any promises are not resolved,
|
|
|
|
// which is a possibility if HTTP requests are dangling
|
|
|
|
// due to retries or timeouts. We know that if we got here
|
|
|
|
// that all promises that we care about have successfully
|
|
|
|
// resolved, so simply exit with success.
|
2024-09-05 08:06:45 -04:00
|
|
|
process.exit(0);
|
2024-08-23 23:58:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function saveCache(): Promise<void> {
|
2024-09-05 08:06:45 -04:00
|
|
|
const cacheKey = core.getState(STATE_CACHE_KEY);
|
|
|
|
const matchedKey = core.getState(STATE_CACHE_MATCHED_KEY);
|
2024-08-23 23:58:26 +02:00
|
|
|
|
|
|
|
if (!cacheKey) {
|
2024-09-05 08:06:45 -04:00
|
|
|
core.warning("Error retrieving cache key from state.");
|
|
|
|
return;
|
2024-09-30 02:55:24 -04:00
|
|
|
}
|
|
|
|
if (matchedKey === cacheKey) {
|
2024-09-05 08:06:45 -04:00
|
|
|
core.info(`Cache hit occurred on key ${cacheKey}, not saving cache.`);
|
|
|
|
return;
|
2024-08-23 23:58:26 +02:00
|
|
|
}
|
2024-08-24 10:08:12 +02:00
|
|
|
|
2024-10-25 08:11:32 -04:00
|
|
|
if (shouldPruneCache) {
|
|
|
|
await pruneCache();
|
|
|
|
}
|
2024-08-24 10:08:12 +02:00
|
|
|
|
2024-09-05 08:06:45 -04:00
|
|
|
core.info(`Saving cache path: ${cacheLocalPath}`);
|
|
|
|
await cache.saveCache([cacheLocalPath], cacheKey);
|
2024-08-23 23:58:26 +02:00
|
|
|
|
2024-09-05 08:06:45 -04:00
|
|
|
core.info(`cache saved with the key: ${cacheKey}`);
|
2024-08-23 23:58:26 +02:00
|
|
|
}
|
|
|
|
|
2024-08-24 10:08:12 +02:00
|
|
|
async function pruneCache(): Promise<void> {
|
|
|
|
const options: exec.ExecOptions = {
|
2024-09-05 08:06:45 -04:00
|
|
|
silent: !core.isDebug(),
|
|
|
|
};
|
|
|
|
const execArgs = ["cache", "prune", "--ci"];
|
2024-08-24 10:08:12 +02:00
|
|
|
|
2024-09-05 08:06:45 -04:00
|
|
|
core.info("Pruning cache...");
|
|
|
|
await exec.exec("uv", execArgs, options);
|
2024-08-24 10:08:12 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 08:06:45 -04:00
|
|
|
run();
|