Merge pull request #2488 from github/henrymercer/debug-artifacts-better-logging

Improve logging when preparing and uploading debug artifacts
This commit is contained in:
Henry Mercer 2024-09-17 21:07:52 +01:00 committed by GitHub
commit 34666c10b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 185 additions and 108 deletions

View file

@ -31,13 +31,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core")); const core = __importStar(require("@actions/core"));
const debugArtifacts = __importStar(require("./debug-artifacts")); const debugArtifacts = __importStar(require("./debug-artifacts"));
const environment_1 = require("./environment"); const environment_1 = require("./environment");
const logging_1 = require("./logging");
const util_1 = require("./util"); const util_1 = require("./util");
async function runWrapper() { async function runWrapper() {
try { try {
const logger = (0, logging_1.getActionsLogger)();
// Upload SARIF artifacts if we determine that this is a first-party analysis run. // Upload SARIF artifacts if we determine that this is a first-party analysis run.
// For third-party runs, this artifact will be uploaded in the `upload-sarif-post` step. // For third-party runs, this artifact will be uploaded in the `upload-sarif-post` step.
if (process.env[environment_1.EnvVar.INIT_ACTION_HAS_RUN] === "true") { if (process.env[environment_1.EnvVar.INIT_ACTION_HAS_RUN] === "true") {
await debugArtifacts.uploadCombinedSarifArtifacts(); await (0, logging_1.withGroup)("Uploading combined SARIF debug artifact", () => debugArtifacts.uploadCombinedSarifArtifacts(logger));
} }
} }
catch (error) { catch (error) {

View file

@ -1 +1 @@
{"version":3,"file":"analyze-action-post.js","sourceRoot":"","sources":["../src/analyze-action-post.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;GAIG;AACH,oDAAsC;AAEtC,kEAAoD;AACpD,+CAAuC;AACvC,iCAAyC;AAEzC,KAAK,UAAU,UAAU;IACvB,IAAI,CAAC;QACH,kFAAkF;QAClF,wFAAwF;QACxF,IAAI,OAAO,CAAC,GAAG,CAAC,oBAAM,CAAC,mBAAmB,CAAC,KAAK,MAAM,EAAE,CAAC;YACvD,MAAM,cAAc,CAAC,4BAA4B,EAAE,CAAC;QACtD,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,CAAC,SAAS,CACZ,oCAAoC,IAAA,sBAAe,EAAC,KAAK,CAAC,EAAE,CAC7D,CAAC;IACJ,CAAC;AACH,CAAC;AAED,KAAK,UAAU,EAAE,CAAC"} {"version":3,"file":"analyze-action-post.js","sourceRoot":"","sources":["../src/analyze-action-post.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;GAIG;AACH,oDAAsC;AAEtC,kEAAoD;AACpD,+CAAuC;AACvC,uCAAwD;AACxD,iCAAyC;AAEzC,KAAK,UAAU,UAAU;IACvB,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAA,0BAAgB,GAAE,CAAC;QAElC,kFAAkF;QAClF,wFAAwF;QACxF,IAAI,OAAO,CAAC,GAAG,CAAC,oBAAM,CAAC,mBAAmB,CAAC,KAAK,MAAM,EAAE,CAAC;YACvD,MAAM,IAAA,mBAAS,EAAC,yCAAyC,EAAE,GAAG,EAAE,CAC9D,cAAc,CAAC,4BAA4B,CAAC,MAAM,CAAC,CACpD,CAAC;QACJ,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,CAAC,SAAS,CACZ,oCAAoC,IAAA,sBAAe,EAAC,KAAK,CAAC,EAAE,CAC7D,CAAC;IACJ,CAAC;AACH,CAAC;AAED,KAAK,UAAU,EAAE,CAAC"}

100
lib/debug-artifacts.js generated
View file

@ -40,6 +40,7 @@ const actions_util_1 = require("./actions-util");
const analyze_1 = require("./analyze"); const analyze_1 = require("./analyze");
const codeql_1 = require("./codeql"); const codeql_1 = require("./codeql");
const environment_1 = require("./environment"); const environment_1 = require("./environment");
const logging_1 = require("./logging");
const util_1 = require("./util"); const util_1 = require("./util");
function sanitizeArtifactName(name) { function sanitizeArtifactName(name) {
return name.replace(/[^a-zA-Z0-9_\\-]+/g, ""); return name.replace(/[^a-zA-Z0-9_\\-]+/g, "");
@ -48,11 +49,11 @@ function sanitizeArtifactName(name) {
* Upload Actions SARIF artifacts for debugging when CODEQL_ACTION_DEBUG_COMBINED_SARIF * Upload Actions SARIF artifacts for debugging when CODEQL_ACTION_DEBUG_COMBINED_SARIF
* environment variable is set * environment variable is set
*/ */
async function uploadCombinedSarifArtifacts() { async function uploadCombinedSarifArtifacts(logger) {
const tempDir = (0, actions_util_1.getTemporaryDirectory)(); const tempDir = (0, actions_util_1.getTemporaryDirectory)();
// Upload Actions SARIF artifacts for debugging when environment variable is set // Upload Actions SARIF artifacts for debugging when environment variable is set
if (process.env["CODEQL_ACTION_DEBUG_COMBINED_SARIF"] === "true") { if (process.env["CODEQL_ACTION_DEBUG_COMBINED_SARIF"] === "true") {
core.info("Uploading available combined SARIF files as Actions debugging artifact..."); logger.info("Uploading available combined SARIF files as Actions debugging artifact...");
const baseTempDir = path.resolve(tempDir, "combined-sarif"); const baseTempDir = path.resolve(tempDir, "combined-sarif");
const toUpload = []; const toUpload = [];
if (fs.existsSync(baseTempDir)) { if (fs.existsSync(baseTempDir)) {
@ -66,17 +67,20 @@ async function uploadCombinedSarifArtifacts() {
} }
} }
} }
if (toUpload.length > 0) { try {
await uploadDebugArtifacts(toUpload, baseTempDir, "combined-sarif-artifacts"); await uploadDebugArtifacts(toUpload, baseTempDir, "combined-sarif-artifacts");
} }
catch (e) {
logger.warning(`Failed to upload combined SARIF files as Actions debugging artifact. Reason: ${(0, util_1.getErrorMessage)(e)}`);
}
} }
} }
/** /**
* Try to get the SARIF result path for the given language. * Try to prepare a SARIF result debug artifact for the given language.
* *
* If an error occurs, log it and return an empty list. * @return The path to that debug artifact, or undefined if an error occurs.
*/ */
function tryGetSarifResultPath(config, language, logger) { function tryPrepareSarifDebugArtifact(config, language, logger) {
try { try {
const analyzeActionOutputDir = process.env[environment_1.EnvVar.SARIF_RESULTS_OUTPUT_DIR]; const analyzeActionOutputDir = process.env[environment_1.EnvVar.SARIF_RESULTS_OUTPUT_DIR];
if (analyzeActionOutputDir !== undefined && if (analyzeActionOutputDir !== undefined &&
@ -87,37 +91,36 @@ function tryGetSarifResultPath(config, language, logger) {
if (fs.existsSync(sarifFile)) { if (fs.existsSync(sarifFile)) {
const sarifInDbLocation = path.resolve(config.dbLocation, `${language}.sarif`); const sarifInDbLocation = path.resolve(config.dbLocation, `${language}.sarif`);
fs.copyFileSync(sarifFile, sarifInDbLocation); fs.copyFileSync(sarifFile, sarifInDbLocation);
return [sarifInDbLocation]; return sarifInDbLocation;
} }
} }
} }
catch (e) { catch (e) {
logger.warning(`Failed to find SARIF results path for ${language}. Reason: ${(0, util_1.getErrorMessage)(e)}`); logger.warning(`Failed to find SARIF results path for ${language}. Reason: ${(0, util_1.getErrorMessage)(e)}`);
} }
return []; return undefined;
} }
/** /**
* Try to bundle the database for the given language. Return a list containing * Try to bundle the database for the given language.
* the path to the database bundle.
* *
* If an error occurs, log it and return an empty list. * @return The path to the database bundle, or undefined if an error occurs.
*/ */
async function tryBundleDatabase(config, language, logger) { async function tryBundleDatabase(config, language, logger) {
try { try {
if ((0, analyze_1.dbIsFinalized)(config, language, logger)) { if ((0, analyze_1.dbIsFinalized)(config, language, logger)) {
try { try {
return [await createDatabaseBundleCli(config, language)]; return await createDatabaseBundleCli(config, language);
} }
catch (e) { catch (e) {
logger.warning(`Failed to bundle database for ${language} using the CLI. ` + logger.warning(`Failed to bundle database for ${language} using the CLI. ` +
`Falling back to a partial bundle. Reason: ${(0, util_1.getErrorMessage)(e)}`); `Falling back to a partial bundle. Reason: ${(0, util_1.getErrorMessage)(e)}`);
} }
} }
return [await createPartialDatabaseBundle(config, language)]; return await createPartialDatabaseBundle(config, language);
} }
catch (e) { catch (e) {
logger.warning(`Failed to bundle database for ${language}. Reason: ${(0, util_1.getErrorMessage)(e)}`); logger.warning(`Failed to bundle database for ${language}. Reason: ${(0, util_1.getErrorMessage)(e)}`);
return []; return undefined;
} }
} }
/** /**
@ -126,25 +129,46 @@ async function tryBundleDatabase(config, language, logger) {
* Logs and suppresses any errors that occur. * Logs and suppresses any errors that occur.
*/ */
async function tryUploadAllAvailableDebugArtifacts(config, logger) { async function tryUploadAllAvailableDebugArtifacts(config, logger) {
const filesToUpload = [];
try { try {
const filesToUpload = [];
for (const language of config.languages) { for (const language of config.languages) {
filesToUpload.push(...tryGetSarifResultPath(config, language, logger)); await (0, logging_1.withGroup)(`Uploading debug artifacts for ${language}`, async () => {
// Add any log files logger.info("Preparing SARIF result debug artifact...");
const databaseDirectory = (0, util_1.getCodeQLDatabasePath)(config, language); const sarifResultDebugArtifact = tryPrepareSarifDebugArtifact(config, language, logger);
const logsDirectory = path.resolve(databaseDirectory, "log"); if (sarifResultDebugArtifact) {
if ((0, util_1.doesDirectoryExist)(logsDirectory)) { filesToUpload.push(sarifResultDebugArtifact);
filesToUpload.push(...(0, util_1.listFolder)(logsDirectory)); logger.info("SARIF result debug artifact ready for upload.");
} }
// Multilanguage tracing: there are additional logs in the root of the cluster logger.info("Preparing database logs debug artifact...");
const multiLanguageTracingLogsDirectory = path.resolve(config.dbLocation, "log"); const databaseDirectory = (0, util_1.getCodeQLDatabasePath)(config, language);
if ((0, util_1.doesDirectoryExist)(multiLanguageTracingLogsDirectory)) { const logsDirectory = path.resolve(databaseDirectory, "log");
filesToUpload.push(...(0, util_1.listFolder)(multiLanguageTracingLogsDirectory)); if ((0, util_1.doesDirectoryExist)(logsDirectory)) {
} filesToUpload.push(...(0, util_1.listFolder)(logsDirectory));
// Add database bundle logger.info("Database logs debug artifact ready for upload.");
filesToUpload.push(...(await tryBundleDatabase(config, language, logger))); }
// Multilanguage tracing: there are additional logs in the root of the cluster
logger.info("Preparing database cluster logs debug artifact...");
const multiLanguageTracingLogsDirectory = path.resolve(config.dbLocation, "log");
if ((0, util_1.doesDirectoryExist)(multiLanguageTracingLogsDirectory)) {
filesToUpload.push(...(0, util_1.listFolder)(multiLanguageTracingLogsDirectory));
logger.info("Database cluster logs debug artifact ready for upload.");
}
// Add database bundle
logger.info("Preparing database bundle debug artifact...");
const databaseBundle = await tryBundleDatabase(config, language, logger);
if (databaseBundle) {
filesToUpload.push(databaseBundle);
logger.info("Database bundle debug artifact ready for upload.");
}
});
} }
await uploadDebugArtifacts(filesToUpload, config.dbLocation, config.debugArtifactName); }
catch (e) {
logger.warning(`Failed to prepare debug artifacts. Reason: ${(0, util_1.getErrorMessage)(e)}`);
return;
}
try {
await (0, logging_1.withGroup)("Uploading debug artifacts", async () => uploadDebugArtifacts(filesToUpload, config.dbLocation, config.debugArtifactName));
} }
catch (e) { catch (e) {
logger.warning(`Failed to upload debug artifacts. Reason: ${(0, util_1.getErrorMessage)(e)}`); logger.warning(`Failed to upload debug artifacts. Reason: ${(0, util_1.getErrorMessage)(e)}`);
@ -165,17 +189,11 @@ async function uploadDebugArtifacts(toUpload, rootDir, artifactName) {
core.info("Could not parse user-specified `matrix` input into JSON. The debug artifact will not be named with the user's `matrix` input."); core.info("Could not parse user-specified `matrix` input into JSON. The debug artifact will not be named with the user's `matrix` input.");
} }
} }
try { await artifact.create().uploadArtifact(sanitizeArtifactName(`${artifactName}${suffix}`), toUpload.map((file) => path.normalize(file)), path.normalize(rootDir), {
await artifact.create().uploadArtifact(sanitizeArtifactName(`${artifactName}${suffix}`), toUpload.map((file) => path.normalize(file)), path.normalize(rootDir), { continueOnError: true,
continueOnError: true, // ensure we don't keep the debug artifacts around for too long since they can be large.
// ensure we don't keep the debug artifacts around for too long since they can be large. retentionDays: 7,
retentionDays: 7, });
});
}
catch (e) {
// A failure to upload debug artifacts should not fail the entire action.
core.warning(`Failed to upload debug artifacts. Reason: ${(0, util_1.getErrorMessage)(e)}`);
}
} }
/** /**
* If a database has not been finalized, we cannot run the `codeql database bundle` * If a database has not been finalized, we cannot run the `codeql database bundle`

File diff suppressed because one or more lines are too long

10
lib/logging.js generated
View file

@ -25,6 +25,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.getActionsLogger = getActionsLogger; exports.getActionsLogger = getActionsLogger;
exports.getRunnerLogger = getRunnerLogger; exports.getRunnerLogger = getRunnerLogger;
exports.withGroup = withGroup;
const core = __importStar(require("@actions/core")); const core = __importStar(require("@actions/core"));
function getActionsLogger() { function getActionsLogger() {
return core; return core;
@ -44,4 +45,13 @@ function getRunnerLogger(debugMode) {
endGroup: () => undefined, endGroup: () => undefined,
}; };
} }
function withGroup(groupName, f) {
core.startGroup(groupName);
try {
return f();
}
finally {
core.endGroup();
}
}
//# sourceMappingURL=logging.js.map //# sourceMappingURL=logging.js.map

View file

@ -1 +1 @@
{"version":3,"file":"logging.js","sourceRoot":"","sources":["../src/logging.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAcA,4CAEC;AAED,0CAcC;AAhCD,oDAAsC;AActC,SAAgB,gBAAgB;IAC9B,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAgB,eAAe,CAAC,SAAkB;IAChD,OAAO;QACL,sCAAsC;QACtC,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,SAAS;QAClD,sCAAsC;QACtC,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,sCAAsC;QACtC,OAAO,EAAE,OAAO,CAAC,IAAI;QACrB,sCAAsC;QACtC,KAAK,EAAE,OAAO,CAAC,KAAK;QACpB,OAAO,EAAE,GAAG,EAAE,CAAC,SAAS;QACxB,UAAU,EAAE,GAAG,EAAE,CAAC,SAAS;QAC3B,QAAQ,EAAE,GAAG,EAAE,CAAC,SAAS;KAC1B,CAAC;AACJ,CAAC"} {"version":3,"file":"logging.js","sourceRoot":"","sources":["../src/logging.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAcA,4CAEC;AAED,0CAcC;AAED,8BAOC;AAzCD,oDAAsC;AActC,SAAgB,gBAAgB;IAC9B,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAgB,eAAe,CAAC,SAAkB;IAChD,OAAO;QACL,sCAAsC;QACtC,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,SAAS;QAClD,sCAAsC;QACtC,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,sCAAsC;QACtC,OAAO,EAAE,OAAO,CAAC,IAAI;QACrB,sCAAsC;QACtC,KAAK,EAAE,OAAO,CAAC,KAAK;QACpB,OAAO,EAAE,GAAG,EAAE,CAAC,SAAS;QACxB,UAAU,EAAE,GAAG,EAAE,CAAC,SAAS;QAC3B,QAAQ,EAAE,GAAG,EAAE,CAAC,SAAS;KAC1B,CAAC;AACJ,CAAC;AAED,SAAgB,SAAS,CAAI,SAAiB,EAAE,CAAU;IACxD,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC;IAC3B,IAAI,CAAC;QACH,OAAO,CAAC,EAAE,CAAC;IACb,CAAC;YAAS,CAAC;QACT,IAAI,CAAC,QAAQ,EAAE,CAAC;IAClB,CAAC;AACH,CAAC"}

View file

@ -31,13 +31,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core")); const core = __importStar(require("@actions/core"));
const debugArtifacts = __importStar(require("./debug-artifacts")); const debugArtifacts = __importStar(require("./debug-artifacts"));
const environment_1 = require("./environment"); const environment_1 = require("./environment");
const logging_1 = require("./logging");
const util_1 = require("./util"); const util_1 = require("./util");
async function runWrapper() { async function runWrapper() {
try { try {
const logger = (0, logging_1.getActionsLogger)();
// Upload SARIF artifacts if we determine that this is a third-party analysis run. // Upload SARIF artifacts if we determine that this is a third-party analysis run.
// For first-party runs, this artifact will be uploaded in the `analyze-post` step. // For first-party runs, this artifact will be uploaded in the `analyze-post` step.
if (process.env[environment_1.EnvVar.INIT_ACTION_HAS_RUN] !== "true") { if (process.env[environment_1.EnvVar.INIT_ACTION_HAS_RUN] !== "true") {
await debugArtifacts.uploadCombinedSarifArtifacts(); await (0, logging_1.withGroup)("Uploading combined SARIF debug artifact", () => debugArtifacts.uploadCombinedSarifArtifacts(logger));
} }
} }
catch (error) { catch (error) {

View file

@ -1 +1 @@
{"version":3,"file":"upload-sarif-action-post.js","sourceRoot":"","sources":["../src/upload-sarif-action-post.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;GAIG;AACH,oDAAsC;AAEtC,kEAAoD;AACpD,+CAAuC;AACvC,iCAAyC;AAEzC,KAAK,UAAU,UAAU;IACvB,IAAI,CAAC;QACH,kFAAkF;QAClF,mFAAmF;QACnF,IAAI,OAAO,CAAC,GAAG,CAAC,oBAAM,CAAC,mBAAmB,CAAC,KAAK,MAAM,EAAE,CAAC;YACvD,MAAM,cAAc,CAAC,4BAA4B,EAAE,CAAC;QACtD,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,CAAC,SAAS,CACZ,yCAAyC,IAAA,sBAAe,EAAC,KAAK,CAAC,EAAE,CAClE,CAAC;IACJ,CAAC;AACH,CAAC;AAED,KAAK,UAAU,EAAE,CAAC"} {"version":3,"file":"upload-sarif-action-post.js","sourceRoot":"","sources":["../src/upload-sarif-action-post.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;GAIG;AACH,oDAAsC;AAEtC,kEAAoD;AACpD,+CAAuC;AACvC,uCAAwD;AACxD,iCAAyC;AAEzC,KAAK,UAAU,UAAU;IACvB,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAA,0BAAgB,GAAE,CAAC;QAClC,kFAAkF;QAClF,mFAAmF;QACnF,IAAI,OAAO,CAAC,GAAG,CAAC,oBAAM,CAAC,mBAAmB,CAAC,KAAK,MAAM,EAAE,CAAC;YACvD,MAAM,IAAA,mBAAS,EAAC,yCAAyC,EAAE,GAAG,EAAE,CAC9D,cAAc,CAAC,4BAA4B,CAAC,MAAM,CAAC,CACpD,CAAC;QACJ,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,CAAC,SAAS,CACZ,yCAAyC,IAAA,sBAAe,EAAC,KAAK,CAAC,EAAE,CAClE,CAAC;IACJ,CAAC;AACH,CAAC;AAED,KAAK,UAAU,EAAE,CAAC"}

View file

@ -7,14 +7,19 @@ import * as core from "@actions/core";
import * as debugArtifacts from "./debug-artifacts"; import * as debugArtifacts from "./debug-artifacts";
import { EnvVar } from "./environment"; import { EnvVar } from "./environment";
import { getActionsLogger, withGroup } from "./logging";
import { getErrorMessage } from "./util"; import { getErrorMessage } from "./util";
async function runWrapper() { async function runWrapper() {
try { try {
const logger = getActionsLogger();
// Upload SARIF artifacts if we determine that this is a first-party analysis run. // Upload SARIF artifacts if we determine that this is a first-party analysis run.
// For third-party runs, this artifact will be uploaded in the `upload-sarif-post` step. // For third-party runs, this artifact will be uploaded in the `upload-sarif-post` step.
if (process.env[EnvVar.INIT_ACTION_HAS_RUN] === "true") { if (process.env[EnvVar.INIT_ACTION_HAS_RUN] === "true") {
await debugArtifacts.uploadCombinedSarifArtifacts(); await withGroup("Uploading combined SARIF debug artifact", () =>
debugArtifacts.uploadCombinedSarifArtifacts(logger),
);
} }
} catch (error) { } catch (error) {
core.setFailed( core.setFailed(

View file

@ -12,7 +12,7 @@ import { getCodeQL } from "./codeql";
import { Config } from "./config-utils"; import { Config } from "./config-utils";
import { EnvVar } from "./environment"; import { EnvVar } from "./environment";
import { Language } from "./languages"; import { Language } from "./languages";
import { Logger } from "./logging"; import { Logger, withGroup } from "./logging";
import { import {
bundleDb, bundleDb,
doesDirectoryExist, doesDirectoryExist,
@ -29,12 +29,12 @@ export function sanitizeArtifactName(name: string): string {
* Upload Actions SARIF artifacts for debugging when CODEQL_ACTION_DEBUG_COMBINED_SARIF * Upload Actions SARIF artifacts for debugging when CODEQL_ACTION_DEBUG_COMBINED_SARIF
* environment variable is set * environment variable is set
*/ */
export async function uploadCombinedSarifArtifacts() { export async function uploadCombinedSarifArtifacts(logger: Logger) {
const tempDir = getTemporaryDirectory(); const tempDir = getTemporaryDirectory();
// Upload Actions SARIF artifacts for debugging when environment variable is set // Upload Actions SARIF artifacts for debugging when environment variable is set
if (process.env["CODEQL_ACTION_DEBUG_COMBINED_SARIF"] === "true") { if (process.env["CODEQL_ACTION_DEBUG_COMBINED_SARIF"] === "true") {
core.info( logger.info(
"Uploading available combined SARIF files as Actions debugging artifact...", "Uploading available combined SARIF files as Actions debugging artifact...",
); );
@ -56,26 +56,32 @@ export async function uploadCombinedSarifArtifacts() {
} }
} }
if (toUpload.length > 0) { try {
await uploadDebugArtifacts( await uploadDebugArtifacts(
toUpload, toUpload,
baseTempDir, baseTempDir,
"combined-sarif-artifacts", "combined-sarif-artifacts",
); );
} catch (e) {
logger.warning(
`Failed to upload combined SARIF files as Actions debugging artifact. Reason: ${getErrorMessage(
e,
)}`,
);
} }
} }
} }
/** /**
* Try to get the SARIF result path for the given language. * Try to prepare a SARIF result debug artifact for the given language.
* *
* If an error occurs, log it and return an empty list. * @return The path to that debug artifact, or undefined if an error occurs.
*/ */
function tryGetSarifResultPath( function tryPrepareSarifDebugArtifact(
config: Config, config: Config,
language: Language, language: Language,
logger: Logger, logger: Logger,
): string[] { ): string | undefined {
try { try {
const analyzeActionOutputDir = process.env[EnvVar.SARIF_RESULTS_OUTPUT_DIR]; const analyzeActionOutputDir = process.env[EnvVar.SARIF_RESULTS_OUTPUT_DIR];
if ( if (
@ -94,7 +100,7 @@ function tryGetSarifResultPath(
`${language}.sarif`, `${language}.sarif`,
); );
fs.copyFileSync(sarifFile, sarifInDbLocation); fs.copyFileSync(sarifFile, sarifInDbLocation);
return [sarifInDbLocation]; return sarifInDbLocation;
} }
} }
} catch (e) { } catch (e) {
@ -104,24 +110,23 @@ function tryGetSarifResultPath(
)}`, )}`,
); );
} }
return []; return undefined;
} }
/** /**
* Try to bundle the database for the given language. Return a list containing * Try to bundle the database for the given language.
* the path to the database bundle.
* *
* If an error occurs, log it and return an empty list. * @return The path to the database bundle, or undefined if an error occurs.
*/ */
async function tryBundleDatabase( async function tryBundleDatabase(
config: Config, config: Config,
language: Language, language: Language,
logger: Logger, logger: Logger,
): Promise<string[]> { ): Promise<string | undefined> {
try { try {
if (dbIsFinalized(config, language, logger)) { if (dbIsFinalized(config, language, logger)) {
try { try {
return [await createDatabaseBundleCli(config, language)]; return await createDatabaseBundleCli(config, language);
} catch (e) { } catch (e) {
logger.warning( logger.warning(
`Failed to bundle database for ${language} using the CLI. ` + `Failed to bundle database for ${language} using the CLI. ` +
@ -129,14 +134,14 @@ async function tryBundleDatabase(
); );
} }
} }
return [await createPartialDatabaseBundle(config, language)]; return await createPartialDatabaseBundle(config, language);
} catch (e) { } catch (e) {
logger.warning( logger.warning(
`Failed to bundle database for ${language}. Reason: ${getErrorMessage( `Failed to bundle database for ${language}. Reason: ${getErrorMessage(
e, e,
)}`, )}`,
); );
return []; return undefined;
} }
} }
@ -149,38 +154,67 @@ export async function tryUploadAllAvailableDebugArtifacts(
config: Config, config: Config,
logger: Logger, logger: Logger,
) { ) {
const filesToUpload: string[] = [];
try { try {
const filesToUpload: string[] = [];
for (const language of config.languages) { for (const language of config.languages) {
filesToUpload.push(...tryGetSarifResultPath(config, language, logger)); await withGroup(`Uploading debug artifacts for ${language}`, async () => {
logger.info("Preparing SARIF result debug artifact...");
const sarifResultDebugArtifact = tryPrepareSarifDebugArtifact(
config,
language,
logger,
);
if (sarifResultDebugArtifact) {
filesToUpload.push(sarifResultDebugArtifact);
logger.info("SARIF result debug artifact ready for upload.");
}
// Add any log files logger.info("Preparing database logs debug artifact...");
const databaseDirectory = getCodeQLDatabasePath(config, language); const databaseDirectory = getCodeQLDatabasePath(config, language);
const logsDirectory = path.resolve(databaseDirectory, "log"); const logsDirectory = path.resolve(databaseDirectory, "log");
if (doesDirectoryExist(logsDirectory)) { if (doesDirectoryExist(logsDirectory)) {
filesToUpload.push(...listFolder(logsDirectory)); filesToUpload.push(...listFolder(logsDirectory));
} logger.info("Database logs debug artifact ready for upload.");
}
// Multilanguage tracing: there are additional logs in the root of the cluster // Multilanguage tracing: there are additional logs in the root of the cluster
const multiLanguageTracingLogsDirectory = path.resolve( logger.info("Preparing database cluster logs debug artifact...");
config.dbLocation, const multiLanguageTracingLogsDirectory = path.resolve(
"log", config.dbLocation,
); "log",
if (doesDirectoryExist(multiLanguageTracingLogsDirectory)) { );
filesToUpload.push(...listFolder(multiLanguageTracingLogsDirectory)); if (doesDirectoryExist(multiLanguageTracingLogsDirectory)) {
} filesToUpload.push(...listFolder(multiLanguageTracingLogsDirectory));
logger.info("Database cluster logs debug artifact ready for upload.");
}
// Add database bundle // Add database bundle
filesToUpload.push( logger.info("Preparing database bundle debug artifact...");
...(await tryBundleDatabase(config, language, logger)), const databaseBundle = await tryBundleDatabase(
); config,
language,
logger,
);
if (databaseBundle) {
filesToUpload.push(databaseBundle);
logger.info("Database bundle debug artifact ready for upload.");
}
});
} }
} catch (e) {
logger.warning(
`Failed to prepare debug artifacts. Reason: ${getErrorMessage(e)}`,
);
return;
}
await uploadDebugArtifacts( try {
filesToUpload, await withGroup("Uploading debug artifacts", async () =>
config.dbLocation, uploadDebugArtifacts(
config.debugArtifactName, filesToUpload,
config.dbLocation,
config.debugArtifactName,
),
); );
} catch (e) { } catch (e) {
logger.warning( logger.warning(
@ -212,23 +246,16 @@ export async function uploadDebugArtifacts(
} }
} }
try { await artifact.create().uploadArtifact(
await artifact.create().uploadArtifact( sanitizeArtifactName(`${artifactName}${suffix}`),
sanitizeArtifactName(`${artifactName}${suffix}`), toUpload.map((file) => path.normalize(file)),
toUpload.map((file) => path.normalize(file)), path.normalize(rootDir),
path.normalize(rootDir), {
{ continueOnError: true,
continueOnError: true, // ensure we don't keep the debug artifacts around for too long since they can be large.
// ensure we don't keep the debug artifacts around for too long since they can be large. retentionDays: 7,
retentionDays: 7, },
}, );
);
} catch (e) {
// A failure to upload debug artifacts should not fail the entire action.
core.warning(
`Failed to upload debug artifacts. Reason: ${getErrorMessage(e)}`,
);
}
} }
/** /**

View file

@ -31,3 +31,12 @@ export function getRunnerLogger(debugMode: boolean): Logger {
endGroup: () => undefined, endGroup: () => undefined,
}; };
} }
export function withGroup<T>(groupName: string, f: () => T): T {
core.startGroup(groupName);
try {
return f();
} finally {
core.endGroup();
}
}

View file

@ -7,14 +7,18 @@ import * as core from "@actions/core";
import * as debugArtifacts from "./debug-artifacts"; import * as debugArtifacts from "./debug-artifacts";
import { EnvVar } from "./environment"; import { EnvVar } from "./environment";
import { getActionsLogger, withGroup } from "./logging";
import { getErrorMessage } from "./util"; import { getErrorMessage } from "./util";
async function runWrapper() { async function runWrapper() {
try { try {
const logger = getActionsLogger();
// Upload SARIF artifacts if we determine that this is a third-party analysis run. // Upload SARIF artifacts if we determine that this is a third-party analysis run.
// For first-party runs, this artifact will be uploaded in the `analyze-post` step. // For first-party runs, this artifact will be uploaded in the `analyze-post` step.
if (process.env[EnvVar.INIT_ACTION_HAS_RUN] !== "true") { if (process.env[EnvVar.INIT_ACTION_HAS_RUN] !== "true") {
await debugArtifacts.uploadCombinedSarifArtifacts(); await withGroup("Uploading combined SARIF debug artifact", () =>
debugArtifacts.uploadCombinedSarifArtifacts(logger),
);
} }
} catch (error) { } catch (error) {
core.setFailed( core.setFailed(