Skip to content

Commit a096d79

Browse files
committed
ci: cancel closed-PR runs from a pull_request trigger instead of pull_request_target
1 parent 27d5ca6 commit a096d79

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

‎.github/workflows/pr-cancel-on-close.yml‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
name: Cancel runs of closed PRs
22

33
# A merged or closed PR gates nothing, so its queued and running runs only hold runners.
4-
# `pull_request_target` runs the base branch's workflow with a token that can cancel runs of
5-
# fork PRs too; it never checks out the PR's code.
64
on:
7-
pull_request_target:
5+
pull_request:
86
types:
97
- closed
108

@@ -13,6 +11,8 @@ permissions:
1311

1412
jobs:
1513
cancel:
14+
# A fork PR's token is read-only, so it cannot cancel runs.
15+
if: github.event.pull_request.head.repo.full_name == github.repository
1616
runs-on: ubuntu-latest
1717
timeout-minutes: 5
1818
steps:
@@ -21,13 +21,14 @@ jobs:
2121
env:
2222
GH_TOKEN: ${{ github.token }}
2323
HEAD_REF: ${{ github.event.pull_request.head.ref }}
24-
HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
24+
RUN_ID: ${{ github.run_id }}
2525
run: |
2626
gh api --paginate -X GET "repos/$GITHUB_REPOSITORY/actions/runs" \
2727
-f branch="$HEAD_REF" -f event=pull_request -f per_page=100 \
2828
--jq ".workflow_runs[]
2929
| select(.status != \"completed\")
30-
| select(.head_repository.full_name == \"$HEAD_REPO\")
30+
| select(.head_repository.full_name == \"$GITHUB_REPOSITORY\")
31+
| select(.id != $RUN_ID)
3132
| select(.path != \".github/workflows/pr-preview-cleanup.yml\")
3233
| .id" |
3334
while read -r id; do

0 commit comments

Comments
 (0)