List matching Actions caches

This commit is contained in:
Henry Mercer 2024-05-21 15:44:39 +01:00
parent b1bd8da5e7
commit cc96c825ba
9 changed files with 78 additions and 15 deletions

25
lib/trap-caching.js generated
View file

@ -23,11 +23,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTotalCacheSize = exports.getLanguagesSupportingCaching = exports.uploadTrapCaches = exports.downloadTrapCaches = void 0;
exports.getTotalCacheSize = exports.getLanguagesSupportingCaching = exports.cleanupTrapCaches = exports.uploadTrapCaches = exports.downloadTrapCaches = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const cache = __importStar(require("@actions/cache"));
const actionsCache = __importStar(require("@actions/cache"));
const actionsUtil = __importStar(require("./actions-util"));
const apiClient = __importStar(require("./api-client"));
const util_1 = require("./util");
// This constant should be bumped if we make a breaking change
// to how the CodeQL Action stores or retrieves the TRAP cache,
@ -35,6 +36,7 @@ const util_1 = require("./util");
// this for CLI/extractor changes, since the CLI version also
// goes into the cache key.
const CACHE_VERSION = 1;
const CODEQL_TRAP_CACHE_PREFIX = "codeql-trap";
// This constant sets the minimum size in megabytes of a TRAP
// cache for us to consider it worth uploading.
const MINIMUM_CACHE_MB_TO_UPLOAD = 10;
@ -81,7 +83,7 @@ async function downloadTrapCaches(codeql, languages, logger) {
// The SHA from the base of the PR is the most similar commit we might have a cache for
const preferredKey = await cacheKey(codeql, language, baseSha);
logger.info(`Looking in Actions cache for TRAP cache with key ${preferredKey}`);
const found = await (0, util_1.withTimeout)(MAX_CACHE_OPERATION_MS, cache.restoreCache([cacheDir], preferredKey, [
const found = await (0, util_1.withTimeout)(MAX_CACHE_OPERATION_MS, actionsCache.restoreCache([cacheDir], preferredKey, [
// Fall back to any cache with the right key prefix
await cachePrefix(codeql, language),
]), () => {
@ -123,13 +125,26 @@ async function uploadTrapCaches(codeql, config, logger) {
}
const key = await cacheKey(codeql, language, process.env.GITHUB_SHA || "unknown");
logger.info(`Uploading TRAP cache to Actions cache with key ${key}`);
await (0, util_1.withTimeout)(MAX_CACHE_OPERATION_MS, cache.saveCache([cacheDir], key), () => {
await (0, util_1.withTimeout)(MAX_CACHE_OPERATION_MS, actionsCache.saveCache([cacheDir], key), () => {
logger.info(`Timed out waiting for TRAP cache for ${language} to upload, will continue without uploading`);
});
}
return true;
}
exports.uploadTrapCaches = uploadTrapCaches;
async function cleanupTrapCaches(logger) {
const event = actionsUtil.getWorkflowEvent();
const defaultBranch = event?.repository?.default_branch;
if (!defaultBranch) {
logger.info("Could not determine default branch, skipping TRAP cache cleanup");
return;
}
const matchingCaches = await apiClient.listActionsCaches(CODEQL_TRAP_CACHE_PREFIX, defaultBranch);
for (const cache of matchingCaches) {
logger.info(`Matched Actions cache ${JSON.stringify(cache)}`);
}
}
exports.cleanupTrapCaches = cleanupTrapCaches;
async function getLanguagesSupportingCaching(codeql, languages, logger) {
const result = [];
const resolveResult = await codeql.betterResolveLanguages();
@ -169,6 +184,6 @@ async function cacheKey(codeql, language, baseSha) {
return `${await cachePrefix(codeql, language)}${baseSha}`;
}
async function cachePrefix(codeql, language) {
return `codeql-trap-${CACHE_VERSION}-${(await codeql.getVersion()).version}-${language}-`;
return `${CODEQL_TRAP_CACHE_PREFIX}-${CACHE_VERSION}-${(await codeql.getVersion()).version}-${language}-`;
}
//# sourceMappingURL=trap-caching.js.map