Skip to content

Commit

Permalink
Merge pull request #30 from mortenlj/master
Browse files Browse the repository at this point in the history
Add support for specifying Run ID
  • Loading branch information
dawidd6 authored Oct 8, 2020
2 parents 378a916 + 20bb4c2 commit 86f3002
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 42 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/download.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ jobs:
branch: master
- name: Test
run: cat artifact/sha | grep $GITHUB_SHA
download-run_id:
runs-on: ubuntu-latest
needs: wait
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Download
uses: ./
with:
workflow: upload.yml
name: artifact
path: artifact
run_id: 294940453
- name: Test
run: cat artifact/sha | grep 378a91650c8f053aefe22c192a49f7ae56101f1c
download-multiple:
runs-on: ubuntu-latest
needs: wait
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ Let's suppose you have a workflow with a job in it that at the end uploads an ar

## Usage

> If `commit` or `pr` or `branch` is not specified then the artifact from the most recent completed workflow run will be downloaded.
> If `branch` is specified, it will ignore any value set for `commit` and `pr`.
> If `commit` or `pr` or `branch` or `run_id` is not specified then the artifact from the most recent completed workflow run will be downloaded.
**Do not specify `pr`, `commit`, `branch` or `run_id` together. Pick just one or none.**

```yaml
- name: Download artifact
Expand All @@ -23,6 +24,8 @@ Let's suppose you have a workflow with a job in it that at the end uploads an ar
commit: ${{github.event.pull_request.head.sha}}
# Optional, will use the branch
branch: master
# Optional, will use specified workflow run
run_id: 1122334455
# Optional, uploaded artifact name,
# will download all artifacts if not specified
# and extract them in respective subdirectories
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ inputs:
branch:
description: Branch name
required: false
run_id:
description: Workflow run id
required: false
name:
description: Artifact name (download all artifacts in not specified)
required: false
Expand Down
73 changes: 33 additions & 40 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,19 @@ async function main() {
const [owner, repo] = core.getInput("repo", { required: true }).split("/")
const path = core.getInput("path", { required: true })
const name = core.getInput("name")
let branch = core.getInput("branch")
let pr = core.getInput("pr")
let commit = core.getInput("commit")
let branch = core.getInput("branch")
let runID = core.getInput("run_id")

const client = github.getOctokit(token)

console.log("==> Repo:", owner + "/" + repo)

if (branch) {
console.log("==> Branch:", branch)

if (pr) {
console.log("==> Branch and PR can not be defined at the same time, ignoring PR:", pr)
pr = undefined
}

if (commit) {
console.log("==> Branch and Commit can not be defined at the same time, ignoring Commit:", commit)
commit = undefined
}
if ([runID, branch, pr, commit].filter(elem => elem).length > 1) {
throw new Error("don't specify `run_id`, `branch`, `pr`, and `commit` together")
}

console.log("==> Repo:", owner + "/" + repo)

if (pr) {
console.log("==> PR:", pr)

Expand All @@ -49,38 +40,40 @@ async function main() {
console.log("==> Commit:", commit)
}

let run
const endpoint = "GET /repos/:owner/:repo/actions/workflows/:id/runs"
const params = {
owner: owner,
repo: repo,
id: workflow,
branch: branch
}
for await (const runs of client.paginate.iterator(endpoint, params)) {
run = runs.data.find((run) => {
if (commit) {
return run.head_sha == commit
}
else {
// No PR or commit was specified just return the first one.
// The results appear to be sorted from API, so the most recent is first.
// Just check if workflow run completed.
return run.status == "completed"
if (!runID) {
const endpoint = "GET /repos/:owner/:repo/actions/workflows/:id/runs"
const params = {
owner: owner,
repo: repo,
id: workflow,
branch: branch
}
for await (const runs of client.paginate.iterator(endpoint, params)) {
const run = runs.data.find(r => {
if (commit) {
return r.head_sha == commit
}
else {
// No PR or commit was specified just return the first one.
// The results appear to be sorted from API, so the most recent is first.
// Just check if workflow run completed.
return r.status == "completed"
}
})

if (run) {
runID = run.id
break
}
})

if (run) {
break
}
}

console.log("==> Run:", run.id)
console.log("==> RunID:", runID)

let artifacts = await client.actions.listWorkflowRunArtifacts({
owner: owner,
repo: repo,
run_id: run.id,
run_id: runID,
})

// One artifact or all if `name` input is not specified.
Expand All @@ -93,7 +86,7 @@ async function main() {
}

if (artifacts.length == 0)
throw new Error("no artifacts found")
throw new Error("no artifacts found")

for (const artifact of artifacts) {
console.log("==> Artifact:", artifact.id)
Expand Down

0 comments on commit 86f3002

Please sign in to comment.