Skip to content

Commit

Permalink
feat: add additional input to define how to fail the action (#187)
Browse files Browse the repository at this point in the history
* feat: add additional input to define how to fail the action

* add input to action.yml

* fix: syntax issues

* feat: rename input parameter

* feat: add output to indicate success

* chore: update default and readme
  • Loading branch information
jaypea committed Aug 30, 2022
1 parent 19a94cd commit 9960cfb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -59,4 +60,21 @@ 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_artifact_found: fail
```

## 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"
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,17 @@ inputs:
dry_run:
description: Check the existence of artifact(s) without downloading.
required: false
if_no_artifact_found:
required: false
description: choose how to exit the action if no artifact is found
default: "fail"
outputs:
error_message:
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
32 changes: 28 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ifNoArtifactFound = core.getInput("if_no_artifact_found")
let workflow = core.getInput("workflow")
let workflowConclusion = core.getInput("workflow_conclusion")
let pr = core.getInput("pr")
Expand All @@ -39,7 +40,7 @@ async function main() {
owner: owner,
repo: repo,
run_id: runID || github.context.runId,
});
})
workflow = run.data.workflow_id
}

Expand Down Expand Up @@ -140,7 +141,8 @@ async function main() {
}

if (!runID) {
throw new Error("no matching workflow run found with any artifacts?")
setExitMessage(ifNoArtifactFound, "no matching workflow run found with any artifacts?")
return
}

let artifacts = await client.paginate(client.rest.actions.listWorkflowRunArtifacts, {
Expand All @@ -167,11 +169,13 @@ 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){
for (const artifact of artifacts) {
const size = filesize(artifact.size_in_bytes, { base: 10 })
core.info(`\t==> Artifact:`)
core.info(`\t==> ID: ${artifact.id}`)
Expand All @@ -183,10 +187,13 @@ async function main() {
}

if (artifacts.length == 0) {
throw new Error("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 })
Expand Down Expand Up @@ -224,9 +231,26 @@ async function main() {
core.endGroup()
}
} catch (error) {
core.setOutput("found_artifact", false)
core.setOutput("error_message", error.message)
core.setFailed(error.message)
}

function setExitMessage(ifNoArtifactFound, message) {
core.setOutput("found_artifact", false)
switch (ifNoArtifactFound) {
case "fail":
core.setFailed(message)
break
case "warn":
core.warning(message)
break
case "ignore":
default:
core.info(message)
break
}
}
}

main()

0 comments on commit 9960cfb

Please sign in to comment.