-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
62 lines (43 loc) · 2.21 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import * as core from '@actions/core';
import WorkflowHandler from './lib/WorkflowHandler';
import Clock from './lib/Clock';
import InputParser from './lib/InputParser';
async function run() {
try {
const inputParser = new InputParser();
const secondsToWait = inputParser.getWait();
const tokenInput = inputParser.getToken();
const workflow = new WorkflowHandler({ token: tokenInput });
const clock = new Clock();
core.info(`⏸ Debouncing ${secondsToWait} seconds before execution.`);
if (!process.env.ACT) {
const workflowsToCancel = await workflow.listActiveWorkflowsToCancel();
const runIdsToCancel = workflowsToCancel.map(workflow => workflow.id);
const cancelledWorkflows = await workflow.cancelWorkflows(runIdsToCancel);
core.info(`🧹 Cancelled ${cancelledWorkflows.length} superseded workflow runs. Starting the clock ...`);
}
await clock.setTimeoutWithLogging(secondsToWait * 1000, (secondsToWait * 1000) / 10, (timeElapsed, timeRemaining) => {
const relativeTimeRemaining = Clock.dayjs().add(timeRemaining, 'milliseconds').fromNow();
core.info(`⏲ Executing ${relativeTimeRemaining}, unless another workflow runs ...`);
});
if (!process.env.ACT) {
// Technically, a more recent workflow would have cancelled this workflow,
// meaning this code wouldn't run, but we're never sure what could happen.
// So here's a failsafe.
const hasBeenSuperseded = await workflow.isCurrentWorkflowSuperseded();
if (hasBeenSuperseded) {
core.notice('⏹ Another workflow run has superseded this workflow run. Cancelling ...')
await workflow.cancelCurrentWorkflowRun();
// Stalling this job to give the new GitHub Action some time to cancel this workflow run.
await clock.setTimeout(secondsToWait);
// If for some reasons that wasn't enough, failing the job.
return core.setFailed('⏹ Another workflow run has superseded this workflow run.');
}
}
core.info(`▶️ ${secondsToWait} seconds has elapsed and no new runs of this workflow could be found. Resuming execution ...`)
} catch (error) {
console.log(error);
core.setFailed(error.message);
}
}
run();