From 0a8e52635114313fdd3061f3911f8d83d5c48fb8 Mon Sep 17 00:00:00 2001 From: Tailong Date: Tue, 6 Jul 2021 14:59:07 +0800 Subject: [PATCH 1/7] download aritfact from latest run which upload an artifact --- main.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.js b/main.js index a35f0053..c946cccc 100644 --- a/main.js +++ b/main.js @@ -73,7 +73,9 @@ async function main() { if (runNumber) { return r.run_number == runNumber } - return true + if (r.artifacts_url) { + return true + } }) if (run) { From add00bd492744b9fa3796ea85211f6a2b81f6038 Mon Sep 17 00:00:00 2001 From: Tailong Date: Tue, 6 Jul 2021 16:42:45 +0800 Subject: [PATCH 2/7] test if last run does not upload artifact --- .github/workflows/upload.yml | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/.github/workflows/upload.yml b/.github/workflows/upload.yml index 619840f3..1ef8ff15 100644 --- a/.github/workflows/upload.yml +++ b/.github/workflows/upload.yml @@ -14,11 +14,7 @@ jobs: run: | mkdir artifact echo $GITHUB_SHA > artifact/sha - - name: Upload - uses: actions/upload-artifact@v2 - with: - name: artifact - path: artifact + upload-multiple: runs-on: ubuntu-latest steps: @@ -27,13 +23,3 @@ jobs: mkdir artifact1 artifact2 echo $GITHUB_SHA > artifact1/sha1 echo $GITHUB_SHA > artifact2/sha2 - - name: Upload first - uses: actions/upload-artifact@v2 - with: - name: artifact1 - path: artifact1 - - name: Upload second - uses: actions/upload-artifact@v2 - with: - name: artifact2 - path: artifact2 From 0a83266f870619016d62e7b50353842bb0c8dd26 Mon Sep 17 00:00:00 2001 From: Tailong Date: Tue, 6 Jul 2021 16:47:30 +0800 Subject: [PATCH 3/7] fix if condition error --- main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.js b/main.js index c946cccc..d173d65b 100644 --- a/main.js +++ b/main.js @@ -73,7 +73,7 @@ async function main() { if (runNumber) { return r.run_number == runNumber } - if (r.artifacts_url) { + if (r.artifacts.length > 0) { return true } }) From 1f3e94a07babed3141505e306aead43311727514 Mon Sep 17 00:00:00 2001 From: Tailong Date: Tue, 6 Jul 2021 16:56:09 +0800 Subject: [PATCH 4/7] fix if condition error --- main.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/main.js b/main.js index d173d65b..6c353398 100644 --- a/main.js +++ b/main.js @@ -73,7 +73,12 @@ async function main() { if (runNumber) { return r.run_number == runNumber } - if (r.artifacts.length > 0) { + let artifacts = client.actions.listWorkflowRunArtifacts({ + owner: owner, + repo: repo, + run_id: r.run_number, + }) + if (artifacts.length > 0) { return true } }) From 35751a65f0fa5e1fb576261867997aca20e2cafb Mon Sep 17 00:00:00 2001 From: Tailong Date: Tue, 6 Jul 2021 17:02:07 +0800 Subject: [PATCH 5/7] fix if condition error --- .github/workflows/upload.yml | 16 +++++++++++++++- main.js | 20 +++++++++++++------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/.github/workflows/upload.yml b/.github/workflows/upload.yml index 1ef8ff15..619840f3 100644 --- a/.github/workflows/upload.yml +++ b/.github/workflows/upload.yml @@ -14,7 +14,11 @@ jobs: run: | mkdir artifact echo $GITHUB_SHA > artifact/sha - + - name: Upload + uses: actions/upload-artifact@v2 + with: + name: artifact + path: artifact upload-multiple: runs-on: ubuntu-latest steps: @@ -23,3 +27,13 @@ jobs: mkdir artifact1 artifact2 echo $GITHUB_SHA > artifact1/sha1 echo $GITHUB_SHA > artifact2/sha2 + - name: Upload first + uses: actions/upload-artifact@v2 + with: + name: artifact1 + path: artifact1 + - name: Upload second + uses: actions/upload-artifact@v2 + with: + name: artifact2 + path: artifact2 diff --git a/main.js b/main.js index 6c353398..d31dbf45 100644 --- a/main.js +++ b/main.js @@ -5,6 +5,13 @@ const filesize = require('filesize') const pathname = require('path') const fs = require('fs') +async function findAsync(arr, asyncCallback) { + const promises = arr.map(asyncCallback); + const results = await Promise.all(promises); + const index = results.findIndex(result => result); + return arr[index]; +} + async function main() { try { const token = core.getInput("github_token", { required: true }) @@ -64,25 +71,24 @@ async function main() { branch: branch, event: event, status: workflowConclusion, + per_page: 10, } )) { - const run = runs.data.find(r => { + const run = await findAsync(runs.data,async r => { if (commit) { return r.head_sha == commit } if (runNumber) { return r.run_number == runNumber } - let artifacts = client.actions.listWorkflowRunArtifacts({ + let artifacts = await client.actions.listWorkflowRunArtifacts({ owner: owner, repo: repo, - run_id: r.run_number, + run_id: r.id, }) - if (artifacts.length > 0) { - return true - } + artifacts = artifacts.data.artifacts + return artifacts.length > 0 }) - if (run) { runID = run.id break From 9f957be14115262327145dd37682e80af9ee0913 Mon Sep 17 00:00:00 2001 From: Tailong Date: Wed, 11 Aug 2021 09:53:20 +0800 Subject: [PATCH 6/7] fetch upstream --- .github/workflows/download.yml | 2 +- README.md | 10 ++++----- action.yml | 4 ++-- main.js | 39 ++++++++++++++++------------------ 4 files changed, 26 insertions(+), 29 deletions(-) diff --git a/.github/workflows/download.yml b/.github/workflows/download.yml index 4f09b1b0..41f441ca 100644 --- a/.github/workflows/download.yml +++ b/.github/workflows/download.yml @@ -84,6 +84,6 @@ jobs: workflow: upload.yml name: artifact path: artifact - workflow_conclusion: success + workflow_conclusion: '' - name: Test run: cat artifact/sha | grep $GITHUB_SHA diff --git a/README.md b/README.md index d8f4c692..cc37bbf2 100644 --- a/README.md +++ b/README.md @@ -15,15 +15,15 @@ Let's suppose you have a workflow with a job in it that at the end uploads an ar uses: dawidd6/action-download-artifact@v2 with: # Optional, GitHub token, a Personal Access Token with `public_repo` scope if needed - github_token: ${{secrets.GITHUB_TOKEN}}, required if artifact is from a different repo + # Required, if artifact is from a different repo + github_token: ${{secrets.GITHUB_TOKEN}} # Required, workflow file name or ID workflow: workflow_name.yml # Optional, the status or conclusion of a completed workflow to search for - # Can be one of a workflow conclusion:: - # "failure", "success", "neutral", "cancelled", "skipped", "timed_out", "action_required" + # Can be one of a workflow conclusion: + # "failure", "success", "neutral", "cancelled", "skipped", "timed_out", "action_required" # Or a workflow status: - # "completed", "in_progress", "queued" - # Default: "completed,success" + # "completed", "in_progress", "queued" workflow_conclusion: success # Optional, will get head commit SHA pr: ${{github.event.pull_request.number}} diff --git a/action.yml b/action.yml index 0c27e9d6..ac2a779b 100644 --- a/action.yml +++ b/action.yml @@ -14,11 +14,11 @@ inputs: required: true workflow_conclusion: description: | - Wanted status or conclusion or both to search for in recent runs + Wanted status or conclusion to search for in recent runs https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#list-workflow-runs required: false - default: completed,success + default: success repo: description: Repository name with owner (like actions/checkout) required: false diff --git a/main.js b/main.js index d31dbf45..1ccac410 100644 --- a/main.js +++ b/main.js @@ -70,35 +70,34 @@ async function main() { workflow_id: workflow, branch: branch, event: event, - status: workflowConclusion, - per_page: 10, } )) { - const run = await findAsync(runs.data,async r => { - if (commit) { - return r.head_sha == commit + for (const run of runs.data) { + if (commit && run.head_sha != commit) { + continue } - if (runNumber) { - return r.run_number == runNumber + if (runNumber && run.run_number != runNumber) { + continue + } + if (workflowConclusion && (workflowConclusion != run.conclusion && workflowConclusion != run.status)) { + continue } - let artifacts = await client.actions.listWorkflowRunArtifacts({ - owner: owner, - repo: repo, - run_id: r.id, - }) - artifacts = artifacts.data.artifacts - return artifacts.length > 0 - }) - if (run) { runID = run.id break } + if (runID) { + break + } } } - console.log("==> RunID:", runID) + if (runID) { + console.log("==> RunID:", runID) + } else { + throw new Error("no matching workflow run found") + } - let artifacts = await client.actions.listWorkflowRunArtifacts({ + let artifacts = await client.paginate(client.actions.listWorkflowRunArtifacts, { owner: owner, repo: repo, run_id: runID, @@ -106,11 +105,9 @@ async function main() { // One artifact or all if `name` input is not specified. if (name) { - artifacts = artifacts.data.artifacts.filter((artifact) => { + artifacts = artifacts.filter((artifact) => { return artifact.name == name }) - } else { - artifacts = artifacts.data.artifacts } if (artifacts.length == 0) From b2d469efb4cb38c4c3377492e68c04b8b7276d36 Mon Sep 17 00:00:00 2001 From: Tailong Date: Wed, 11 Aug 2021 10:11:28 +0800 Subject: [PATCH 7/7] add toggle on feature to get an artifacts from previous run --- README.md | 4 ++++ action.yml | 3 +++ main.js | 18 +++++++++++------- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index cc37bbf2..29605035 100644 --- a/README.md +++ b/README.md @@ -46,4 +46,8 @@ Let's suppose you have a workflow with a job in it that at the end uploads an ar path: extract_here # Optional, defaults to current repo repo: ${{github.repository}} + # Optional, check the workflow run whether it has an artifact + # then will get the last available artifact from previous workflow + # default false, just try to download from the last one + check_artifacts: false ``` diff --git a/action.yml b/action.yml index ac2a779b..7d8ef100 100644 --- a/action.yml +++ b/action.yml @@ -48,6 +48,9 @@ inputs: description: Where to unpack the artifact required: false default: "./" + check_artifacts: + description: Check workflow run whether it has an artifact + required: false runs: using: node12 main: main.js diff --git a/main.js b/main.js index 1ccac410..7e787a9e 100644 --- a/main.js +++ b/main.js @@ -5,13 +5,6 @@ const filesize = require('filesize') const pathname = require('path') const fs = require('fs') -async function findAsync(arr, asyncCallback) { - const promises = arr.map(asyncCallback); - const results = await Promise.all(promises); - const index = results.findIndex(result => result); - return arr[index]; -} - async function main() { try { const token = core.getInput("github_token", { required: true }) @@ -26,6 +19,7 @@ async function main() { let event = core.getInput("event") let runID = core.getInput("run_id") let runNumber = core.getInput("run_number") + let checkArtifacts = core.getInput("check_artifacts") const client = github.getOctokit(token) @@ -82,6 +76,16 @@ async function main() { if (workflowConclusion && (workflowConclusion != run.conclusion && workflowConclusion != run.status)) { continue } + if (checkArtifacts) { + let artifacts = await client.actions.listWorkflowRunArtifacts({ + owner: owner, + repo: repo, + run_id: r.id, + }) + if (artifacts.data.artifacts.length == 0) { + continue + } + } runID = run.id break }