Set build tracing environment variables after init step

This commit is contained in:
Angela P Wen
2024-08-07 14:13:13 +02:00
parent 5c02493ebf
commit b7be4059f2
15 changed files with 70 additions and 12 deletions

View File

@@ -32,6 +32,7 @@ import {
getActionsStatus,
StatusReportBase,
} from "./status-report";
import { setTracerEnvVariables } from "./tracer-config";
import {
cleanupTrapCaches,
getTotalCacheSize,
@@ -222,6 +223,12 @@ async function run() {
const codeql = await getCodeQL(config.codeQLCmd);
// If the autobuild Action already ran, we do not need to trace any parts of the analyze action
// so we only set the tracer environment variables if autobuild has not completed successfully.
if (process.env[EnvVar.AUTOBUILD_DID_COMPLETE_SUCCESSFULLY] !== "true") {
setTracerEnvVariables(logger);
}
if (hasBadExpectErrorInput()) {
throw new util.ConfigurationError(
"`expect-error` input parameter is for internal use only. It should only be set by codeql-action or a fork.",

View File

@@ -19,7 +19,7 @@ import {
sendStatusReport,
ActionName,
} from "./status-report";
import { endTracingForCluster } from "./tracer-config";
import { endTracingForCluster, setTracerEnvVariables } from "./tracer-config";
import {
checkActionVersion,
checkDiskUsage,
@@ -97,6 +97,7 @@ async function run() {
}
const codeql = await getCodeQL(config.codeQLCmd);
setTracerEnvVariables(logger);
languages = await determineAutobuildLanguages(codeql, config, logger);
if (languages !== undefined) {

View File

@@ -72,6 +72,9 @@ export enum EnvVar {
TESTING_ENVIRONMENT = "CODEQL_ACTION_TESTING_ENVIRONMENT",
/** Stringified representation of environment variables that need to be set for tracing. */
TRACER_CONFIG_ENV = "CODEQL_ACTION_TRACER_CONFIG_ENV",
/** Semver of the CodeQL Action as specified in `package.json`. */
VERSION = "CODEQL_ACTION_VERSION",

View File

@@ -585,10 +585,12 @@ async function run() {
apiDetails,
logger,
);
// Save the requirement variables required for tracing so they can be set in future actions.
if (tracerConfig !== undefined) {
for (const [key, value] of Object.entries(tracerConfig.env)) {
core.exportVariable(key, value);
}
core.exportVariable(
EnvVar.TRACER_CONFIG_ENV,
JSON.stringify(tracerConfig.env),
);
}
// Write diagnostics to the database that we previously stored in memory because the database

View File

@@ -1,6 +1,8 @@
import * as fs from "fs";
import * as path from "path";
import * as core from "@actions/core";
import { type CodeQL } from "./codeql";
import { type Config } from "./config-utils";
import { isTracedLanguage } from "./languages";
@@ -34,6 +36,25 @@ export async function shouldEnableIndirectTracing(
return config.languages.some((l) => isTracedLanguage(l));
}
/**
* Set the tracer environment variables based off of the JSON string in the
* TRACER_CONFIG_ENV environment variable. The variable is populated in the init Action
* and this method is called in future steps where tracing is meant to be enabled.
*/
export function setTracerEnvVariables(logger: Logger): void {
logger.info(
"Setting build tracing environment variables. Subsequent steps of this job will be traced unless otherwise specified.",
);
const tracerConfigEnv: object = JSON.parse(
process.env["CODEQL_ACTION_TRACER_CONFIG_ENV"] ?? "null",
);
if (tracerConfigEnv !== null) {
for (const [key, value] of Object.entries(tracerConfigEnv)) {
core.exportVariable(key, value);
}
}
}
/**
* Delete variables as specified by the end-tracing script
*