TRAP Caching: Disable on self-hosted runners

This commit is contained in:
Edoardo Pirovano 2022-10-13 14:31:54 +01:00
parent 160e3fe79a
commit 2fafb297de
No known key found for this signature in database
GPG key ID: 047556B5D93FFE28
6 changed files with 61 additions and 7 deletions

View file

@ -39,6 +39,7 @@ import {
getRequiredEnvParam,
getThreadsFlagValue,
initializeEnvironment,
isHostedRunner,
Mode,
} from "./util";
@ -320,10 +321,14 @@ async function run() {
async function getTrapCachingEnabled(
featureEnablement: FeatureEnablement
): Promise<boolean> {
// If the workflow specified something always respect that
const trapCaching = getOptionalInput("trap-caching");
if (trapCaching !== undefined) {
return trapCaching === "true";
}
if (trapCaching !== undefined) return trapCaching === "true";
// On self-hosted runners which may have slow network access, disable TRAP caching by default
if (!isHostedRunner()) return false;
// On hosted runners, respect the feature flag
return await featureEnablement.getValue(Feature.TrapCachingEnabled);
}

View file

@ -892,3 +892,24 @@ export async function withTimeout<T>(
return await Promise.race([mainTask(), timeout]);
}
/**
* This function implements a heuristic to determine whether the
* runner we are on is hosted by GitHub. It does this by checking
* the name of the runner against the list of known GitHub-hosted
* runner names. It also checks for the presence of a toolcache
* directory with the name hostedtoolcache which is present on
* GitHub-hosted runners.
*
* @returns true iff the runner is hosted by GitHub
*/
export function isHostedRunner() {
return (
// Name of the runner on hosted Windows runners
process.env["RUNNER_NAME"]?.includes("Hosted Agent") ||
// Name of the runner on hosted POSIX runners
process.env["RUNNER_NAME"]?.includes("GitHub Actions") ||
// Segment of the path to the tool cache on all hosted runners
process.env["RUNNER_TOOL_CACHE"]?.includes("hostedtoolcache")
);
}