Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/workflows/pr_close_cancel_job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Cancel runs on PR close
Copy link
Copy Markdown
Collaborator

@wangxiyuan wangxiyuan Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename this file to pr_close_cancel_job to keep the same style like others

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

on:
pull_request:
types: [closed]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the pr is merged, does this job still work?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. closed includes merged.


permissions:
actions: write
contents: read

jobs:
cancel:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
github-token: ${{ github.token }}
script: |
const { owner, repo } = context.repo;
const branch = context.payload.pull_request.head.ref;

const statuses = ["in_progress", "queued", "waiting", "pending", "requested"];
for (const status of statuses) {
let page = 1;
while (true) {
const resp = await github.rest.actions.listWorkflowRunsForRepo({
owner, repo, branch, status, per_page: 100, page
});

const runs = resp.data.workflow_runs;
if (!runs.length) break;

for (const run of runs) {
if (run.id === context.runId) continue; // don't cancel this workflow
try {
await github.rest.actions.cancelWorkflowRun({ owner, repo, run_id: run.id });
core.info(`Cancel requested: ${run.html_url}`);
} catch (e) {
// common reasons: already completed (409) or insufficient permissions (403)
core.warning(`Failed to cancel ${run.html_url}: ${e.message}`);
}
}

if (runs.length < 100) break;
page++;
}
}