GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Cancel queued runs and runs of fork PRs when a PR is merged (#18026) · netty/netty@12c5eb7

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .github/workflows
    • cancel-on-merge.yml
    • ci-pr-reports.yml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.github/workflows/cancel-on-merge.yml‎

Copy file name to clipboard
+45-17Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,65 @@
11
# Fork of https://github.com/AgentWorkforce/relay/blob/v12.2.2/.github/workflows/cancel-on-merge.yml
22
name: Cancel PR Jobs on Merge
3+
4+
# We use pull_request_target (and not pull_request) as the GITHUB_TOKEN of a pull_request run triggered by a PR from
5+
# a fork is read-only and so we would not be allowed to cancel anything. This is safe as this workflow never checks out
6+
# or executes any code of the PR and only uses the event payload to look up the runs to cancel.
7+
#
8+
# Be aware that pull_request_target uses the workflow file of the base branch of the PR, so this file needs to exist
9+
# on every branch we merge PRs into.
310
on:
4-
pull_request:
11+
pull_request_target:
512
types: [closed]
613

14+
permissions:
15+
actions: write # to cancel the workflow runs
16+
717
jobs:
818
cancel:
919
if: github.event.pull_request.merged == true
1020
runs-on: ubuntu-latest
1121
steps:
12-
- name: Cancel running workflows for this PR
13-
uses: actions/github-script@v7
22+
- name: Cancel running and queued workflows for this PR
23+
uses: actions/github-script@v8
1424
with:
1525
script: |
1626
const { owner, repo } = context.repo;
17-
const headSha = context.payload.pull_request.head.sha;
27+
const pr = context.payload.pull_request;
1828
19-
// Get all running workflow runs for this commit
20-
const runs = await github.rest.actions.listWorkflowRunsForRepo({
29+
// Everything that has not completed yet, which includes runs that are still waiting for a runner.
30+
const active = new Set(['queued', 'in_progress', 'waiting', 'pending', 'requested']);
31+
32+
const runs = await github.paginate(github.rest.actions.listWorkflowRunsForRepo, {
2133
owner,
2234
repo,
23-
status: 'in_progress',
24-
head_sha: headSha,
35+
head_sha: pr.head.sha,
36+
per_page: 100,
2537
});
2638
27-
// Cancel each running workflow
28-
for (const run of runs.data.workflow_runs) {
29-
console.log(`Cancelling workflow run ${run.id} (${run.name})`);
30-
await github.rest.actions.cancelWorkflowRun({
31-
owner,
32-
repo,
33-
run_id: run.id,
34-
});
39+
let cancelled = 0;
40+
for (const run of runs) {
41+
if (!active.has(run.status) || run.id === context.runId) {
42+
continue;
43+
}
44+
// Only touch runs triggered by the PR itself and not by a push to a branch that happens to point to
45+
// the same commit.
46+
if (run.event !== 'pull_request') {
47+
continue;
48+
}
49+
// Runs of fork PRs have no pull_requests attached, for all others make sure the run does not belong
50+
// to another PR that uses the same head commit (for example one targeting a different branch).
51+
if (run.pull_requests.length > 0 && !run.pull_requests.some(p => p.number === pr.number)) {
52+
continue;
53+
}
54+
55+
console.log(`Cancelling workflow run ${run.id} (${run.name}, ${run.status})`);
56+
try {
57+
await github.rest.actions.cancelWorkflowRun({ owner, repo, run_id: run.id });
58+
cancelled++;
59+
} catch (e) {
60+
// Most likely the run completed in the meantime, there is nothing to free up in this case.
61+
console.log(`Could not cancel workflow run ${run.id}: ${e.message}`);
62+
}
3563
}
3664
37-
console.log(`Cancelled ${runs.data.workflow_runs.length} workflow runs`);
65+
console.log(`Cancelled ${cancelled} workflow runs`);
Collapse file

‎.github/workflows/ci-pr-reports.yml‎

Copy file name to clipboardExpand all lines: .github/workflows/ci-pr-reports.yml
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ concurrency:
3333

3434
jobs:
3535
tests:
36+
# Nothing to report if the run was cancelled, for example by cancel-on-merge.yml once the PR was merged.
37+
if: github.event.workflow_run.conclusion != 'cancelled'
3638
permissions:
3739
actions: read # for dawidd6/action-download-artifact to query and download artifacts
3840
checks: write # for scacap/action-surefire-report to publish result as PR check

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.