Skip to content

Cancel CI of Draft PR #1584

Cancel CI of Draft PR

Cancel CI of Draft PR #1584

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 }}