2024-07-22 17:29:56 +03:00
|
|
|
import * as os from "os";
|
|
|
|
import { join } from "path";
|
|
|
|
|
|
|
|
import * as actionsCache from "@actions/cache";
|
|
|
|
import * as glob from "@actions/glob";
|
|
|
|
|
|
|
|
import { getTotalCacheSize } from "./caching-utils";
|
|
|
|
import { Config } from "./config-utils";
|
2024-10-07 14:07:36 +03:00
|
|
|
import { EnvVar } from "./environment";
|
2024-07-22 17:29:56 +03:00
|
|
|
import { Language } from "./languages";
|
|
|
|
import { Logger } from "./logging";
|
|
|
|
import { getRequiredEnvParam } from "./util";
|
|
|
|
|
2024-07-26 15:22:46 +03:00
|
|
|
/**
|
|
|
|
* Caching configuration for a particular language.
|
|
|
|
*/
|
2024-07-22 17:29:56 +03:00
|
|
|
interface CacheConfig {
|
2024-07-26 15:22:46 +03:00
|
|
|
/** The paths of directories on the runner that should be included in the cache. */
|
2024-07-22 17:29:56 +03:00
|
|
|
paths: string[];
|
2024-07-26 15:22:46 +03:00
|
|
|
/**
|
|
|
|
* Patterns for the paths of files whose contents affect which dependencies are used
|
|
|
|
* by a project. We find all files which match these patterns, calculate a hash for
|
|
|
|
* their contents, and use that hash as part of the cache key.
|
|
|
|
*/
|
2024-07-22 17:29:56 +03:00
|
|
|
hash: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
const CODEQL_DEPENDENCY_CACHE_PREFIX = "codeql-dependencies";
|
|
|
|
const CODEQL_DEPENDENCY_CACHE_VERSION = 1;
|
|
|
|
|
2024-07-26 15:22:46 +03:00
|
|
|
/**
|
|
|
|
* Default caching configurations per language.
|
|
|
|
*/
|
2024-07-22 17:29:56 +03:00
|
|
|
const CODEQL_DEFAULT_CACHE_CONFIG: { [language: string]: CacheConfig } = {
|
|
|
|
java: {
|
|
|
|
paths: [
|
|
|
|
// Maven
|
|
|
|
join(os.homedir(), ".m2", "repository"),
|
|
|
|
// Gradle
|
|
|
|
join(os.homedir(), ".gradle", "caches"),
|
|
|
|
],
|
|
|
|
hash: [
|
|
|
|
// Maven
|
|
|
|
"**/pom.xml",
|
|
|
|
// Gradle
|
|
|
|
"**/*.gradle*",
|
|
|
|
"**/gradle-wrapper.properties",
|
|
|
|
"buildSrc/**/Versions.kt",
|
|
|
|
"buildSrc/**/Dependencies.kt",
|
|
|
|
"gradle/*.versions.toml",
|
|
|
|
"**/versions.properties",
|
|
|
|
],
|
|
|
|
},
|
2024-07-23 14:03:28 +03:00
|
|
|
csharp: {
|
|
|
|
paths: [join(os.homedir(), ".nuget", "packages")],
|
|
|
|
hash: [
|
|
|
|
// NuGet
|
|
|
|
"**/packages.lock.json",
|
2024-10-14 11:26:28 +03:00
|
|
|
// Paket
|
|
|
|
"**/paket.lock",
|
2024-07-23 14:03:28 +03:00
|
|
|
],
|
|
|
|
},
|
2024-07-23 14:08:31 +03:00
|
|
|
go: {
|
|
|
|
paths: [join(os.homedir(), "go", "pkg", "mod")],
|
|
|
|
hash: ["**/go.sum"],
|
|
|
|
},
|
2024-07-22 17:29:56 +03:00
|
|
|
};
|
|
|
|
|
2024-07-26 15:25:26 +03:00
|
|
|
async function makeGlobber(patterns: string[]): Promise<glob.Globber> {
|
|
|
|
return glob.create(patterns.join("\n"));
|
2024-07-23 14:38:04 +03:00
|
|
|
}
|
|
|
|
|
2024-07-22 17:29:56 +03:00
|
|
|
/**
|
|
|
|
* Attempts to restore dependency caches for the languages being analyzed.
|
|
|
|
*
|
|
|
|
* @param languages The languages being analyzed.
|
|
|
|
* @param logger A logger to record some informational messages to.
|
|
|
|
* @returns A list of languages for which dependency caches were restored.
|
|
|
|
*/
|
|
|
|
export async function downloadDependencyCaches(
|
|
|
|
languages: Language[],
|
|
|
|
logger: Logger,
|
|
|
|
): Promise<Language[]> {
|
|
|
|
const restoredCaches: Language[] = [];
|
|
|
|
|
|
|
|
for (const language of languages) {
|
|
|
|
const cacheConfig = CODEQL_DEFAULT_CACHE_CONFIG[language];
|
|
|
|
|
|
|
|
if (cacheConfig === undefined) {
|
|
|
|
logger.info(
|
|
|
|
`Skipping download of dependency cache for ${language} as we have no caching configuration for it.`,
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-07-23 14:38:04 +03:00
|
|
|
// Check that we can find files to calculate the hash for the cache key from, so we don't end up
|
|
|
|
// with an empty string.
|
|
|
|
const globber = await makeGlobber(cacheConfig.hash);
|
|
|
|
|
|
|
|
if ((await globber.glob()).length === 0) {
|
|
|
|
logger.info(
|
|
|
|
`Skipping download of dependency cache for ${language} as we cannot calculate a hash for the cache key.`,
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-07-22 17:29:56 +03:00
|
|
|
const primaryKey = await cacheKey(language, cacheConfig);
|
|
|
|
const restoreKeys: string[] = [await cachePrefix(language)];
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
`Downloading cache for ${language} with key ${primaryKey} and restore keys ${restoreKeys.join(
|
|
|
|
", ",
|
|
|
|
)}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
const hitKey = await actionsCache.restoreCache(
|
|
|
|
cacheConfig.paths,
|
|
|
|
primaryKey,
|
|
|
|
restoreKeys,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (hitKey !== undefined) {
|
|
|
|
logger.info(`Cache hit on key ${hitKey} for ${language}.`);
|
|
|
|
restoredCaches.push(language);
|
2024-10-07 13:25:44 +03:00
|
|
|
} else {
|
|
|
|
logger.info(`No suitable cache found for ${language}.`);
|
2024-07-22 17:29:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return restoredCaches;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to store caches for the languages that were analyzed.
|
|
|
|
*
|
|
|
|
* @param config The configuration for this workflow.
|
|
|
|
* @param logger A logger to record some informational messages to.
|
|
|
|
*/
|
|
|
|
export async function uploadDependencyCaches(config: Config, logger: Logger) {
|
|
|
|
for (const language of config.languages) {
|
|
|
|
const cacheConfig = CODEQL_DEFAULT_CACHE_CONFIG[language];
|
|
|
|
|
|
|
|
if (cacheConfig === undefined) {
|
|
|
|
logger.info(
|
|
|
|
`Skipping upload of dependency cache for ${language} as we have no caching configuration for it.`,
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-07-23 14:38:04 +03:00
|
|
|
// Check that we can find files to calculate the hash for the cache key from, so we don't end up
|
|
|
|
// with an empty string.
|
|
|
|
const globber = await makeGlobber(cacheConfig.hash);
|
|
|
|
|
|
|
|
if ((await globber.glob()).length === 0) {
|
|
|
|
logger.info(
|
|
|
|
`Skipping upload of dependency cache for ${language} as we cannot calculate a hash for the cache key.`,
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
2024-07-22 17:29:56 +03:00
|
|
|
|
2024-10-29 15:04:46 +03:00
|
|
|
// Calculate the size of the files that we would store in the cache. We use this to determine whether the
|
|
|
|
// cache should be saved or not. For example, if there are no files to store, then we skip creating the
|
|
|
|
// cache. In the future, we could also:
|
|
|
|
// - Skip uploading caches with a size below some threshold: this makes sense for avoiding the overhead
|
|
|
|
// of storing and restoring small caches, but does not help with alert wobble if a package repository
|
|
|
|
// cannot be reached in a given run.
|
|
|
|
// - Skip uploading caches with a size above some threshold: this could be a concern if other workflows
|
|
|
|
// use the cache quota that we compete with. In that case, we do not wish to use up all of the quota
|
|
|
|
// with the dependency caches. For this, we could use the Cache API to check whether other workflows
|
|
|
|
// are using the quota and how full it is.
|
2024-11-12 18:16:18 +03:00
|
|
|
const size = await getTotalCacheSize(cacheConfig.paths, logger, true);
|
2024-07-26 15:42:40 +03:00
|
|
|
|
|
|
|
// Skip uploading an empty cache.
|
|
|
|
if (size === 0) {
|
|
|
|
logger.info(
|
|
|
|
`Skipping upload of dependency cache for ${language} since it is empty.`,
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-07-22 17:29:56 +03:00
|
|
|
const key = await cacheKey(language, cacheConfig);
|
|
|
|
|
|
|
|
logger.info(
|
2024-11-12 18:08:09 +03:00
|
|
|
`Uploading cache of size ${size} for ${language} with key ${key}...`,
|
2024-07-22 17:29:56 +03:00
|
|
|
);
|
|
|
|
|
2024-11-12 18:08:09 +03:00
|
|
|
try {
|
|
|
|
await actionsCache.saveCache(cacheConfig.paths, key);
|
|
|
|
} catch (error) {
|
|
|
|
// `ReserveCacheError` indicates that the cache key is already in use, which means that a
|
|
|
|
// cache with that key already exists or is in the process of being uploaded by another
|
|
|
|
// workflow. We can ignore this.
|
|
|
|
if (error instanceof actionsCache.ReserveCacheError) {
|
|
|
|
logger.info(
|
|
|
|
`Not uploading cache for ${language}, because ${key} is already in use.`,
|
|
|
|
);
|
|
|
|
logger.debug(error.message);
|
|
|
|
} else {
|
|
|
|
// Propagate other errors upwards.
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2024-07-22 17:29:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes a cache key for the specified language.
|
|
|
|
*
|
|
|
|
* @param language The language being analyzed.
|
|
|
|
* @param cacheConfig The cache configuration for the language.
|
|
|
|
* @returns A cache key capturing information about the project(s) being analyzed in the specified language.
|
|
|
|
*/
|
|
|
|
async function cacheKey(
|
|
|
|
language: Language,
|
|
|
|
cacheConfig: CacheConfig,
|
|
|
|
): Promise<string> {
|
|
|
|
const hash = await glob.hashFiles(cacheConfig.hash.join("\n"));
|
|
|
|
return `${await cachePrefix(language)}${hash}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a prefix for the cache key, comprised of a CodeQL-specific prefix, a version number that
|
|
|
|
* can be changed to invalidate old caches, the runner's operating system, and the specified language name.
|
|
|
|
*
|
|
|
|
* @param language The language being analyzed.
|
|
|
|
* @returns The prefix that identifies what a cache is for.
|
|
|
|
*/
|
|
|
|
async function cachePrefix(language: Language): Promise<string> {
|
|
|
|
const runnerOs = getRequiredEnvParam("RUNNER_OS");
|
2024-10-07 12:59:35 +03:00
|
|
|
const customPrefix = process.env[EnvVar.DEPENDENCY_CACHING_PREFIX];
|
|
|
|
let prefix = CODEQL_DEPENDENCY_CACHE_PREFIX;
|
|
|
|
|
|
|
|
if (customPrefix !== undefined && customPrefix.length > 0) {
|
|
|
|
prefix = `${prefix}-${customPrefix}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${prefix}-${CODEQL_DEPENDENCY_CACHE_VERSION}-${runnerOs}-${language}-`;
|
2024-07-22 17:29:56 +03:00
|
|
|
}
|