From bd6ef4177afc69c2704e05ea94a6125bc5dfb0d7 Mon Sep 17 00:00:00 2001 From: Josh Soref <2119212+jsoref@users.noreply.github.com> Date: Tue, 30 Jan 2024 14:58:20 -0500 Subject: [PATCH] Add warn-on-failure Some users might not want their workflow jobs to die just because an artifact isn't available... --- .github/workflows/test.yml | 23 +++++++++++++++++++++++ README.md | 5 +++++ action.yml | 6 ++++++ dist/index.js | 21 ++++++++++++++++++--- src/constants.ts | 3 ++- src/download-artifact.ts | 28 +++++++++++++++++++++++----- 6 files changed, 77 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7913e0d..bd0f1af 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -118,6 +118,29 @@ jobs: path: single/directory merge-multiple: true + - name: Request missing Artifact + id: request-missing-artifact + uses: ./ + with: + name: nonexistent + warn-on-failure: true + + - name: Check missing warning + env: + output: ${{ steps.request-missing-artifact.outputs.failure }} + if: ${{ !contains(env.output, 'Unable to download artifact(s):') }} + run: | + false + shell: bash + + - name: Log missing warning + env: + output: ${{ steps.request-missing-artifact.outputs.failure }} + run_os: ${{ matrix.runs-on }} + run: | + echo "::notice title=This message is expected::[$run_os] $output" + shell: bash + - name: Verify successful download run: | $fileA = "single/directory/file-A.txt" diff --git a/README.md b/README.md index 6ed23d8..77455a1 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,10 @@ For assistance with breaking changes, see [MIGRATION.md](docs/MIGRATION.md). # If github-token is specified, this is the run that artifacts will be downloaded from. # Optional. Default is ${{ github.run_id }} run-id: + + # Map failure result to an output instead of failing the job. + # Optional. Default is `false` + warn-on-failure: ``` ### Outputs @@ -89,6 +93,7 @@ For assistance with breaking changes, see [MIGRATION.md](docs/MIGRATION.md). | Name | Description | Example | | - | - | - | | `download-path` | Absolute path where the artifact(s) were downloaded | `/tmp/my/download/path` | +| `failure` | Failure message (if using `warn-on-failure`) | `Unable to download artifact(s): ...` | ## Examples diff --git a/action.yml b/action.yml index 54a3eb6..7e91294 100644 --- a/action.yml +++ b/action.yml @@ -32,9 +32,15 @@ inputs: If github-token is specified, this is the run that artifacts will be downloaded from.' required: false default: ${{ github.run_id }} + warn-on-failure: + description: 'Map failure result to an output instead of failing the job' + required: false + default: false outputs: download-path: description: 'Path of artifact download' + failure: + description: 'Failure message (if warn-on-failure is set)' runs: using: 'node20' main: 'dist/index.js' diff --git a/dist/index.js b/dist/index.js index fc7187a..3db6fb2 100644 --- a/dist/index.js +++ b/dist/index.js @@ -121005,6 +121005,7 @@ var Inputs; Inputs["RunID"] = "run-id"; Inputs["Pattern"] = "pattern"; Inputs["MergeMultiple"] = "merge-multiple"; + Inputs["WarnOnFailure"] = "warn-on-failure"; })(Inputs || (exports.Inputs = Inputs = {})); var Outputs; (function (Outputs) { @@ -121069,7 +121070,7 @@ const chunk = (arr, n) => arr.reduce((acc, cur, i) => { return acc; }, []); exports.chunk = chunk; -function run() { +function run(flags) { return __awaiter(this, void 0, void 0, function* () { const inputs = { name: core.getInput(constants_1.Inputs.Name, { required: false }), @@ -121078,8 +121079,12 @@ function run() { repository: core.getInput(constants_1.Inputs.Repository, { required: false }), runID: parseInt(core.getInput(constants_1.Inputs.RunID, { required: false })), pattern: core.getInput(constants_1.Inputs.Pattern, { required: false }), - mergeMultiple: core.getBooleanInput(constants_1.Inputs.MergeMultiple, { required: false }) + mergeMultiple: core.getBooleanInput(constants_1.Inputs.MergeMultiple, { + required: false + }), + warnOnFailure: core.getBooleanInput(constants_1.Inputs.WarnOnFailure, { required: false }) }; + flags.warnOnFailure = inputs.warnOnFailure; if (!inputs.path) { inputs.path = process.env['GITHUB_WORKSPACE'] || process.cwd(); } @@ -121147,7 +121152,17 @@ function run() { core.info('Download artifact has finished successfully'); }); } -run().catch(err => core.setFailed(`Unable to download artifact(s): ${err.message}`)); +{ + const flags = { warnOnFailure: false }; + run(flags).catch(err => { + if (flags.warnOnFailure) { + core.setOutput('failure', `Unable to download artifact(s): ${err.message}`); + } + else { + core.setFailed(`Unable to download artifact(s): ${err.message}`); + } + }); +} /***/ }), diff --git a/src/constants.ts b/src/constants.ts index 17c7d34..57b14e1 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -5,7 +5,8 @@ export enum Inputs { Repository = 'repository', RunID = 'run-id', Pattern = 'pattern', - MergeMultiple = 'merge-multiple' + MergeMultiple = 'merge-multiple', + WarnOnFailure = 'warn-on-failure' } export enum Outputs { diff --git a/src/download-artifact.ts b/src/download-artifact.ts index aedfe12..890c356 100644 --- a/src/download-artifact.ts +++ b/src/download-artifact.ts @@ -8,6 +8,10 @@ import {Inputs, Outputs} from './constants' const PARALLEL_DOWNLOADS = 5 +interface Flags { + warnOnFailure: boolean +} + export const chunk = (arr: T[], n: number): T[][] => arr.reduce((acc, cur, i) => { const index = Math.floor(i / n) @@ -15,7 +19,7 @@ export const chunk = (arr: T[], n: number): T[][] => return acc }, [] as T[][]) -async function run(): Promise { +async function run(flags: Flags): Promise { const inputs = { name: core.getInput(Inputs.Name, {required: false}), path: core.getInput(Inputs.Path, {required: false}), @@ -23,8 +27,12 @@ async function run(): Promise { repository: core.getInput(Inputs.Repository, {required: false}), runID: parseInt(core.getInput(Inputs.RunID, {required: false})), pattern: core.getInput(Inputs.Pattern, {required: false}), - mergeMultiple: core.getBooleanInput(Inputs.MergeMultiple, {required: false}) + mergeMultiple: core.getBooleanInput(Inputs.MergeMultiple, { + required: false + }), + warnOnFailure: core.getBooleanInput(Inputs.WarnOnFailure, {required: false}) } + flags.warnOnFailure = inputs.warnOnFailure if (!inputs.path) { inputs.path = process.env['GITHUB_WORKSPACE'] || process.cwd() @@ -131,6 +139,16 @@ async function run(): Promise { core.info('Download artifact has finished successfully') } -run().catch(err => - core.setFailed(`Unable to download artifact(s): ${err.message}`) -) +{ + const flags = {warnOnFailure: false} + run(flags).catch(err => { + if (flags.warnOnFailure) { + core.setOutput( + 'failure', + `Unable to download artifact(s): ${err.message}` + ) + } else { + core.setFailed(`Unable to download artifact(s): ${err.message}`) + } + }) +}