diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml new file mode 100644 index 0000000..e79a154 --- /dev/null +++ b/.github/workflows/cancel.yml @@ -0,0 +1,30 @@ +name: Cancel runs + +on: + delete: + pull_request: + types: + - closed + +permissions: + actions: write + +jobs: + cancel: + name: Cancel the runs of a closed branch + runs-on: ubuntu-latest + if: github.event_name == 'pull_request' || github.event.ref_type == 'branch' + + steps: + - name: Cancel every queued or running workflow of the branch + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GH_REPO: ${{ github.repository }} + BRANCH: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || github.event.ref }} + run: | + set -euo pipefail + gh run list --branch "${BRANCH}" --limit 100 \ + --json databaseId,status \ + --jq '.[] | select(.status != "completed") | .databaseId' \ + | sed "/^${GITHUB_RUN_ID}$/d" \ + | xargs -r -n1 gh run cancel