From cca55d08908402be05eca4d6a759e3974b63bc72 Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Tue, 14 Sep 2021 05:33:50 -0600 Subject: [PATCH] Fail action execution on unhandled errors Without this, the error logs contain an "UnhandledPromiseRejectionError" but the action is reported as succeeding. --- .github/workflows/failure-cases.yml | 5 +++-- src/main.ts | 16 ++++++++-------- src/post.ts | 10 +++++++++- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/.github/workflows/failure-cases.yml b/.github/workflows/failure-cases.yml index fa91cd4..9565917 100644 --- a/.github/workflows/failure-cases.yml +++ b/.github/workflows/failure-cases.yml @@ -11,26 +11,27 @@ env: CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}- jobs: + wrapper-missing: + continue-on-error: true runs-on: ubuntu-latest steps: - name: Checkout sources uses: actions/checkout@v2 - name: Test wrapper missing uses: ./ - continue-on-error: true with: build-root-directory: __tests__/samples/no-wrapper arguments: help bad-configuration: + continue-on-error: true runs-on: ubuntu-latest steps: - name: Checkout sources uses: actions/checkout@v2 - name: Test bad config value uses: ./ - continue-on-error: true with: build-root-directory: __tests__/samples/no-wrapper arguments: help diff --git a/src/main.ts b/src/main.ts index 5896098..0445f10 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,12 +9,12 @@ import * as provision from './provision' // Invoked by GitHub Actions export async function run(): Promise { - const workspaceDirectory = process.env[`GITHUB_WORKSPACE`] || '' - const buildRootDirectory = resolveBuildRootDirectory(workspaceDirectory) - - await caches.restore(buildRootDirectory) - try { + const workspaceDirectory = process.env[`GITHUB_WORKSPACE`] || '' + const buildRootDirectory = resolveBuildRootDirectory(workspaceDirectory) + + await caches.restore(buildRootDirectory) + const args: string[] = parseCommandLineArguments() // TODO: instead of running with no-daemon, run `--stop` in post action. args.push('--no-daemon') @@ -39,10 +39,10 @@ export async function run(): Promise { core.setFailed(`Gradle process exited with status ${result.status}`) } } catch (error) { - if (!(error instanceof Error)) { - throw error + core.setFailed(String(error)) + if (error instanceof Error && error.stack) { + core.info(error.stack) } - core.setFailed(error.message) } } diff --git a/src/post.ts b/src/post.ts index 698ddf3..583be81 100644 --- a/src/post.ts +++ b/src/post.ts @@ -1,8 +1,16 @@ +import * as core from '@actions/core' import * as caches from './caches' // Invoked by GitHub Actions export async function run(): Promise { - await caches.save() + try { + await caches.save() + } catch (error) { + core.setFailed(String(error)) + if (error instanceof Error && error.stack) { + core.info(error.stack) + } + } } run()