Skip to content
This repository was archived by the owner on May 15, 2021. It is now read-only.

Commit 56a5fc1

Browse files
feat: build for release
1 parent cea9df1 commit 56a5fc1

File tree

5 files changed

+51
-27
lines changed

5 files changed

+51
-27
lines changed

action.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ inputs:
99
description: Secret GitHub API token to use for making API requests.
1010
default: ${{ github.token }}
1111
required: true
12+
TARGET_RUN_ID:
13+
description: Target run id
14+
default: ${{ github.run_id }}
15+
required: true
1216
EXCLUDE_MERGED:
1317
description: Whether to exclude merge push.
1418
required: false
@@ -21,6 +25,13 @@ inputs:
2125
description: Whether to exclude tag push.
2226
required: false
2327
default: 'true'
28+
CONSIDER_RE_RUN:
29+
description: Whether to consider re-run.
30+
required: false
31+
default: 'false'
32+
INTERVAL_MS:
33+
description: Interval milliseconds
34+
required: false
2435

2536
branding:
2637
icon: 'x-circle'

build.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"owner":"technote-space","repo":"auto-cancel-redundant-workflow","sha":"FETCH_HEAD","ref":"refs/heads/master","tagName":"test/v1.6.7","branch":"gh-actions","tags":["test/v1.6.7","test/v1.6","test/v1"],"updated_at":"2021-01-05T14:22:51.820Z"}
1+
{"owner":"technote-space","repo":"auto-cancel-redundant-workflow","sha":"FETCH_HEAD","ref":"refs/heads/master","tagName":"test/v1.6.7","branch":"gh-actions","tags":["test/v1.6.7","test/v1.6","test/v1"],"updated_at":"2021-01-05T17:22:55.525Z"}

