Skip to content

Commit 3393554

Browse files
committed
ci: when looking for successful workflow jobs, use a smart order
There might be runs for the very same commit, those should be looked at first. Then there might be runs that have not yet finished; Let's first look at the finished ones. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent effc5cb commit 3393554

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

.github/workflows/build.yaml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,33 @@ jobs:
9595
per_page: 500,
9696
workflow_id,
9797
});
98-
for (const run of runs.workflow_runs) {
98+
// first look at commit-same runs, then at finished ones, then at in-progress ones
99+
const rank = (a) => (a.status === 'in_progress' ? 0 : (head_sha === a.head_sha ? 2 : 1))
100+
const demoteInProgressToEnd = (a, b) => (rank(b) - rank(a))
101+
for (const run of runs.workflow_runs.sort(demoteInProgressToEnd)) {
99102
let reason
100103
if (head_sha === run.head_sha) reason = 'the same commit'
101104
else if (tree_id === run.head_commit?.tree_id) reason = 'a tree-same commit'
102105
else continue
103106
107+
if (run.status === 'in_progress') {
108+
// poll until the run is done
109+
const pollIntervalInSeconds = 30
110+
let seconds = 0
111+
for (;;) {
112+
console.log(`Found existing, in-progress run at ${run.html_url}; Waiting for it to finish (waited ${seconds} seconds so far)...`)
113+
await new Promise((resolve) => { setTimeout(resolve, pollIntervalInSeconds * 1000) })
114+
seconds += pollIntervalInSeconds
115+
116+
const { data: polledRun } = await github.rest.actions.getWorkflowRun({
117+
owner: context.repo.owner,
118+
repo: context.repo.repo,
119+
run_id: run.id
120+
})
121+
if (polledRun.status !== 'in_progress') break
122+
}
123+
}
124+
104125
const { data: jobs } = await github.rest.actions.listJobsForWorkflowRun({
105126
owner: context.repo.owner,
106127
repo: context.repo.repo,

0 commit comments

Comments
 (0)