check if running locally

This commit is contained in:
Robert 2020-11-12 14:18:58 +00:00
parent 80b43ca9d3
commit 1737b806ff
6 changed files with 59 additions and 23 deletions

23
lib/actions-util.js generated
View file

@ -7,6 +7,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result; return result;
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const path = __importStar(require("path"));
const core = __importStar(require("@actions/core")); const core = __importStar(require("@actions/core"));
const toolrunnner = __importStar(require("@actions/exec/lib/toolrunner")); const toolrunnner = __importStar(require("@actions/exec/lib/toolrunner"));
const api = __importStar(require("./api-client")); const api = __importStar(require("./api-client"));
@ -190,6 +191,11 @@ async function createStatusReportBase(actionName, status, actionStartedAt, cause
workflowStartedAt = actionStartedAt.toISOString(); workflowStartedAt = actionStartedAt.toISOString();
core.exportVariable(sharedEnv.CODEQL_WORKFLOW_STARTED_AT, workflowStartedAt); core.exportVariable(sharedEnv.CODEQL_WORKFLOW_STARTED_AT, workflowStartedAt);
} }
// If running locally then the GITHUB_ACTION_REF cannot be trusted as it may be for the previous action
// See https://github.com/actions/runner/issues/803
const actionRef = isRunningLocalAction()
? undefined
: process.env["GITHUB_ACTION_REF"];
const statusReport = { const statusReport = {
workflow_run_id: workflowRunID, workflow_run_id: workflowRunID,
workflow_name: workflowName, workflow_name: workflowName,
@ -198,7 +204,7 @@ async function createStatusReportBase(actionName, status, actionStartedAt, cause
commit_oid: commitOid, commit_oid: commitOid,
ref, ref,
action_name: actionName, action_name: actionName,
action_ref: process.env["GITHUB_ACTION_REF"], action_ref: actionRef,
action_oid: "unknown", action_oid: "unknown",
started_at: workflowStartedAt, started_at: workflowStartedAt,
action_started_at: actionStartedAt.toISOString(), action_started_at: actionStartedAt.toISOString(),
@ -268,4 +274,19 @@ async function sendStatusReport(statusReport, ignoreFailures) {
return true; return true;
} }
exports.sendStatusReport = sendStatusReport; exports.sendStatusReport = sendStatusReport;
// Is the current action executing a local copy (i.e. we're running a workflow on the codeql-action repo itself)
// as opposed to running a remote action (i.e. when another repo references us)
function isRunningLocalAction() {
const relativeScriptPath = getRelativeScriptPath();
return (relativeScriptPath.startsWith("..") || path.isAbsolute(relativeScriptPath));
}
exports.isRunningLocalAction = isRunningLocalAction;
// Get the location where the action is runnning from.
// This can be used to get the actions name or tell if we're running a local action.
function getRelativeScriptPath() {
const runnerTemp = getRequiredEnvParam("RUNNER_TEMP");
const actionsDirectory = path.join(path.dirname(runnerTemp), "_actions");
return path.relative(actionsDirectory, __filename);
}
exports.getRelativeScriptPath = getRelativeScriptPath;
//# sourceMappingURL=actions-util.js.map //# sourceMappingURL=actions-util.js.map

File diff suppressed because one or more lines are too long

12
lib/codeql.js generated
View file

@ -54,18 +54,14 @@ function getCodeQLActionRepository(mode, logger) {
} }
// The Actions Runner used with GitHub Enterprise Server 2.22 did not set the GITHUB_ACTION_REPOSITORY variable. // The Actions Runner used with GitHub Enterprise Server 2.22 did not set the GITHUB_ACTION_REPOSITORY variable.
// This fallback logic can be removed after the end-of-support for 2.22 on 2021-09-23. // This fallback logic can be removed after the end-of-support for 2.22 on 2021-09-23.
const runnerTemp = actions_util_1.getRequiredEnvParam("RUNNER_TEMP"); if (actions_util_1.isRunningLocalAction()) {
const actionsDirectory = path.join(path.dirname(runnerTemp), "_actions"); // This handles the case where the Action does not come from an Action repository,
const relativeScriptPath = path.relative(actionsDirectory, __filename); // e.g. our integration tests which use the Action code from the current checkout.
// This handles the case where the Action does not come from an Action repository,
// e.g. our integration tests which use the Action code from the current checkout.
if (relativeScriptPath.startsWith("..") ||
path.isAbsolute(relativeScriptPath)) {
logger.info("The CodeQL Action is checked out locally. Using the default CodeQL Action repository."); logger.info("The CodeQL Action is checked out locally. Using the default CodeQL Action repository.");
return CODEQL_DEFAULT_ACTION_REPOSITORY; return CODEQL_DEFAULT_ACTION_REPOSITORY;
} }
logger.info("GITHUB_ACTION_REPOSITORY environment variable was not set. Falling back to legacy method of finding the GitHub Action."); logger.info("GITHUB_ACTION_REPOSITORY environment variable was not set. Falling back to legacy method of finding the GitHub Action.");
const relativeScriptPathParts = relativeScriptPath.split(path.sep); const relativeScriptPathParts = actions_util_1.getRelativeScriptPath().split(path.sep);
return `${relativeScriptPathParts[0]}/${relativeScriptPathParts[1]}`; return `${relativeScriptPathParts[0]}/${relativeScriptPathParts[1]}`;
} }
async function getCodeQLBundleDownloadURL(githubAuth, githubUrl, mode, logger) { async function getCodeQLBundleDownloadURL(githubAuth, githubUrl, mode, logger) {

File diff suppressed because one or more lines are too long

View file

@ -1,3 +1,5 @@
import * as path from "path";
import * as core from "@actions/core"; import * as core from "@actions/core";
import * as toolrunnner from "@actions/exec/lib/toolrunner"; import * as toolrunnner from "@actions/exec/lib/toolrunner";
@ -249,6 +251,11 @@ export async function createStatusReportBase(
workflowStartedAt workflowStartedAt
); );
} }
// If running locally then the GITHUB_ACTION_REF cannot be trusted as it may be for the previous action
// See https://github.com/actions/runner/issues/803
const actionRef = isRunningLocalAction()
? undefined
: process.env["GITHUB_ACTION_REF"];
const statusReport: StatusReportBase = { const statusReport: StatusReportBase = {
workflow_run_id: workflowRunID, workflow_run_id: workflowRunID,
@ -258,7 +265,7 @@ export async function createStatusReportBase(
commit_oid: commitOid, commit_oid: commitOid,
ref, ref,
action_name: actionName, action_name: actionName,
action_ref: process.env["GITHUB_ACTION_REF"], action_ref: actionRef,
action_oid: "unknown", // TODO decide if it's possible to fill this in action_oid: "unknown", // TODO decide if it's possible to fill this in
started_at: workflowStartedAt, started_at: workflowStartedAt,
action_started_at: actionStartedAt.toISOString(), action_started_at: actionStartedAt.toISOString(),
@ -344,3 +351,20 @@ export async function sendStatusReport<S extends StatusReportBase>(
return true; return true;
} }
// Is the current action executing a local copy (i.e. we're running a workflow on the codeql-action repo itself)
// as opposed to running a remote action (i.e. when another repo references us)
export function isRunningLocalAction(): boolean {
const relativeScriptPath = getRelativeScriptPath();
return (
relativeScriptPath.startsWith("..") || path.isAbsolute(relativeScriptPath)
);
}
// Get the location where the action is runnning from.
// This can be used to get the actions name or tell if we're running a local action.
export function getRelativeScriptPath(): string {
const runnerTemp = getRequiredEnvParam("RUNNER_TEMP");
const actionsDirectory = path.join(path.dirname(runnerTemp), "_actions");
return path.relative(actionsDirectory, __filename);
}

View file

@ -10,7 +10,7 @@ import * as toolcache from "@actions/tool-cache";
import * as semver from "semver"; import * as semver from "semver";
import { v4 as uuidV4 } from "uuid"; import { v4 as uuidV4 } from "uuid";
import { getRequiredEnvParam } from "./actions-util"; import { isRunningLocalAction, getRelativeScriptPath } from "./actions-util";
import * as api from "./api-client"; import * as api from "./api-client";
import * as defaults from "./defaults.json"; // Referenced from codeql-action-sync-tool! import * as defaults from "./defaults.json"; // Referenced from codeql-action-sync-tool!
import { errorMatchers } from "./error-matcher"; import { errorMatchers } from "./error-matcher";
@ -143,15 +143,10 @@ function getCodeQLActionRepository(mode: util.Mode, logger: Logger): string {
// The Actions Runner used with GitHub Enterprise Server 2.22 did not set the GITHUB_ACTION_REPOSITORY variable. // The Actions Runner used with GitHub Enterprise Server 2.22 did not set the GITHUB_ACTION_REPOSITORY variable.
// This fallback logic can be removed after the end-of-support for 2.22 on 2021-09-23. // This fallback logic can be removed after the end-of-support for 2.22 on 2021-09-23.
const runnerTemp = getRequiredEnvParam("RUNNER_TEMP");
const actionsDirectory = path.join(path.dirname(runnerTemp), "_actions"); if (isRunningLocalAction()) {
const relativeScriptPath = path.relative(actionsDirectory, __filename); // This handles the case where the Action does not come from an Action repository,
// This handles the case where the Action does not come from an Action repository, // e.g. our integration tests which use the Action code from the current checkout.
// e.g. our integration tests which use the Action code from the current checkout.
if (
relativeScriptPath.startsWith("..") ||
path.isAbsolute(relativeScriptPath)
) {
logger.info( logger.info(
"The CodeQL Action is checked out locally. Using the default CodeQL Action repository." "The CodeQL Action is checked out locally. Using the default CodeQL Action repository."
); );
@ -160,7 +155,7 @@ function getCodeQLActionRepository(mode: util.Mode, logger: Logger): string {
logger.info( logger.info(
"GITHUB_ACTION_REPOSITORY environment variable was not set. Falling back to legacy method of finding the GitHub Action." "GITHUB_ACTION_REPOSITORY environment variable was not set. Falling back to legacy method of finding the GitHub Action."
); );
const relativeScriptPathParts = relativeScriptPath.split(path.sep); const relativeScriptPathParts = getRelativeScriptPath().split(path.sep);
return `${relativeScriptPathParts[0]}/${relativeScriptPathParts[1]}`; return `${relativeScriptPathParts[0]}/${relativeScriptPathParts[1]}`;
} }