Skip to content

Commit ceb85f2

Browse files
henrymercerCopilot
andcommitted
Distinguish GitHub-hosted runners from ones that look hosted
`isHostedRunner` infers hostedness from the runner name and the toolcache path, so it also matches self-hosted runners that are configured to resemble hosted ones. Rename it to `looksLikeHostedRunner` so callers can see they are getting a heuristic, and add `isGitHubHostedRunner`, which reads the `RUNNER_ENVIRONMENT` value the Actions service reports. The existing callers keep the heuristic, so there is no behaviour change. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 5914b03 commit ceb85f2

5 files changed

Lines changed: 26 additions & 10 deletions

File tree

‎lib/entry-points.js‎

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

‎src/actions-util.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,19 @@ export function isSelfHostedRunner(env: Env = getEnv()) {
283283
return env.getOptional(ActionsEnvVars.RUNNER_ENVIRONMENT) === "self-hosted";
284284
}
285285

286+
/**
287+
* Whether the job is running on a runner that GitHub hosts, and whose toolcache is therefore thrown
288+
* away once the job has finished.
289+
*
290+
* Unlike `looksLikeHostedRunner`, this is based on what the service reports for the job rather than
291+
* on how the runner's filesystem happens to be laid out, so it does not match self-hosted runners
292+
* that are configured to resemble hosted ones, such as those that mount a persistent volume at
293+
* `/opt/hostedtoolcache`.
294+
*/
295+
export function isGitHubHostedRunner(env: Env = getEnv()) {
296+
return env.getOptional(ActionsEnvVars.RUNNER_ENVIRONMENT) === "github-hosted";
297+
}
298+
286299
/** Determines whether the workflow trigger is `dynamic`. */
287300
export function isDynamicWorkflow(env: Env = getEnv()): boolean {
288301
return getWorkflowEventName(env) === "dynamic";

‎src/caching-utils.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as core from "@actions/core";
55
import { getOptionalInput, isDefaultSetup } from "./actions-util";
66
import { EnvVar } from "./environment";
77
import { Logger } from "./logging";
8-
import { isHostedRunner, tryGetFolderBytes } from "./util";
8+
import { looksLikeHostedRunner, tryGetFolderBytes } from "./util";
99

1010
/**
1111
* Returns the total size of all the specified paths.
@@ -109,7 +109,7 @@ export function getDependencyCachingEnabled(): CachingKind {
109109
if (dependencyCaching !== undefined) return getCachingKind(dependencyCaching);
110110

111111
// On self-hosted runners which may have dependencies installed centrally, disable caching by default
112-
if (!isHostedRunner()) return CachingKind.None;
112+
if (!looksLikeHostedRunner()) return CachingKind.None;
113113

114114
// Disable in advanced workflows by default.
115115
if (!isDefaultSetup()) return CachingKind.None;

‎src/config-utils.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,8 @@ import {
9090
Result,
9191
Success,
9292
Failure,
93-
isHostedRunner,
93+
looksLikeHostedRunner,
9494
} from "./util";
95-
9695
export { type Config } from "./config/action-config";
9796

9897
/**
@@ -938,7 +937,7 @@ export async function isTrapCachingEnabled(
938937
if (trapCaching !== undefined) return trapCaching === "true";
939938

940939
// On self-hosted runners which may have slow network access, disable TRAP caching by default.
941-
if (!isHostedRunner()) return false;
940+
if (!looksLikeHostedRunner()) return false;
942941

943942
// If overlay analysis is enabled, then disable TRAP caching since overlay analysis supersedes it.
944943
// This change is gated behind a feature flag.

‎src/util.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -842,9 +842,13 @@ export async function checkForTimeout() {
842842
* directory with the name hostedtoolcache which is present on
843843
* GitHub-hosted runners.
844844
*
845-
* @returns true iff the runner is hosted by GitHub
845+
* Since this is a heuristic over how the runner happens to be named and laid out, it also matches
846+
* self-hosted runners that are configured to resemble hosted ones. Prefer
847+
* `isGitHubHostedRunner` when you need the answer the Actions service reports.
848+
*
849+
* @returns true iff the runner looks like it is hosted by GitHub
846850
*/
847-
export function isHostedRunner() {
851+
export function looksLikeHostedRunner() {
848852
return (
849853
// Name of the runner on hosted Windows runners
850854
process.env["RUNNER_NAME"]?.includes("Hosted Agent") ||

0 commit comments

Comments
 (0)