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:
Andrew Eisenberg 2021-06-01 14:49:07 -07:00
parent 539d968ad7
commit 3708898bf2
48 changed files with 387 additions and 250 deletions

91
lib/util.js generated
View file

@ -39,7 +39,9 @@ exports.getExtraOptionsEnvParam = getExtraOptionsEnvParam;
function isLocalRun() {
return (!!process.env.CODEQL_LOCAL_RUN &&
process.env.CODEQL_LOCAL_RUN !== "false" &&
process.env.CODEQL_LOCAL_RUN !== "0");
process.env.CODEQL_LOCAL_RUN !== "0" &&
// local runs only allowed for actions
isActions());
}
exports.isLocalRun = isLocalRun;
/**
@ -242,7 +244,7 @@ function checkGitHubVersionInRange(version, logger, toolName) {
logger.warning(`GitHub Enterprise ${version.version} is too old to be compatible with this version of the CodeQL ${toolName}. If you experience issues, please upgrade to a more recent version of GitHub Enterprise or use an older version of the CodeQL ${toolName}.`);
}
hasBeenWarnedAboutVersion = true;
if (actions_util_1.isActions()) {
if (isActions()) {
core.exportVariable(CODEQL_ACTION_WARNED_ABOUT_VERSION_ENV_VAR, true);
}
}
@ -330,4 +332,89 @@ function assertNever(value) {
throw new ExhaustivityCheckingError(value);
}
exports.assertNever = assertNever;
var Mode;
(function (Mode) {
Mode["actions"] = "Action";
Mode["runner"] = "Runner";
})(Mode = exports.Mode || (exports.Mode = {}));
/**
* Environment variables to be set by codeql-action and used by the
* CLI. These environment variables are relevant for both the runner
* and the action.
*/
var EnvVar;
(function (EnvVar) {
// either 'actions' or 'runner'
EnvVar["RUN_MODE"] = "CODEQL_ACTION_RUN_MODE";
// semver of this action
EnvVar["VERSION"] = "CODEQL_ACTION_VERSION";
// if set to a truthy value, then the action might combine SARIF
// output from several `interpret-results` runs for the same language
EnvVar["FEATURE_SARIF_COMBINE"] = "CODEQL_ACTION_FEATURE_SARIF_COMBINE";
// if set to a truthy value, then the action will upload SARIF,
// not the CLI
EnvVar["FEATURE_WILL_UPLOAD"] = "CODEQL_ACTION_FEATURE_WILL_UPLOAD";
// if set to a truthy value, then the action is using its
// own deprecated and non-standard way of scanning for multiple
// languages
EnvVar["FEATURE_MULTI_LANGUAGE"] = "CODEQL_ACTION_FEATURE_MULTI_LANGUAGE";
// if set to a truthy value, then the action is using its
// own sandwiched workflow mechanism
EnvVar["FEATURE_SANDWICH"] = "CODEQL_ACTION_FEATURE_SANDWICH";
})(EnvVar || (EnvVar = {}));
function initializeEnvironment(mode, version) {
// avoid accessing actions core when in runner mode
if (mode === Mode.actions) {
core.exportVariable(EnvVar.RUN_MODE, mode);
core.exportVariable(EnvVar.VERSION, version);
core.exportVariable(EnvVar.FEATURE_SARIF_COMBINE, "true");
core.exportVariable(EnvVar.FEATURE_WILL_UPLOAD, "true");
core.exportVariable(EnvVar.FEATURE_MULTI_LANGUAGE, "true");
core.exportVariable(EnvVar.FEATURE_SANDWICH, "true");
actions_util_1.prepareLocalRunEnvironment();
}
else {
process.env[EnvVar.RUN_MODE] = mode;
process.env[EnvVar.VERSION] = version;
process.env[EnvVar.FEATURE_SARIF_COMBINE] = "true";
process.env[EnvVar.FEATURE_WILL_UPLOAD] = "true";
process.env[EnvVar.FEATURE_MULTI_LANGUAGE] = "true";
process.env[EnvVar.FEATURE_SANDWICH] = "true";
}
}
exports.initializeEnvironment = initializeEnvironment;
function getMode() {
// 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(EnvVar.RUN_MODE);
if (mode !== Mode.actions && mode !== Mode.runner) {
throw new Error(`Unknown mode: ${mode}.`);
}
return mode;
}
exports.getMode = getMode;
function isActions() {
return getMode() === Mode.actions;
}
exports.isActions = isActions;
/**
* Get an environment parameter, but throw an error if it is not set.
*/
function getRequiredEnvParam(paramName) {
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;
}
exports.getRequiredEnvParam = getRequiredEnvParam;
function getTemporaryDirectory() {
const value = process.env["CODEQL_ACTION_TEMP"];
return value !== undefined && value !== ""
? value
: getRequiredEnvParam("RUNNER_TEMP");
}
exports.getTemporaryDirectory = getTemporaryDirectory;
//# sourceMappingURL=util.js.map