diff --git a/.github/workflows/bat.yml b/.github/workflows/bat.yml index 8518446..b32f58a 100644 --- a/.github/workflows/bat.yml +++ b/.github/workflows/bat.yml @@ -183,4 +183,20 @@ jobs: run: | grep "::group::deploy" console.log grep "::error::error task failed" console.log + rm console.log + + - name: Run build with invalid task + continue-on-error: true + uses: ./ + with: + tasks: badTask + build-options: -continueOnFailure + startup-options: -logfile console.log + + - name: Verify summary exception for invalid task + run: | + set -e + grep "Build summary not created" console.log + ! grep "while reading the build summary file:" console.log + ! grep "trying to delete the build summary" console.log rm console.log \ No newline at end of file diff --git a/src/buildSummary.ts b/src/buildSummary.ts index 2a05215..2dec28a 100644 --- a/src/buildSummary.ts +++ b/src/buildSummary.ts @@ -1,7 +1,7 @@ // Copyright 2024 The MathWorks, Inc. import * as core from "@actions/core"; import { join } from 'path'; -import { readFileSync, unlinkSync} from 'fs'; +import { readFileSync, unlinkSync, accessSync} from 'fs'; export interface Task { name: string; @@ -43,20 +43,26 @@ export function processAndDisplayBuildSummary() { const filePath: string = join(runnerTemp, `buildSummary${runId}.json`); let taskSummaryTableRows; - try { - const data = JSON.parse(readFileSync(filePath, { encoding: 'utf8' })); - taskSummaryTableRows = getBuildSummaryTable(data); - } catch (e) { - console.error('An error occurred while reading the build summary file:', e); - return; - } finally { + if (checkFileExists(filePath)) { + try { - unlinkSync(filePath); + const data = JSON.parse(readFileSync(filePath, { encoding: 'utf8' })); + taskSummaryTableRows = getBuildSummaryTable(data); } catch (e) { - console.error(`An error occurred while trying to delete the build summary file ${filePath}:`, e); + console.error('An error occurred while reading the build summary file:', e); + return; + } finally { + try { + unlinkSync(filePath); + } catch (e) { + console.error(`An error occurred while trying to delete the build summary file ${filePath}:`, e); + } } + writeSummary(taskSummaryTableRows); + } else { + core.info(`Build summary file ${filePath} does not exist.`); } - writeSummary(taskSummaryTableRows); + } export function getTaskDetails(tasks: Task): string[] { @@ -80,3 +86,12 @@ export function getTaskSummaryRows(task: Task, taskSummaryTableRows: string[][]) taskSummaryTableRows.push(taskDetails); return taskSummaryTableRows; } + +function checkFileExists(filePath: string): boolean { + try { + accessSync(filePath); + return true; + } catch (err) { + return false; + } +}