Cancel CI of Draft PR #1553
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |