Skip to content

Commit

Permalink
Add warn-on-failure
Browse files Browse the repository at this point in the history
Some users might not want their workflow jobs to die just because an
artifact isn't available...
  • Loading branch information
jsoref committed Jan 31, 2024
1 parent c3c8e41 commit bd6ef41
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 9 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,18 @@ 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
| 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

Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
21 changes: 18 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 }),
Expand All @@ -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();
}
Expand Down Expand Up @@ -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}`);
}
});
}


/***/ }),
Expand Down
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 23 additions & 5 deletions src/download-artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,31 @@ import {Inputs, Outputs} from './constants'

const PARALLEL_DOWNLOADS = 5

interface Flags {
warnOnFailure: boolean
}

export const chunk = <T>(arr: T[], n: number): T[][] =>
arr.reduce((acc, cur, i) => {
const index = Math.floor(i / n)
acc[index] = [...(acc[index] || []), cur]
return acc
}, [] as T[][])

async function run(): Promise<void> {
async function run(flags: Flags): Promise<void> {
const inputs = {
name: core.getInput(Inputs.Name, {required: false}),
path: core.getInput(Inputs.Path, {required: false}),
token: core.getInput(Inputs.GitHubToken, {required: false}),
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()
Expand Down Expand Up @@ -131,6 +139,16 @@ async function run(): Promise<void> {
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}`)
}
})
}

0 comments on commit bd6ef41

Please sign in to comment.