-
Notifications
You must be signed in to change notification settings - Fork 104
59 lines (47 loc) · 1.78 KB
/
cancel-ci-draft-pr.yml
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
name: Cancel CI of Draft PR
on:
workflow_run:
workflows: [CI Workflow]
types:
- requested
jobs:
check:
name: Check for draft PR
runs-on: ubuntu-latest
steps:
- name: Checkout the code
uses: actions/checkout@main
- name: Check workflow run
shell: bash
run: |
if [[ "${{ github.event.workflow_run.event }}" == "pull_request" || "${{ github.event.workflow_run.event }}" == "pull_request_target" ]]; then
echo "🔘 Detected pull_request event; start processing..."
echo ""
run_id=${{ github.event.workflow_run.id }}
run_sha=${{ github.event.workflow_run.head_sha }}
echo "Workflow run id: ${run_id}"
echo "Workflow run head_sha: ${run_sha}"
echo ""
echo "🔘 Retrieve head_sha of Draft PRs..."
gh pr list --json isDraft,commits --jq '.[] | select(.isDraft) | .commits | last | .oid' > draft_prs.txt
echo "Draft PRs head_sha:"
cat draft_prs.txt
echo ""
for pr_sha in $(cat draft_prs.txt); do
echo "🔘 Check workflow run against Draft PR with head_sha = ${pr_sha}..."
if [ "${run_sha}" == "${pr_sha}" ]; then
echo " ⚠️ Workflow run is associated to a Draft PR!"
echo " Cancelling workflow run ${run_id}..."
gh run cancel ${run_id}
echo " ❌ Workflow run cancelled!"
exit 0
else
echo " ✅ Done"
fi
echo "✅ No Draft PR associated to this workflow run"
done
else
echo "✅ No pull_request event detected; skipping..."
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}