Skip to content

Commit

Permalink
feat: optional search for workflows
Browse files Browse the repository at this point in the history
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 committed Feb 14, 2024
1 parent 4d83789 commit b2bcf76
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 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
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ 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
Expand Down
38 changes: 27 additions & 11 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

0 comments on commit b2bcf76

Please sign in to comment.