Skip to content

Commit

Permalink
Optionally search for workflows (#270)
Browse files Browse the repository at this point in the history
* style: remove trailing whitespace

* docs: update url

The API is now versioned. The link has changed.

* feat: optional search for workflows

When no workflow is set, until now the current workflow has been chosen. But
the workflow we get the artifact from could be another one determined by other
criteria like the branch and the artifact name.

We provide a new option workflow_search, that if set, allows us to search for
the workflow repo-wide according to the other provided data.
  • Loading branch information
romangg authored Feb 15, 2024
1 parent a07e1dd commit f6b0bac
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Let's suppose you have a workflow with a job in it that at the end uploads an ar
# Optional, workflow file name or ID
# If not specified, will be inferred from run_id (if run_id is specified), or will be the current workflow
workflow: workflow_name.yml
# If no workflow is set and workflow_search set to true, then the most recent workflow matching
# all other criteria will be looked up instead of using the current workflow
workflow_search: false
# 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"
Expand Down
11 changes: 9 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ inputs:
If not specified, will be inferred from run_id (if run_id is specified), or will be the current workflow
required: false
workflow_search:
description: |
Most recent workflow matching all other criteria will be looked up instead of using the current workflow
https://docs.github.com/de/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-repository
required: false
default: false
workflow_conclusion:
description: |
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
https://docs.github.com/de/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-workflow
required: false
default: success
repo:
Expand Down Expand Up @@ -78,7 +85,7 @@ inputs:
required: false
description: |
Choose how to exit the action if no artifact is found
fail, warn or ignore
default: fail
outputs:
Expand Down
42 changes: 29 additions & 13 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ async function downloadAction(name, path) {
core.setOutput("found_artifact", true)
}

async function getWorkflow(client, owner, repo, runID) {
const run = await client.rest.actions.getWorkflowRun({
owner: owner,
repo: repo,
run_id: runID || github.context.runId,
})
return run.data.workflow_id
}

async function main() {
try {
const token = core.getInput("github_token", { required: true })
Expand All @@ -29,6 +38,7 @@ async function main() {
const skipUnpack = core.getBooleanInput("skip_unpack")
const ifNoArtifactFound = core.getInput("if_no_artifact_found")
let workflow = core.getInput("workflow")
let workflowSearch = core.getInput("workflow_search")
let workflowConclusion = core.getInput("workflow_conclusion")
let pr = core.getInput("pr")
let commit = core.getInput("commit")
Expand All @@ -47,16 +57,13 @@ async function main() {
core.info(`==> Artifact name: ${name}`)
core.info(`==> Local path: ${path}`)

if (!workflow) {
const run = await client.rest.actions.getWorkflowRun({
owner: owner,
repo: repo,
run_id: runID || github.context.runId,
})
workflow = run.data.workflow_id
if (!workflow && !workflowSearch) {
workflow = await getWorkflow(client, owner, repo, runID)
}

core.info(`==> Workflow name: ${workflow}`)
if (workflow) {
core.info(`==> Workflow name: ${workflow}`)
}
core.info(`==> Workflow conclusion: ${workflowConclusion}`)

const uniqueInputSets = [
Expand Down Expand Up @@ -106,17 +113,20 @@ async function main() {
core.info(`==> Allow forks: ${allowForks}`)

if (!runID) {
const runGetter = workflow ? client.rest.actions.listWorkflowRuns : client.rest.actions.listWorkflowRunsForRepo
// Note that the runs are returned in most recent first order.
for await (const runs of client.paginate.iterator(client.rest.actions.listWorkflowRuns, {
for await (const runs of client.paginate.iterator(runGetter, {
owner: owner,
repo: repo,
workflow_id: workflow,
...(workflow ? { workflow_id: workflow } : {}),
...(branch ? { branch } : {}),
...(event ? { event } : {}),
...(commit ? { head_sha: commit } : {}),
}
)) {
for (const run of runs.data) {
if (commit && run.head_sha != commit) {
continue
}
if (runNumber && run.run_number != runNumber) {
continue
}
Expand Down Expand Up @@ -148,9 +158,15 @@ async function main() {
}
}
}

runID = run.id
core.info(`==> (found) Run ID: ${runID}`)
core.info(`==> (found) Run date: ${run.created_at}`)

if (!workflow) {
workflow = await getWorkflow(client, owner, repo, runID)
core.info(`==> (found) Workflow: ${workflow}`)
}
break
}
if (runID) {
Expand Down Expand Up @@ -222,7 +238,7 @@ async function main() {
}

core.setOutput("found_artifact", true)

for (const artifact of artifacts) {
core.info(`==> Artifact: ${artifact.id}`)

Expand Down Expand Up @@ -277,7 +293,7 @@ async function main() {

function setExitMessage(ifNoArtifactFound, message) {
core.setOutput("found_artifact", false)

switch (ifNoArtifactFound) {
case "fail":
core.setFailed(message)
Expand Down

0 comments on commit f6b0bac

Please sign in to comment.