From f704403a053ce2e8f2865800afe110d2b78b3323 Mon Sep 17 00:00:00 2001 From: Jan Prieser Date: Fri, 19 Aug 2022 16:14:51 +0200 Subject: [PATCH 1/6] feat: add additional input to define how to fail the action --- README.md | 5 +++++ main.js | 24 +++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c2bca591..88285c95 100644 --- a/README.md +++ b/README.md @@ -59,4 +59,9 @@ Let's suppose you have a workflow with a job in it that at the end uploads an ar # Optional, choose to skip unpacking the downloaded artifact(s) # default false skip_unpack: false + # Optional, choose how to exit the action if no artifact is found + # can be one of: + # "fail", "warn", "ignore" + # default ignore + if_no_files_found: false ``` diff --git a/main.js b/main.js index 015cefd9..df79edd4 100644 --- a/main.js +++ b/main.js @@ -16,6 +16,7 @@ async function main() { const path = core.getInput("path", { required: true }) const name = core.getInput("name") const skipUnpack = core.getInput("skip_unpack") + const ifNoFilesFound = core.getInput("if_no_files_found") let workflow = core.getInput("workflow") let workflowConclusion = core.getInput("workflow_conclusion") let pr = core.getInput("pr") @@ -140,7 +141,8 @@ async function main() { } if (!runID) { - throw new Error("no matching workflow run found with any artifacts?") + setExitMessage("no matching workflow run found with any artifacts?") + return; } let artifacts = await client.paginate(client.rest.actions.listWorkflowRunArtifacts, { @@ -171,7 +173,7 @@ async function main() { } else { core.setOutput("dry_run", true) core.info('==> (found) Artifacts') - for (const artifact of artifacts){ + for (const artifact of artifacts) { const size = filesize(artifact.size_in_bytes, { base: 10 }) core.info(`\t==> Artifact:`) core.info(`\t==> ID: ${artifact.id}`) @@ -183,7 +185,8 @@ async function main() { } if (artifacts.length == 0) { - throw new Error("no artifacts found") + setExitMessage("no artifacts found") + return; } for (const artifact of artifacts) { @@ -227,6 +230,21 @@ async function main() { core.setOutput("error_message", error.message) core.setFailed(error.message) } + + function setExitMessage(message) { + switch (ifNoFilesFound) { + case "fail": + core.setFailed(message) + break + case "warn": + core.warning(message) + break + case "ignore": + default: + core.info(message) + break + } + } } main() From fc70a0d10e66557b92cc100e6c820a3304821b8b Mon Sep 17 00:00:00 2001 From: Jan Prieser Date: Fri, 19 Aug 2022 16:30:29 +0200 Subject: [PATCH 2/6] add input to action.yml --- action.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/action.yml b/action.yml index 846ed262..6ea20f35 100644 --- a/action.yml +++ b/action.yml @@ -63,6 +63,10 @@ inputs: dry_run: description: Check the existence of artifact(s) without downloading. required: false + if_no_files_found: false + required: false + description: choose how to exit the action if no artifact is found + default: "ignore" outputs: error_message: description: The error message, if an error occurs From 3683e2bb0de7c657320e2b56e14928df8c42aec6 Mon Sep 17 00:00:00 2001 From: Jan Prieser Date: Sun, 21 Aug 2022 13:13:49 +0200 Subject: [PATCH 3/6] fix: syntax issues --- action.yml | 2 +- main.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/action.yml b/action.yml index 6ea20f35..57e84d0b 100644 --- a/action.yml +++ b/action.yml @@ -63,7 +63,7 @@ inputs: dry_run: description: Check the existence of artifact(s) without downloading. required: false - if_no_files_found: false + if_no_files_found: required: false description: choose how to exit the action if no artifact is found default: "ignore" diff --git a/main.js b/main.js index df79edd4..14eadbb1 100644 --- a/main.js +++ b/main.js @@ -40,7 +40,7 @@ async function main() { owner: owner, repo: repo, run_id: runID || github.context.runId, - }); + }) workflow = run.data.workflow_id } @@ -142,7 +142,7 @@ async function main() { if (!runID) { setExitMessage("no matching workflow run found with any artifacts?") - return; + return } let artifacts = await client.paginate(client.rest.actions.listWorkflowRunArtifacts, { @@ -186,7 +186,7 @@ async function main() { if (artifacts.length == 0) { setExitMessage("no artifacts found") - return; + return } for (const artifact of artifacts) { From 9018e81d414c381497b0b075ad16e59045f49577 Mon Sep 17 00:00:00 2001 From: Jan Prieser Date: Sun, 21 Aug 2022 13:18:20 +0200 Subject: [PATCH 4/6] feat: rename input parameter --- README.md | 2 +- action.yml | 2 +- main.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 88285c95..a9b1b3e8 100644 --- a/README.md +++ b/README.md @@ -63,5 +63,5 @@ Let's suppose you have a workflow with a job in it that at the end uploads an ar # can be one of: # "fail", "warn", "ignore" # default ignore - if_no_files_found: false + if_no_artifact_found: false ``` diff --git a/action.yml b/action.yml index 57e84d0b..52a3b15a 100644 --- a/action.yml +++ b/action.yml @@ -63,7 +63,7 @@ inputs: dry_run: description: Check the existence of artifact(s) without downloading. required: false - if_no_files_found: + if_no_artifact_found: required: false description: choose how to exit the action if no artifact is found default: "ignore" diff --git a/main.js b/main.js index 14eadbb1..47e96863 100644 --- a/main.js +++ b/main.js @@ -16,7 +16,7 @@ async function main() { const path = core.getInput("path", { required: true }) const name = core.getInput("name") const skipUnpack = core.getInput("skip_unpack") - const ifNoFilesFound = core.getInput("if_no_files_found") + const ifNoArtifactFound = core.getInput("if_no_artifact_found") let workflow = core.getInput("workflow") let workflowConclusion = core.getInput("workflow_conclusion") let pr = core.getInput("pr") From 387cb75069b3fd7838f76188a06dfa4187dd2851 Mon Sep 17 00:00:00 2001 From: Jan Prieser Date: Sun, 21 Aug 2022 13:33:54 +0200 Subject: [PATCH 5/6] feat: add output to indicate success --- README.md | 13 +++++++++++++ action.yml | 2 ++ main.js | 14 ++++++++++---- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a9b1b3e8..a726ec3c 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Let's suppose you have a workflow with a job in it that at the end uploads an ar ```yaml - name: Download artifact + id: download-artifact uses: dawidd6/action-download-artifact@v2 with: # Optional, GitHub token, a Personal Access Token with `public_repo` scope if needed @@ -65,3 +66,15 @@ Let's suppose you have a workflow with a job in it that at the end uploads an ar # default ignore if_no_artifact_found: false ``` + +## Output + +The `found_artifact` step output contains a boolean value indicating whether an artifact was found. +```yaml +steps: + - id: download-artifact + uses: dawidd6/action-download-artifact@v2 + + - name: 'use artifact' + if: steps.download-artifact.found_artifact + run: echo "Found artifact" diff --git a/action.yml b/action.yml index 52a3b15a..9204380b 100644 --- a/action.yml +++ b/action.yml @@ -72,6 +72,8 @@ outputs: description: The error message, if an error occurs dry_run: description: Boolean output which is true if the dry run was successful and false otherwise. + found_artifact: + description: Boolean output which is true if the artifact was found and false otherwise. runs: using: node16 main: main.js diff --git a/main.js b/main.js index 47e96863..2683fbb5 100644 --- a/main.js +++ b/main.js @@ -141,7 +141,7 @@ async function main() { } if (!runID) { - setExitMessage("no matching workflow run found with any artifacts?") + setExitMessage(ifNoArtifactFound, "no matching workflow run found with any artifacts?") return } @@ -169,9 +169,11 @@ async function main() { if (dryRun) { if (artifacts.length == 0) { core.setOutput("dry_run", false) + core.setOutput("found_artifact", false) return } else { core.setOutput("dry_run", true) + core.setOutput("found_artifact", true) core.info('==> (found) Artifacts') for (const artifact of artifacts) { const size = filesize(artifact.size_in_bytes, { base: 10 }) @@ -185,11 +187,13 @@ async function main() { } if (artifacts.length == 0) { - setExitMessage("no artifacts found") + setExitMessage(ifNoArtifactFound, "no artifacts found") return } + core.setOutput("found_artifact", true) for (const artifact of artifacts) { + core.info(`==> Artifact: ${artifact.id}`) const size = filesize(artifact.size_in_bytes, { base: 10 }) @@ -227,12 +231,14 @@ async function main() { core.endGroup() } } catch (error) { + core.setOutput("found_artifact", false) core.setOutput("error_message", error.message) core.setFailed(error.message) } - function setExitMessage(message) { - switch (ifNoFilesFound) { + function setExitMessage(ifNoArtifactFound, message) { + core.setOutput("found_artifact", false) + switch (ifNoArtifactFound) { case "fail": core.setFailed(message) break From 757ba7c6fe21e3274bc0c16718533ff456334876 Mon Sep 17 00:00:00 2001 From: Jan Prieser Date: Tue, 30 Aug 2022 11:05:34 +0200 Subject: [PATCH 6/6] chore: update default and readme --- README.md | 2 +- action.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a726ec3c..f409beae 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Let's suppose you have a workflow with a job in it that at the end uploads an ar # can be one of: # "fail", "warn", "ignore" # default ignore - if_no_artifact_found: false + if_no_artifact_found: fail ``` ## Output diff --git a/action.yml b/action.yml index 9204380b..5ef88cd2 100644 --- a/action.yml +++ b/action.yml @@ -66,7 +66,7 @@ inputs: if_no_artifact_found: required: false description: choose how to exit the action if no artifact is found - default: "ignore" + default: "fail" outputs: error_message: description: The error message, if an error occurs