|
| 1 | +# Copyright (c) 2026 Arpit Khandelwal |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: BSL-1.0 |
| 4 | +# Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | +# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | + |
| 7 | +name: Cancel Workflows |
| 8 | + |
| 9 | +on: |
| 10 | + pull_request_target: |
| 11 | + types: [labeled] |
| 12 | + issue_comment: |
| 13 | + types: [created] |
| 14 | + |
| 15 | +jobs: |
| 16 | + cancel-all: |
| 17 | + # Trigger if label 'cancel-ci' is added OR if a comment contains '/cancel-ci' |
| 18 | + if: | |
| 19 | + (github.event_name == 'pull_request_target' && github.event.label.name == 'cancel-ci') || |
| 20 | + (github.event_name == 'issue_comment' && github.event.issue.pull_request && contains(github.event.comment.body, '/cancel-ci')) |
| 21 | + runs-on: ubuntu-latest |
| 22 | + permissions: |
| 23 | + actions: write |
| 24 | + pull-requests: write |
| 25 | + issues: write |
| 26 | + steps: |
| 27 | + - name: Authorize and Extract PR Number |
| 28 | + id: context |
| 29 | + env: |
| 30 | + EVENT_NAME: ${{ github.event_name }} |
| 31 | + AUTHOR_ASSOC: ${{ github.event.comment.author_association }} |
| 32 | + COMMENT_USER_ID: ${{ github.event.comment.user.id }} |
| 33 | + PR_AUTHOR_ID: ${{ github.event.issue.user.id }} |
| 34 | + run: | |
| 35 | + if [ "$EVENT_NAME" == "issue_comment" ]; then |
| 36 | + # Authorize: Maintainers (OWNER, MEMBER, COLLABORATOR) or PR Author |
| 37 | + if [[ "$AUTHOR_ASSOC" =~ ^(OWNER|MEMBER|COLLABORATOR)$ ]] || [ "$COMMENT_USER_ID" == "$PR_AUTHOR_ID" ]; then |
| 38 | + echo "User authorized." |
| 39 | + else |
| 40 | + echo "Error: Unauthorized user attempted to cancel CI." |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | + echo "pr_number=${{ github.event.issue.number }}" >> "$GITHUB_OUTPUT" |
| 44 | + else |
| 45 | + # Labels addition is already restricted by GitHub to those with write access |
| 46 | + echo "pr_number=${{ github.event.pull_request.number }}" >> "$GITHUB_OUTPUT" |
| 47 | + fi |
| 48 | +
|
| 49 | + - name: Cancel Workflow Runs |
| 50 | + env: |
| 51 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 52 | + PR_NUMBER: ${{ steps.context.outputs.pr_number }} |
| 53 | + CURRENT_RUN_ID: ${{ github.run_id }} |
| 54 | + run: | |
| 55 | + echo "Cancelling runs for PR #$PR_NUMBER..." |
| 56 | +
|
| 57 | + # Get all runs for the PR that are NOT completed, excluding this current run |
| 58 | + RUN_IDS=$(gh run list --pr "$PR_NUMBER" --limit 200 --json databaseId,status --jq '.[] | select(.status != "completed" and .databaseId != (env.CURRENT_RUN_ID | tonumber)) | .databaseId') |
| 59 | +
|
| 60 | + if [ -z "$RUN_IDS" ]; then |
| 61 | + echo "No active runs found to cancel." |
| 62 | + else |
| 63 | + echo "Found the following run IDs to cancel:" |
| 64 | + echo "$RUN_IDS" |
| 65 | + for ID in $RUN_IDS; do |
| 66 | + echo "Cancelling run $ID..." |
| 67 | + gh run cancel "$ID" || echo "Failed to cancel run $ID (it might have already finished)" |
| 68 | + done |
| 69 | + fi |
| 70 | +
|
| 71 | + # Remove label if triggered by label |
| 72 | + if [ "${{ github.event_name }}" == "pull_request_target" ]; then |
| 73 | + echo "Removing 'cancel-ci' label..." |
| 74 | + gh pr edit "$PR_NUMBER" --remove-label "cancel-ci" || echo "Failed to remove label" |
| 75 | + fi |
0 commit comments