-
Notifications
You must be signed in to change notification settings - Fork 34
41 lines (38 loc) · 1.38 KB
/
cancel-running-workflows.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
name: Cancel Workflow Runs
on:
pull_request:
types: [ synchronize ]
jobs:
cancel-runs:
runs-on: ubuntu-latest
steps:
- name: Extract branch name
shell: bash
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
id: extract_branch
- name: Get Workflow Runs
id: get_runs
uses: actions/github-script@v4
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { data: runs } = await github.actions.listRepoWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
per_page: 100,
status: 'in_progress' // Filter for in-progress runs
});
return runs;
- name: Cancel Workflow Runs
if: ${{ always() }}
run: |
for run in "${{ steps.get_runs.outputs.runtimes }}"; do
# Cancel runs based on conditions
# Example: cancel runs triggered by the main branch
if [[ $run.branch == ${{ steps.extract_branch.outputs.branch }} ]]; then
curl -X POST \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/actions/runs/${run.id}/cancel
fi
done