lib/process.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Object.defineProperty(exports, "__esModule", { value: true });
33
exports.execute = void 0;
44
const core_1 = require("@actions/core");
5+
const github_action_helper_1 = require("@technote-space/github-action-helper");
56
const misc_1 = require("./utils/misc");
67
const workflow_1 = require("./utils/workflow");
78
const execute = async (logger, octokit, context) => {
@@ -10,9 +11,9 @@ const execute = async (logger, octokit, context) => {
1011
core_1.setOutput('ids', '');
1112
return;
1213
}
13-
const runId = misc_1.getRunId();
14+
const runId = misc_1.getTargetRunId(context);
1415
logger.info('run id: %d', runId);
15-
const run = await workflow_1.getWorkflowRun(octokit, context);
16+
const run = await workflow_1.getWorkflowRun(runId, octokit, context);
1617
logger.startProcess('run:');
1718
console.log(misc_1.getFilteredRun(run));
1819
logger.endProcess();
@@ -22,22 +23,25 @@ const execute = async (logger, octokit, context) => {
2223
logger.startProcess('workflow runs:');
2324
console.log(runs.map(run => misc_1.getFilteredRun(run)));
2425
logger.endProcess();
26+
// cancel all workflows except latest one (consider re-run)
27+
const runsWithUpdatedAt = runs.map(run => ({ ...run, updatedAt: workflow_1.getWorkflowRunUpdatedAt(run) }));
28+
const latestRunNumber = Math.max(...runsWithUpdatedAt.map(run => workflow_1.getWorkflowRunNumber(run)));
29+
const latestUpdatedAt = Math.max(...runsWithUpdatedAt.map(run => run.updatedAt));
30+
const targetRuns = runsWithUpdatedAt.filter(run => workflow_1.getWorkflowRunNumber(run) < latestRunNumber && // not latest run
31+
(!misc_1.isConsiderReRun() || run.updatedAt < latestUpdatedAt));
2532
logger.log();
26-
const runsWithCreatedAtTime = runs.filter(run => run.id !== runId).map(run => ({ ...run, createdAt: Date.parse(workflow_1.getWorkflowRunCreatedAt(run)) }));
27-
const createdAt = Date.parse(workflow_1.getWorkflowRunCreatedAt(run));
28-
const runNumber = workflow_1.getWorkflowRunNumber(run);
29-
if (runsWithCreatedAtTime.find(run => run.createdAt > createdAt || (run.createdAt === createdAt && workflow_1.getWorkflowRunNumber(run) > runNumber))) {
30-
logger.info(logger.c('newer job exists', { color: 'yellow' }));
31-
core_1.setOutput('ids', '');
32-
return;
33-
}
3433
logger.startProcess('Cancelling...');
35-
await Promise.all(runsWithCreatedAtTime.map(run => {
34+
const interval = misc_1.getIntervalMs();
35+
await targetRuns.reduce(async (prev, run) => {
36+
await prev;
3637
logger.log('cancel: %d', run.id);
37-
return workflow_1.cancelWorkflowRun(run.id, octokit, context);
38-
}));
39-
logger.info('total: %d', runsWithCreatedAtTime.length);
40-
core_1.setOutput('ids', runsWithCreatedAtTime.map(run => run.id).join(','));
38+
await workflow_1.cancelWorkflowRun(run.id, octokit, context);
39+
if (interval) {
40+
await github_action_helper_1.Utils.sleep(interval);
41+
}
42+
}, Promise.resolve());
43+
logger.info('total: %d', targetRuns.length);
44+
core_1.setOutput('ids', targetRuns.map(run => run.id).join(','));
4145
logger.endProcess();
4246
};
4347
exports.execute = execute;

lib/utils/misc.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.getFilteredRun = exports.getTargetBranch = exports.getRunId = exports.isNotExcludeRun = exports.isExcludeContext = void 0;
3+
exports.getFilteredRun = exports.getTargetBranch = exports.getTargetRunId = exports.isNotExcludeRun = exports.isExcludeContext = exports.getIntervalMs = exports.isConsiderReRun = void 0;
44
const core_1 = require("@actions/core");
55
const github_action_helper_1 = require("@technote-space/github-action-helper");
66
const constant_1 = require("../constant");
77
const getMergeMessagePrefix = () => github_action_helper_1.Utils.getPrefixRegExp(core_1.getInput('MERGE_MESSAGE_PREFIX'));
88
const isExcludeMerged = () => github_action_helper_1.Utils.getBoolValue(core_1.getInput('EXCLUDE_MERGED'));
99
const isExcludeTagPush = () => github_action_helper_1.Utils.getBoolValue(core_1.getInput('EXCLUDE_TAG_PUSH'));
10+
const parseNumber = (value, defaultValue) => {
11+
if (value && /^\d+$/.test(value)) {
12+
return Number(value);
13+
}
14+
return defaultValue;
15+
};
16+
const isConsiderReRun = () => github_action_helper_1.Utils.getBoolValue(core_1.getInput('CONSIDER_RE_RUN'));
17+
exports.isConsiderReRun = isConsiderReRun;
18+
const getIntervalMs = () => parseNumber(core_1.getInput('INTERVAL_MS'), undefined);
19+
exports.getIntervalMs = getIntervalMs;
1020
const isExcludeContext = (context) => github_action_helper_1.ContextHelper.isPush(context) && ((isExcludeTagPush() && github_action_helper_1.Utils.isTagRef(context)) ||
1121
(isExcludeMerged() && getMergeMessagePrefix().test(context.payload.head_commit.message)));
1222
exports.isExcludeContext = isExcludeContext;
1323
const isNotExcludeRun = (run) => !isExcludeMerged() || !getMergeMessagePrefix().test(run.head_commit.message);
1424
exports.isNotExcludeRun = isNotExcludeRun;
15-
const getRunId = () => Number(process.env.GITHUB_RUN_ID);
16-
exports.getRunId = getRunId;
17-
const getTargetBranch = async (octokit, context) => {
25+
const getTargetRunId = (context) => parseNumber(core_1.getInput('TARGET_RUN_ID'), context.runId);
26+
exports.getTargetRunId = getTargetRunId;
27+
const getTargetBranch = async (context) => {
1828
if (context.payload.pull_request) {
1929
return context.payload.pull_request.head.ref;
2030
}

lib/utils/workflow.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.cancelWorkflowRun = exports.getWorkflowRuns = exports.getWorkflowRunNumber = exports.getWorkflowRunCreatedAt = exports.getWorkflowId = exports.getWorkflowRun = void 0;
3+
exports.cancelWorkflowRun = exports.getWorkflowRuns = exports.getWorkflowRunNumber = exports.getWorkflowRunUpdatedAt = exports.getWorkflowId = exports.getWorkflowRun = void 0;
44
const github_action_helper_1 = require("@technote-space/github-action-helper");
55
const misc_1 = require("./misc");
6-
const getWorkflowRun = async (octokit, context) => {
6+
const getWorkflowRun = async (runId, octokit, context) => {
77
return (await octokit.actions.getWorkflowRun({
88
owner: context.repo.owner,
99
repo: context.repo.repo,
10-
'run_id': Number(process.env.GITHUB_RUN_ID),
10+
'run_id': runId,
1111
})).data;
1212
};
1313
exports.getWorkflowRun = getWorkflowRun;
@@ -19,8 +19,8 @@ const getWorkflowId = (run) => {
1919
return Number(matches[0]);
2020
};
2121
exports.getWorkflowId = getWorkflowId;
22-
const getWorkflowRunCreatedAt = (run) => github_action_helper_1.Utils.ensureNotNull(run.created_at);
23-
exports.getWorkflowRunCreatedAt = getWorkflowRunCreatedAt;
22+
const getWorkflowRunUpdatedAt = (run) => Date.parse(github_action_helper_1.Utils.ensureNotNull(run.updated_at));
23+
exports.getWorkflowRunUpdatedAt = getWorkflowRunUpdatedAt;
2424
const getWorkflowRunNumber = (run) => run.run_number;
2525
exports.getWorkflowRunNumber = getWorkflowRunNumber;
2626
const getWorkflowRuns = async (workflowId, logger, octokit, context) => {
@@ -29,7 +29,7 @@ const getWorkflowRuns = async (workflowId, logger, octokit, context) => {
2929
'workflow_id': workflowId,
3030
status: 'in_progress',
3131
};
32-
const branch = await misc_1.getTargetBranch(octokit, context);
32+
const branch = await misc_1.getTargetBranch(context);
3333
logger.log('target event: %s', logger.c(context.eventName, { color: 'green' }));
3434
if (branch) {
3535
logger.log('target branch: %s', logger.c(branch, { color: 'green' }));
@@ -43,7 +43,6 @@ const getWorkflowRuns = async (workflowId, logger, octokit, context) => {
4343
options)).map(run => run).filter(run => run.event === context.eventName).filter(misc_1.isNotExcludeRun);
4444
};
4545
exports.getWorkflowRuns = getWorkflowRuns;
46-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4746
const cancelWorkflowRun = async (runId, octokit, context) => octokit.actions.cancelWorkflowRun({
4847
...context.repo,
4948
'run_id': runId,

0 commit comments

Comments
 (0)