Add CachingKind enum to control whether to restore or store caches

This commit is contained in:
Michael B. Gale 2024-10-14 09:51:23 +01:00
parent 668531eca8
commit 79faaf1396
No known key found for this signature in database
GPG key ID: FF5E2765BD00628F
15 changed files with 195 additions and 57 deletions

3
lib/analyze-action.js generated
View file

@ -35,6 +35,7 @@ const actionsUtil = __importStar(require("./actions-util"));
const analyze_1 = require("./analyze");
const api_client_1 = require("./api-client");
const autobuild_1 = require("./autobuild");
const caching_utils_1 = require("./caching-utils");
const codeql_1 = require("./codeql");
const config_utils_1 = require("./config-utils");
const database_upload_1 = require("./database-upload");
@ -206,7 +207,7 @@ async function run() {
// Clean up TRAP caches
trapCacheCleanupTelemetry = await (0, trap_caching_1.cleanupTrapCaches)(config, features, logger);
// Store dependency cache(s) if dependency caching is enabled.
if (config.dependencyCachingEnabled) {
if ((0, caching_utils_1.shouldStoreCache)(config.dependencyCachingEnabled)) {
await (0, dependency_caching_1.uploadDependencyCaches)(config, logger);
}
// We don't upload results in test mode, so don't wait for processing

File diff suppressed because one or more lines are too long

91
lib/caching-utils.js generated
View file

@ -1,6 +1,37 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CachingKind = void 0;
exports.getTotalCacheSize = getTotalCacheSize;
exports.shouldStoreCache = shouldStoreCache;
exports.shouldRestoreCache = shouldRestoreCache;
exports.getCachingKind = getCachingKind;
exports.getDependencyCachingEnabled = getDependencyCachingEnabled;
const core = __importStar(require("@actions/core"));
const actions_util_1 = require("./actions-util");
const environment_1 = require("./environment");
const util_1 = require("./util");
/**
* Returns the total size of all the specified paths.
@ -12,4 +43,64 @@ async function getTotalCacheSize(paths, logger) {
const sizes = await Promise.all(paths.map((cacheDir) => (0, util_1.tryGetFolderBytes)(cacheDir, logger)));
return sizes.map((a) => a || 0).reduce((a, b) => a + b, 0);
}
/* Enumerates caching modes. */
var CachingKind;
(function (CachingKind) {
/** Do not restore or store any caches. */
CachingKind["None"] = "none";
/** Store caches, but do not restore any existing ones. */
CachingKind["Store"] = "store";
/** Restore existing caches, but do not store any new ones. */
CachingKind["Restore"] = "restore";
/** Restore existing caches, and store new ones. */
CachingKind["Full"] = "full";
})(CachingKind || (exports.CachingKind = CachingKind = {}));
/** Returns a value indicating whether new caches should be stored, based on `kind`. */
function shouldStoreCache(kind) {
return kind === CachingKind.Full || kind === CachingKind.Store;
}
/** Returns a value indicating whether existing caches should be restored, based on `kind`. */
function shouldRestoreCache(kind) {
return kind === CachingKind.Full || kind === CachingKind.Restore;
}
/**
* Parses the `upload` input into an `UploadKind`, converting unspecified and deprecated upload
* inputs appropriately.
*/
function getCachingKind(input) {
switch (input) {
case undefined:
case "none":
case "off":
case "false":
return CachingKind.None;
case "full":
case "on":
case "true":
return CachingKind.Full;
case "store":
return CachingKind.Store;
case "restore":
return CachingKind.Restore;
default:
core.warning(`Unrecognized 'dependency-caching' input: ${input}. Defaulting to 'none'.`);
return CachingKind.None;
}
}
/** Determines whether dependency caching is enabled. */
function getDependencyCachingEnabled() {
// If the workflow specified something always respect that
const dependencyCaching = (0, actions_util_1.getOptionalInput)("dependency-caching") ||
process.env[environment_1.EnvVar.DEPENDENCY_CACHING];
if (dependencyCaching !== undefined)
return getCachingKind(dependencyCaching);
// On self-hosted runners which may have dependencies installed centrally, disable caching by default
if (!(0, util_1.isHostedRunner)())
return CachingKind.None;
// Disable in advanced workflows by default.
if (!(0, actions_util_1.isDefaultSetup)())
return CachingKind.None;
// On hosted runners, enable dependency caching by default
return CachingKind.Full;
}
//# sourceMappingURL=caching-utils.js.map

View file

@ -1 +1 @@
{"version":3,"file":"caching-utils.js","sourceRoot":"","sources":["../src/caching-utils.ts"],"names":[],"mappings":";;AASA,8CAQC;AAhBD,iCAA2C;AAE3C;;;;;GAKG;AACI,KAAK,UAAU,iBAAiB,CACrC,KAAe,EACf,MAAc;IAEd,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,GAAG,CAC7B,KAAK,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAA,wBAAiB,EAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAC7D,CAAC;IACF,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC;AAC7D,CAAC"}
{"version":3,"file":"caching-utils.js","sourceRoot":"","sources":["../src/caching-utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAaA,8CAQC;AAeD,4CAEC;AAGD,gDAEC;AAMD,wCAqBC;AAGD,kEAeC;AAxFD,oDAAsC;AAEtC,iDAAkE;AAClE,+CAAuC;AAEvC,iCAA2D;AAE3D;;;;;GAKG;AACI,KAAK,UAAU,iBAAiB,CACrC,KAAe,EACf,MAAc;IAEd,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,GAAG,CAC7B,KAAK,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAA,wBAAiB,EAAC,QAAQ,EAAE,MA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,OAAO,WAAW,CAAC,IAAI,CAAC;IAC5B,CAAC;AACH,CAAC;AAED,wDAAwD;AACxD,SAAgB,2BAA2B;IACzC,0DAA0D;IAC1D,MAAM,iBAAiB,GACrB,IAAA,+BAAgB,EAAC,oBAAoB,CAAC;QACtC,OAAO,CAAC,GAAG,CAAC,oBAAM,CAAC,kBAAkB,CAAC,CAAC;IACzC,IAAI,iBAAiB,KAAK,SAAS;QAAE,OAAO,cAAc,CAAC,iBAAiB,CAAC,CAAC;IAE9E,qGAAqG;IACrG,IAAI,CAAC,IAAA,qBAAc,GAAE;QAAE,OAAO,WAAW,CAAC,IAAI,CAAC;IAE/C,4CAA4C;IAC5C,IAAI,CAAC,IAAA,6BAAc,GAAE;QAAE,OAAO,WAAW,CAAC,IAAI,CAAC;IAE/C,0DAA0D;IAC1D,OAAO,WAAW,CAAC,IAAI,CAAC;AAC1B,CAAC"}

5
lib/config-utils.js generated
View file

@ -53,6 +53,7 @@ const perf_hooks_1 = require("perf_hooks");
const yaml = __importStar(require("js-yaml"));
const semver = __importStar(require("semver"));
const api = __importStar(require("./api-client"));
const caching_utils_1 = require("./caching-utils");
const feature_flags_1 = require("./feature-flags");
const languages_1 = require("./languages");
const trap_caching_1 = require("./trap-caching");
@ -232,7 +233,7 @@ async function getDefaultConfig({ languagesInput, queriesInput, packsInput, buil
augmentationProperties,
trapCaches,
trapCacheDownloadTime,
dependencyCachingEnabled,
dependencyCachingEnabled: (0, caching_utils_1.getCachingKind)(dependencyCachingEnabled),
};
}
async function downloadCacheWithTime(trapCachingEnabled, codeQL, languages, logger) {
@ -282,7 +283,7 @@ async function loadConfig({ languagesInput, queriesInput, packsInput, buildModeI
augmentationProperties,
trapCaches,
trapCacheDownloadTime,
dependencyCachingEnabled,
dependencyCachingEnabled: (0, caching_utils_1.getCachingKind)(dependencyCachingEnabled),
};
}
/**

File diff suppressed because one or more lines are too long

View file

@ -33,6 +33,7 @@ const ava_1 = __importDefault(require("ava"));
const yaml = __importStar(require("js-yaml"));
const sinon = __importStar(require("sinon"));
const api = __importStar(require("./api-client"));
const caching_utils_1 = require("./caching-utils");
const codeql_1 = require("./codeql");
const configUtils = __importStar(require("./config-utils"));
const feature_flags_1 = require("./feature-flags");
@ -53,7 +54,7 @@ function createTestInitConfigInputs(overrides) {
configInput: undefined,
buildModeInput: undefined,
trapCachingEnabled: false,
dependencyCachingEnabled: false,
dependencyCachingEnabled: caching_utils_1.CachingKind.None,
debugMode: false,
debugArtifactName: "",
debugDatabaseName: "",
@ -287,7 +288,7 @@ function mockListLanguages(languages) {
augmentationProperties: configUtils.defaultAugmentationProperties,
trapCaches: {},
trapCacheDownloadTime: 0,
dependencyCachingEnabled: false,
dependencyCachingEnabled: caching_utils_1.CachingKind.None,
};
const languagesInput = "javascript";
const configFilePath = createConfigFile(inputFileContents, tempDir);

File diff suppressed because one or more lines are too long

21
lib/init-action.js generated
View file

@ -30,6 +30,7 @@ const safe_which_1 = require("@chrisgavin/safe-which");
const uuid_1 = require("uuid");
const actions_util_1 = require("./actions-util");
const api_client_1 = require("./api-client");
const caching_utils_1 = require("./caching-utils");
const configUtils = __importStar(require("./config-utils"));
const dependency_caching_1 = require("./dependency-caching");
const diagnostics_1 = require("./diagnostics");
@ -182,7 +183,7 @@ async function run() {
dbLocation: (0, actions_util_1.getOptionalInput)("db-location"),
configInput: (0, actions_util_1.getOptionalInput)("config"),
trapCachingEnabled: getTrapCachingEnabled(),
dependencyCachingEnabled: getDependencyCachingEnabled(),
dependencyCachingEnabled: (0, caching_utils_1.getDependencyCachingEnabled)(),
// Debug mode is enabled if:
// - The `init` Action is passed `debug: true`.
// - Actions step debugging is enabled (e.g. by [enabling debug logging for a rerun](https://docs.github.com/en/actions/managing-workflow-runs/re-running-workflows-and-jobs#re-running-all-the-jobs-in-a-workflow),
@ -327,7 +328,7 @@ async function run() {
core.exportVariable(bmnVar, value);
}
// Restore dependency cache(s), if they exist.
if (config.dependencyCachingEnabled) {
if ((0, caching_utils_1.shouldRestoreCache)(config.dependencyCachingEnabled)) {
await (0, dependency_caching_1.downloadDependencyCaches)(config.languages, logger);
}
// For CLI versions <2.15.1, build tracing caused errors in MacOS ARM machines with
@ -417,22 +418,6 @@ async function recordZstdAvailability(config, zstdAvailability) {
},
}));
}
/** Determines whether dependency caching is enabled. */
function getDependencyCachingEnabled() {
// If the workflow specified something always respect that
const dependencyCaching = (0, actions_util_1.getOptionalInput)("dependency-caching") ||
process.env[environment_1.EnvVar.DEPENDENCY_CACHING];
if (dependencyCaching !== undefined)
return dependencyCaching === "true";
// On self-hosted runners which may have dependencies installed centrally, disable caching by default
if (!(0, util_1.isHostedRunner)())
return false;
// Disable in advanced workflows by default.
if (!(0, actions_util_1.isDefaultSetup)())
return false;
// On hosted runners, enable dependency caching by default
return true;
}
async function runWrapper() {
try {
await run();

File diff suppressed because one or more lines are too long