Add environment variables to signal feature and version to the CLI
This PR ensures environment variables are set before any invocation of the CLI. Here is a list of vars that are set: https://github.com/github/codeql-coreql-team/issues/1124#issuecomment-852463521 This ensures the CLI knows the features and versions of the containing actions/runner. Additionally: - Fix the user agent so that it more closely aligns with user agent spec - Refactor environment variable initialization so that it all happens in one place and call. - Move Mode, getRequiredEnvParam, setMode, getMode out of actions-util and into util. actions-util is meant for utils only called by the action, not the runner. The `prepareLocalRunEnvironment()` method is most likely deprecated and should be removed. I originally added it because I had a way of working where I would run the action from my local machine to test out changes, but this was always a little flaky. So, I no longer use this way of working. I will probably remove it soon.
This commit is contained in:
parent
539d968ad7
commit
3708898bf2
48 changed files with 387 additions and 250 deletions
|
|
@ -8,12 +8,12 @@ import * as yaml from "js-yaml";
|
|||
|
||||
import * as api from "./api-client";
|
||||
import * as sharedEnv from "./shared-environment";
|
||||
import { GITHUB_DOTCOM_URL, isLocalRun } from "./util";
|
||||
import { getRequiredEnvParam, GITHUB_DOTCOM_URL, isLocalRun } from "./util";
|
||||
|
||||
export enum Mode {
|
||||
actions = "Action",
|
||||
runner = "Runner",
|
||||
}
|
||||
/**
|
||||
* The utils in this module are meant to be run inside of the action only.
|
||||
* Code paths from the runner should not enter this module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Wrapper around core.getInput for inputs that always have a value.
|
||||
|
|
@ -38,25 +38,6 @@ export function getOptionalInput(name: string): string | undefined {
|
|||
return value.length > 0 ? value : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an environment parameter, but throw an error if it is not set.
|
||||
*/
|
||||
export function getRequiredEnvParam(paramName: string): string {
|
||||
const value = process.env[paramName];
|
||||
if (value === undefined || value.length === 0) {
|
||||
throw new Error(`${paramName} environment variable must be set`);
|
||||
}
|
||||
core.debug(`${paramName}=${value}`);
|
||||
return value;
|
||||
}
|
||||
|
||||
export function getTemporaryDirectory(): string {
|
||||
const value = process.env["CODEQL_ACTION_TEMP"];
|
||||
return value !== undefined && value !== ""
|
||||
? value
|
||||
: getRequiredEnvParam("RUNNER_TEMP");
|
||||
}
|
||||
|
||||
export function getToolCacheDirectory(): string {
|
||||
const value = process.env["CODEQL_ACTION_TOOL_CACHE"];
|
||||
return value !== undefined && value !== ""
|
||||
|
|
@ -732,30 +713,3 @@ export function getRelativeScriptPath(): string {
|
|||
const actionsDirectory = path.join(path.dirname(runnerTemp), "_actions");
|
||||
return path.relative(actionsDirectory, __filename);
|
||||
}
|
||||
|
||||
const CODEQL_RUN_MODE_ENV_VAR = "CODEQL_RUN_MODE";
|
||||
|
||||
export function setMode(mode: Mode) {
|
||||
// avoid accessing actions core when in runner mode
|
||||
if (mode === Mode.actions) {
|
||||
core.exportVariable(CODEQL_RUN_MODE_ENV_VAR, mode);
|
||||
} else {
|
||||
process.env[CODEQL_RUN_MODE_ENV_VAR] = mode;
|
||||
}
|
||||
}
|
||||
|
||||
export function getMode(): Mode {
|
||||
// Make sure we fail fast if the env var is missing. This should
|
||||
// only happen if there is a bug in our code and we neglected
|
||||
// to set the mode early in the process.
|
||||
const mode = getRequiredEnvParam(CODEQL_RUN_MODE_ENV_VAR);
|
||||
|
||||
if (mode !== Mode.actions && mode !== Mode.runner) {
|
||||
throw new Error(`Unknown mode: ${mode}.`);
|
||||
}
|
||||
return mode;
|
||||
}
|
||||
|
||||
export function isActions(): boolean {
|
||||
return getMode() === Mode.actions;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue