Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add additional input to define how to fail the action #187

Merged
merged 6 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# default ignore
# default: "fail"

😄

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"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
default: "fail"
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.
dawidd6 marked this conversation as resolved.
Show resolved Hide resolved
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
Comment on lines +144 to +145
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
setExitMessage(ifNoArtifactFound, "no matching workflow run found with any artifacts?")
return
return exitWithMessage(ifNoArtifactFound, "no matching workflow run found with any artifacts?")

Could we do it like this?

}

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) {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

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()