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

Optionally search for workflows #270

Merged
merged 3 commits into from
Feb 15, 2024
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
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")
dawidd6 marked this conversation as resolved.
Show resolved Hide resolved
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