Skip to content

Commit a0e258e

Browse files
committed
Make looksLikeHostedRunner env explicit
1 parent b381506 commit a0e258e

7 files changed

Lines changed: 126 additions & 120 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 14 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/caching-utils.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import * as crypto from "crypto";
22

33
import * as core from "@actions/core";
44

5-
import { getOptionalInput, isDefaultSetup } from "./actions-util";
5+
import type { ActionState } from "./action-common";
6+
import { isDefaultSetup } from "./actions-util";
67
import { EnvVar } from "./environment";
78
import { Logger } from "./logging";
89
import { looksLikeHostedRunner, tryGetFolderBytes } from "./util";
@@ -101,18 +102,20 @@ export function createCacheKeyHash(components: Record<string, any>): string {
101102
}
102103

103104
/** Determines whether dependency caching is enabled. */
104-
export function getDependencyCachingEnabled(): CachingKind {
105+
export function getDependencyCachingEnabled(
106+
action: ActionState<["ReadOnlyEnv", "Actions"]>,
107+
): CachingKind {
105108
// If the workflow specified something always respect that
106109
const dependencyCaching =
107-
getOptionalInput("dependency-caching") ||
108-
process.env[EnvVar.DEPENDENCY_CACHING];
110+
action.actions.getOptionalInput("dependency-caching") ||
111+
action.env.getOptional(EnvVar.DEPENDENCY_CACHING);
109112
if (dependencyCaching !== undefined) return getCachingKind(dependencyCaching);
110113

111114
// On self-hosted runners which may have dependencies installed centrally, disable caching by default
112-
if (!looksLikeHostedRunner()) return CachingKind.None;
115+
if (!looksLikeHostedRunner(action.env)) return CachingKind.None;
113116

114117
// Disable in advanced workflows by default.
115-
if (!isDefaultSetup()) return CachingKind.None;
118+
if (!isDefaultSetup(action.env)) return CachingKind.None;
116119

117120
// On hosted runners, disable dependency caching by default.
118121
// TODO: Review later whether we can enable this by default.

‎src/config-utils.test.ts‎

Lines changed: 88 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2132,101 +2132,99 @@ test.serial("getPrimaryAnalysisConfig - Code Scanning + Code Quality", (t) => {
21322132
);
21332133
});
21342134

2135-
test.serial(
2136-
"isTrapCachingEnabled: explicit true input overrides self-hosted default",
2137-
async (t) => {
2138-
return await withTmpDir(async (tmpDir) => {
2139-
setupActionsVars(tmpDir, tmpDir);
2140-
sinon
2141-
.stub(actionsUtil, "getOptionalInput")
2142-
.withArgs("trap-caching")
2143-
.returns("true");
2144-
t.true(configUtils.isTrapCachingEnabled(OverlayDatabaseMode.None));
2145-
});
2146-
},
2147-
);
2135+
test("isTrapCachingEnabled: explicit true input overrides self-hosted default", async (t) => {
2136+
return await withTmpDir(async (tmpDir) => {
2137+
const action = initAllState();
2138+
setupActionsVars(tmpDir, tmpDir, {}, action.env);
2139+
sinon
2140+
.stub(action.actions, "getOptionalInput")
2141+
.withArgs("trap-caching")
2142+
.returns("true");
2143+
t.true(configUtils.isTrapCachingEnabled(action, OverlayDatabaseMode.None));
2144+
});
2145+
});
21482146

2149-
test.serial(
2150-
"isTrapCachingEnabled: explicit true input overrides overlay defaults",
2151-
async (t) => {
2152-
return await withTmpDir(async (tmpDir) => {
2153-
setupActionsVars(tmpDir, path.join(tmpDir, "hostedtoolcache"));
2154-
sinon
2155-
.stub(actionsUtil, "getOptionalInput")
2156-
.withArgs("trap-caching")
2157-
.returns("true");
2158-
for (const overlayDatabaseMode of [
2159-
OverlayDatabaseMode.Overlay,
2160-
OverlayDatabaseMode.OverlayBase,
2161-
]) {
2162-
t.true(configUtils.isTrapCachingEnabled(overlayDatabaseMode));
2163-
}
2164-
});
2165-
},
2166-
);
2147+
test("isTrapCachingEnabled: explicit true input overrides overlay defaults", async (t) => {
2148+
return await withTmpDir(async (tmpDir) => {
2149+
const action = initAllState();
2150+
setupActionsVars(
2151+
tmpDir,
2152+
path.join(tmpDir, "hostedtoolcache"),
2153+
{},
2154+
action.env,
2155+
);
2156+
sinon
2157+
.stub(action.actions, "getOptionalInput")
2158+
.withArgs("trap-caching")
2159+
.returns("true");
2160+
for (const overlayDatabaseMode of [
2161+
OverlayDatabaseMode.Overlay,
2162+
OverlayDatabaseMode.OverlayBase,
2163+
]) {
2164+
t.true(configUtils.isTrapCachingEnabled(action, overlayDatabaseMode));
2165+
}
2166+
});
2167+
});
21672168

2168-
test.serial(
2169-
"isTrapCachingEnabled: explicit false input overrides hosted default",
2170-
async (t) => {
2171-
return await withTmpDir(async (tmpDir) => {
2172-
setupActionsVars(tmpDir, path.join(tmpDir, "hostedtoolcache"));
2173-
sinon
2174-
.stub(actionsUtil, "getOptionalInput")
2175-
.withArgs("trap-caching")
2176-
.returns("false");
2177-
t.false(configUtils.isTrapCachingEnabled(OverlayDatabaseMode.None));
2178-
});
2179-
},
2180-
);
2169+
test("isTrapCachingEnabled: explicit false input overrides hosted default", async (t) => {
2170+
return await withTmpDir(async (tmpDir) => {
2171+
const action = initAllState();
2172+
setupActionsVars(
2173+
tmpDir,
2174+
path.join(tmpDir, "hostedtoolcache"),
2175+
{},
2176+
action.env,
2177+
);
2178+
sinon
2179+
.stub(action.actions, "getOptionalInput")
2180+
.withArgs("trap-caching")
2181+
.returns("false");
2182+
t.false(configUtils.isTrapCachingEnabled(action, OverlayDatabaseMode.None));
2183+
});
2184+
});
21812185

2182-
test.serial(
2183-
"isTrapCachingEnabled: disabled on self-hosted runner by default",
2184-
async (t) => {
2185-
return await withTmpDir(async (tmpDir) => {
2186-
setupActionsVars(tmpDir, tmpDir);
2187-
sinon
2188-
.stub(actionsUtil, "getOptionalInput")
2189-
.withArgs("trap-caching")
2190-
.returns(undefined);
2191-
t.false(configUtils.isTrapCachingEnabled(OverlayDatabaseMode.None));
2192-
});
2193-
},
2194-
);
2186+
test("isTrapCachingEnabled: disabled on self-hosted runner by default", async (t) => {
2187+
return await withTmpDir(async (tmpDir) => {
2188+
const action = initAllState();
2189+
setupActionsVars(tmpDir, tmpDir, {}, action.env);
2190+
sinon
2191+
.stub(action.actions, "getOptionalInput")
2192+
.withArgs("trap-caching")
2193+
.returns(undefined);
2194+
t.false(configUtils.isTrapCachingEnabled(action, OverlayDatabaseMode.None));
2195+
});
2196+
});
21952197

2196-
test.serial(
2197-
"isTrapCachingEnabled: enabled on hosted runner without overlay analysis",
2198-
async (t) => {
2199-
return await withTmpDir(async (tmpDir) => {
2200-
const hostedToolCache = path.join(tmpDir, "hostedtoolcache");
2201-
setupActionsVars(tmpDir, hostedToolCache);
2202-
sinon
2203-
.stub(actionsUtil, "getOptionalInput")
2204-
.withArgs("trap-caching")
2205-
.returns(undefined);
2206-
t.true(configUtils.isTrapCachingEnabled(OverlayDatabaseMode.None));
2207-
});
2208-
},
2209-
);
2198+
test("isTrapCachingEnabled: enabled on hosted runner without overlay analysis", async (t) => {
2199+
return await withTmpDir(async (tmpDir) => {
2200+
const action = initAllState();
2201+
const hostedToolCache = path.join(tmpDir, "hostedtoolcache");
2202+
setupActionsVars(tmpDir, hostedToolCache, {}, action.env);
2203+
sinon
2204+
.stub(action.actions, "getOptionalInput")
2205+
.withArgs("trap-caching")
2206+
.returns(undefined);
2207+
t.true(configUtils.isTrapCachingEnabled(action, OverlayDatabaseMode.None));
2208+
});
2209+
});
22102210

2211-
test.serial(
2212-
"isTrapCachingEnabled: disabled on hosted runner with overlay analysis",
2213-
async (t) => {
2214-
return await withTmpDir(async (tmpDir) => {
2215-
const hostedToolCache = path.join(tmpDir, "hostedtoolcache");
2216-
setupActionsVars(tmpDir, hostedToolCache);
2217-
sinon
2218-
.stub(actionsUtil, "getOptionalInput")
2219-
.withArgs("trap-caching")
2220-
.returns(undefined);
2221-
for (const overlayDatabaseMode of [
2222-
OverlayDatabaseMode.Overlay,
2223-
OverlayDatabaseMode.OverlayBase,
2224-
]) {
2225-
t.false(configUtils.isTrapCachingEnabled(overlayDatabaseMode));
2226-
}
2227-
});
2228-
},
2229-
);
2211+
test("isTrapCachingEnabled: disabled on hosted runner with overlay analysis", async (t) => {
2212+
return await withTmpDir(async (tmpDir) => {
2213+
const action = initAllState();
2214+
const hostedToolCache = path.join(tmpDir, "hostedtoolcache");
2215+
setupActionsVars(tmpDir, hostedToolCache, {}, action.env);
2216+
sinon
2217+
.stub(action.actions, "getOptionalInput")
2218+
.withArgs("trap-caching")
2219+
.returns(undefined);
2220+
for (const overlayDatabaseMode of [
2221+
OverlayDatabaseMode.Overlay,
2222+
OverlayDatabaseMode.OverlayBase,
2223+
]) {
2224+
t.false(configUtils.isTrapCachingEnabled(action, overlayDatabaseMode));
2225+
}
2226+
});
2227+
});
22302228

22312229
test("applyIncrementalAnalysisSettings: no-op when mode is not Overlay and diff ranges are unavailable", async (t) => {
22322230
const config = createTestConfig({});

‎src/config-utils.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import * as yaml from "js-yaml";
77
import { ActionState } from "./action-common";
88
import {
99
getActionVersion,
10-
getOptionalInput,
1110
isAnalyzingPullRequest,
1211
isDefaultSetup,
1312
isDynamicWorkflow,
@@ -951,14 +950,15 @@ async function validateOverlayDatabaseMode(
951950
* @returns Whether TRAP caches should be downloaded and uploaded.
952951
*/
953952
export function isTrapCachingEnabled(
953+
action: ActionState<["ReadOnlyEnv", "Actions"]>,
954954
overlayDatabaseMode: OverlayDatabaseMode,
955955
): boolean {
956956
// If the workflow specified something, always respect that.
957-
const trapCaching = getOptionalInput("trap-caching");
957+
const trapCaching = action.actions.getOptionalInput("trap-caching");
958958
if (trapCaching !== undefined) return trapCaching === "true";
959959

960960
// On self-hosted runners which may have slow network access, disable TRAP caching by default.
961-
if (!looksLikeHostedRunner()) return false;
961+
if (!looksLikeHostedRunner(action.env)) return false;
962962

963963
// Overlay analysis supersedes TRAP caching, so disable TRAP caching by default when it is active.
964964
if (overlayDatabaseMode !== OverlayDatabaseMode.None) {
@@ -1162,7 +1162,7 @@ export async function determineUserConfig(
11621162
* a default config. The parsed config is then stored to a known location.
11631163
*/
11641164
export async function initConfig(
1165-
actionState: ActionState<["Logger", "Env", "FeatureFlags"]>,
1165+
actionState: ActionState<["Logger", "Env", "Actions", "FeatureFlags"]>,
11661166
inputs: InitConfigInputs,
11671167
): Promise<Config> {
11681168
const { logger, features } = actionState;
@@ -1304,7 +1304,7 @@ export async function initConfig(
13041304
logger,
13051305
);
13061306

1307-
if (isTrapCachingEnabled(config.overlayDatabaseMode)) {
1307+
if (isTrapCachingEnabled(actionState, config.overlayDatabaseMode)) {
13081308
const { trapCaches, trapCacheDownloadTime } = await downloadCacheWithTime(
13091309
inputs.codeql,
13101310
config.languages,

‎src/init-action.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ async function run(
392392
configFile,
393393
dbLocation: getOptionalInput("db-location"),
394394
configInput,
395-
dependencyCachingEnabled: getDependencyCachingEnabled(),
395+
dependencyCachingEnabled: getDependencyCachingEnabled(actionState),
396396
// Debug mode is enabled if:
397397
// - The `init` Action is passed `debug: true`.
398398
// - 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),

‎src/init.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export async function initCodeQL(
7575
}
7676

7777
export async function initConfig(
78-
actionState: ActionState<["Logger", "Env", "FeatureFlags"]>,
78+
actionState: ActionState<["Logger", "Env", "Actions", "FeatureFlags"]>,
7979
inputs: configUtils.InitConfigInputs,
8080
): Promise<configUtils.Config> {
8181
return await withGroupAsync("Load language configuration", async () => {

‎src/util.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
exportEnvVar,
2222
getEnv,
2323
type ReadOnlyEnv,
24+
ActionsEnvVars,
2425
} from "./environment";
2526
import { ConfigurationError, getErrorMessage, wrapError } from "./errors";
2627
import { GitHubVariant, type GitHubVersion } from "./github";
@@ -797,14 +798,17 @@ export async function checkForTimeout() {
797798
*
798799
* @returns true iff the runner looks like it is hosted by GitHub
799800
*/
800-
export function looksLikeHostedRunner() {
801+
export function looksLikeHostedRunner(env: ReadOnlyEnv) {
802+
const runnerName = env.getOptional(ActionsEnvVars.RUNNER_NAME);
801803
return (
802804
// Name of the runner on hosted Windows runners
803-
process.env["RUNNER_NAME"]?.includes("Hosted Agent") ||
805+
runnerName?.includes("Hosted Agent") ||
804806
// Name of the runner on hosted POSIX runners
805-
process.env["RUNNER_NAME"]?.includes("GitHub Actions") ||
807+
runnerName?.includes("GitHub Actions") ||
806808
// Segment of the path to the tool cache on all hosted runners
807-
process.env["RUNNER_TOOL_CACHE"]?.includes("hostedtoolcache")
809+
env
810+
.getOptional(ActionsEnvVars.RUNNER_TOOL_CACHE)
811+
?.includes("hostedtoolcache")
808812
);
809813
}
810814

0 commit comments

Comments
 (0)