Skip to content

Commit b3db08f

Browse files
committed
fix(deduplicate): only run once!
1 parent 502a4e2 commit b3db08f

4 files changed

Lines changed: 55 additions & 14 deletions

File tree

action/lib/deduplicate.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* eslint-disable camelcase */
2+
3+
import core from '@actions/core'
4+
import github from '@actions/github'
5+
6+
export default async function (octokit) {
7+
// extract sha
8+
const { sha, runId: run_id } = github.context
9+
10+
// get workflow id from run id
11+
const { data: { workflow_id } } = await octokit.request('GET /repos/{owner}/{repo}/actions/runs/{run_id}', {
12+
...github.context.repo,
13+
run_id
14+
})
15+
16+
// get current run of this workflow
17+
const { data: { workflow_runs } } = await octokit.request('GET /repos/{owner}/{repo}/actions/runs', {
18+
...github.context.repo
19+
})
20+
21+
// find any instances of the same workflow
22+
workflow_runs
23+
// filter to relevant runs
24+
.filter(run => run.workflow_id === workflow_id && run.head_sha === sha)
25+
// pick relevant properties
26+
.map(run => ({ id: run.id, name: run.name, created_at: run.created_at }))
27+
// sort
28+
.sort(run => (a, b) => new Date(b.created_at) - new Date(a.created_at))
29+
// remove last one
30+
.pop()
31+
32+
// sort
33+
for (const run of workflow_runs) {
34+
core.info(`${run.id}: ${run.name} => ${run.created_at}`)
35+
36+
await octokit.request('POST /repos/{owner}/{repo}/actions/runs/{run_id}/cancel', {
37+
...github.context.repo,
38+
run_id: run.id
39+
})
40+
}
41+
}

action/lib/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// packages
22
import core from '@actions/core'
33
import github from '@actions/github'
4+
import deduplicate from './deduplicate.js'
45

56
import runs from './runs.js'
67
import workflows from './workflows.js'
@@ -9,15 +10,17 @@ import workflows from './workflows.js'
910
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
1011

1112
export default async function ({ token, delay, timeout }) {
13+
let timer = 0
14+
1215
// init octokit
1316
const octokit = github.getOctokit(token)
1417

15-
let timer = 0
18+
await deduplicate(octokit)
1619

17-
const flows = await workflows(octokit)
20+
const dependencies = await workflows(octokit)
1821

1922
// check runs
20-
let result = await runs(octokit, flows)
23+
let result = await runs(octokit, dependencies)
2124

2225
while (result.find(run => run.conclusion !== 'success')) {
2326
timer += delay
@@ -38,7 +41,7 @@ export default async function ({ token, delay, timeout }) {
3841
await sleep(delay)
3942

4043
// get the data again
41-
result = await runs(octokit, flows)
44+
result = await runs(octokit, dependencies)
4245
}
4346

4447
for (const run of result) {

action/lib/runs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import github from '@actions/github'
22

3-
export default async function (octokit, workflows) {
3+
export default async function (octokit, dependencies) {
44
// extract sha
55
const { sha } = github.context
66

@@ -10,7 +10,7 @@ export default async function (octokit, workflows) {
1010

1111
return workflow_runs
1212
// filter to relevant runs
13-
.filter(run => workflows.includes(run.name) && run.head_sha === sha)
13+
.filter(run => dependencies.includes(run.name) && run.head_sha === sha)
1414
// pick properties
1515
.map(run => ({ id: run.id, name: run.name, conclusion: run.conclusion }))
1616
}

action/lib/workflows.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,21 @@ export default async function (octokit) {
77

88
// get workflow id from run id
99
const { data: { workflow_id } } = await octokit.request('GET /repos/{owner}/{repo}/actions/runs/{run_id}', { // eslint-disable-line camelcase
10-
owner: 'hashtagpaid',
11-
repo: 'template-api',
10+
...github.context.repo,
1211
run_id
1312
})
1413

1514
// get the file name from the workflow
1615
const { data: { path } } = await octokit.request('GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}', {
17-
owner: 'hashtagpaid',
18-
repo: 'template-api',
16+
...github.context.repo,
1917
workflow_id
2018
})
2119

2220
// get the workflow content
2321
const { data: { content } } = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
24-
owner: 'hashtagpaid',
25-
repo: 'template-api',
26-
ref,
27-
path
22+
...github.context.repo,
23+
path,
24+
ref
2825
})
2926

3027
const { on: { workflow_run: { workflows } } } = yaml.parse(Buffer.from(content, 'base64').toString())

0 commit comments

Comments
 (0)