Skip to content

Commit 6fd170c

Browse files
committed
Add stop-on-signals and auto-cancellation
When workflows are cancelled, any started builds are left running. This is not usually desired, for example if using workflow concurrency[^1] to cancel redundant builds. In cases like this, we'd want to also stop any triggered builds. This commit adds a `stop-on-signals` input with a default value of `SIGINT` and updates the action to listen for the listed signal(s) and stop any already-started build in response. This accomplishes the desired behavior in the common case. [^1]: https://docs.github.com/en/actions/using-jobs/using-concurrency#example-using-concurrency-to-cancel-any-in-progress-job-or-run
1 parent 8802399 commit 6fd170c

File tree

4 files changed

+2389
-35218
lines changed

4 files changed

+2389
-35218
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ The only required input is `project-name`.
120120
1. **disable-github-env-vars** (optional) :
121121
Set to `true` if you want do disable github environment variables in codebuild.
122122

123+
1. **stop-on-signals** (optional) :
124+
Comma-separated list of signals that will cause any started builds to be
125+
stopped. The default value is `SIGINT`, which is what GitHub sends processes
126+
when a workflow is cancelled. This means you can use concurrency settings or
127+
other GitHub features that cause workflow cancellations without leaving
128+
orphan builds running. Set to an empty string to disable.
129+
123130
### Outputs
124131

125132
1. **aws-build-id** : The CodeBuild build ID of the build that the action ran.

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ inputs:
4646
artifacts-type-override:
4747
description: 'The type of build output artifact'
4848
required: false
49+
stop-on-signals:
50+
description: 'Comma separated list of process signals on which to stop the build. Default is SIGINT.'
51+
required: false
52+
default: 'SIGINT'
4953
outputs:
5054
aws-build-id:
5155
description: 'The AWS CodeBuild Build ID for this build.'

code-build.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ function runBuild() {
2323

2424
const inputs = githubInputs();
2525

26-
const config = (({ updateInterval, updateBackOff, hideCloudWatchLogs }) => ({
26+
const config = (({
2727
updateInterval,
2828
updateBackOff,
2929
hideCloudWatchLogs,
30+
stopOnSignals,
31+
}) => ({
32+
updateInterval,
33+
updateBackOff,
34+
hideCloudWatchLogs,
35+
stopOnSignals,
3036
}))(inputs);
3137

3238
// Get input options for startBuild
@@ -39,10 +45,27 @@ async function build(sdk, params, config) {
3945
// Start the build
4046
const start = await sdk.codeBuild.startBuild(params);
4147

48+
// Set up signal handling to stop the build on cancellation
49+
setupSignalHandlers(sdk, start.build.id, config.stopOnSignals);
50+
4251
// Wait for the build to "complete"
4352
return waitForBuildEndTime(sdk, start.build, config);
4453
}
4554

55+
function setupSignalHandlers(sdk, id, signals) {
56+
signals.forEach((s) => {
57+
core.info(`Installing signal handler for ${s}`);
58+
process.on(s, async () => {
59+
try {
60+
core.info(`Caught ${s}, attempting to stop build...`);
61+
await sdk.codeBuild.stopBuild({ id }).promise();
62+
} catch (ex) {
63+
core.error(`Error stopping build: ${ex}`);
64+
}
65+
});
66+
});
67+
}
68+
4669
async function waitForBuildEndTime(
4770
sdk,
4871
{ id, logs },
@@ -226,6 +249,12 @@ function githubInputs() {
226249
const artifactsTypeOverride =
227250
core.getInput("artifacts-type-override", { required: false }) || undefined;
228251

252+
const stopOnSignals = core
253+
.getInput("stop-on-signals", { required: false })
254+
.split(",")
255+
.map((i) => i.trim())
256+
.filter((i) => i !== "");
257+
229258
return {
230259
projectName,
231260
owner,
@@ -243,6 +272,7 @@ function githubInputs() {
243272
hideCloudWatchLogs,
244273
disableGithubEnvVars,
245274
artifactsTypeOverride,
275+
stopOnSignals,
246276
};
247277
}
248278

0 commit comments

Comments
 (0)