|
1 | 1 | # Fork of https://github.com/AgentWorkforce/relay/blob/v12.2.2/.github/workflows/cancel-on-merge.yml |
2 | 2 | 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. |
3 | 10 | on: |
4 | | - pull_request: |
| 11 | + pull_request_target: |
5 | 12 | types: [closed] |
6 | 13 |
|
| 14 | +permissions: |
| 15 | + actions: write # to cancel the workflow runs |
| 16 | + |
7 | 17 | jobs: |
8 | 18 | cancel: |
9 | 19 | if: github.event.pull_request.merged == true |
10 | 20 | runs-on: ubuntu-latest |
11 | 21 | 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 |
14 | 24 | with: |
15 | 25 | script: | |
16 | 26 | const { owner, repo } = context.repo; |
17 | | - const headSha = context.payload.pull_request.head.sha; |
| 27 | + const pr = context.payload.pull_request; |
18 | 28 |
|
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, { |
21 | 33 | owner, |
22 | 34 | repo, |
23 | | - status: 'in_progress', |
24 | | - head_sha: headSha, |
| 35 | + head_sha: pr.head.sha, |
| 36 | + per_page: 100, |
25 | 37 | }); |
26 | 38 |
|
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 | + } |
35 | 63 | } |
36 | 64 |
|
37 | | - console.log(`Cancelled ${runs.data.workflow_runs.length} workflow runs`); |
| 65 | + console.log(`Cancelled ${cancelled} workflow runs`); |