From 215ab44d34341c9fd2d067248f4f8d0bff4217eb Mon Sep 17 00:00:00 2001 From: Christopher Roemheld Date: Tue, 20 Feb 2024 15:16:51 +0100 Subject: [PATCH] Add allow-not-found input in case artifact does not exist --- action.yml | 4 ++++ src/constants.ts | 3 ++- src/download-artifact.ts | 34 +++++++++++++++++++++------------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/action.yml b/action.yml index 54a3eb6..3760fca 100644 --- a/action.yml +++ b/action.yml @@ -17,6 +17,10 @@ inputs: If false, the downloaded artifacts will be extracted into individual named directories within the specified path.' required: false default: 'false' + allow-not-found: + description: 'If an artifact was not found, do not cause the action to fail.' + required: false + default: 'false' github-token: description: 'The GitHub token used to authenticate with the GitHub API. This is required when downloading artifacts from a different repository or from a different workflow run. diff --git a/src/constants.ts b/src/constants.ts index 17c7d34..06873e0 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', + AllowNotFound = 'allow-not-found' } export enum Outputs { diff --git a/src/download-artifact.ts b/src/download-artifact.ts index aedfe12..2b93c20 100644 --- a/src/download-artifact.ts +++ b/src/download-artifact.ts @@ -23,7 +23,10 @@ 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 + }), + allowNotFound: core.getBooleanInput(Inputs.AllowNotFound, {required: false}) } if (!inputs.path) { @@ -35,7 +38,7 @@ async function run(): Promise { } const isSingleArtifactDownload = !!inputs.name - const resolvedPath = path.resolve(inputs.path) + let resolvedPath = path.resolve(inputs.path) core.debug(`Resolved path is ${resolvedPath}`) const options: FindOptions = {} @@ -60,20 +63,25 @@ async function run(): Promise { if (isSingleArtifactDownload) { core.info(`Downloading single artifact`) - const {artifact: targetArtifact} = await artifactClient.getArtifact( - inputs.name, - options - ) + const targetArtifact = await artifactClient + .getArtifact(inputs.name, options) + .catch(() => null) if (!targetArtifact) { - throw new Error(`Artifact '${inputs.name}' not found`) + const message = `Artifact '${inputs.name}' not found` + if (!inputs.allowNotFound) { + throw new Error(message) + } else { + core.warning(message) + resolvedPath = '' + } + } else { + core.debug( + `Found named artifact '${inputs.name}' (ID: ${targetArtifact.artifact.id}, Size: ${targetArtifact.artifact.size})` + ) + + artifacts = [targetArtifact.artifact] } - - core.debug( - `Found named artifact '${inputs.name}' (ID: ${targetArtifact.id}, Size: ${targetArtifact.size})` - ) - - artifacts = [targetArtifact] } else { const listArtifactResponse = await artifactClient.listArtifacts({ latest: true,