Skip to content

Commit e10bd78

Browse files
committed
Dispatch merge queue cancellation separately
1 parent 76e0e6e commit e10bd78

3 files changed

Lines changed: 82 additions & 30 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
name="$1"
6+
7+
echo "::error::Requesting merge queue workflow cancellation because ${name} failed"
8+
if ! gh workflow run cancel-merge-queue-workflow.yml \
9+
--repo "${GITHUB_REPOSITORY}" \
10+
--ref "${GITHUB_REF_NAME}" \
11+
-f run_id="${GITHUB_RUN_ID}" \
12+
-f run_attempt="${GITHUB_RUN_ATTEMPT}"
13+
then
14+
echo "::warning::Failed to dispatch cancel-merge-queue-workflow.yml"
15+
fi
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Cancel merge queue workflow
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
run_id:
7+
description: Workflow run to cancel
8+
required: true
9+
run_attempt:
10+
description: Workflow run attempt to cancel
11+
required: true
12+
13+
defaults:
14+
run:
15+
shell: bash --noprofile --norc -euo pipefail {0}
16+
17+
permissions:
18+
actions: write
19+
20+
jobs:
21+
cancel:
22+
runs-on: ubuntu-latest
23+
steps:
24+
- name: Cancel merge queue workflow after failure is recorded
25+
env:
26+
GH_TOKEN: ${{ github.token }}
27+
RUN_ID: ${{ inputs.run_id }}
28+
RUN_ATTEMPT: ${{ inputs.run_attempt }}
29+
run: |
30+
# Wait until GitHub records the failed job before cancelling the workflow, so the
31+
# failing job keeps the failure conclusion instead of being finalized as cancelled.
32+
for attempt in {1..30}; do
33+
failed_jobs=$(gh api --paginate "/repos/${GITHUB_REPOSITORY}/actions/runs/${RUN_ID}/attempts/${RUN_ATTEMPT}/jobs" \
34+
--jq '.jobs[] | select(.status == "completed" and .conclusion == "failure") | .name' \
35+
| paste -sd ',' - \
36+
| sed 's/,/, /g')
37+
if [[ -n "${failed_jobs}" ]]; then
38+
echo "::notice::Cancelling merge queue workflow after failed jobs were recorded: ${failed_jobs}"
39+
run_status=$(gh api "/repos/${GITHUB_REPOSITORY}/actions/runs/${RUN_ID}" --jq .status)
40+
if [[ "${run_status}" == "completed" ]]; then
41+
echo "::notice::Workflow run ${RUN_ID} is already completed"
42+
exit 0
43+
fi
44+
if ! gh run cancel "${RUN_ID}" --repo "${GITHUB_REPOSITORY}"; then
45+
run_status=$(gh api "/repos/${GITHUB_REPOSITORY}/actions/runs/${RUN_ID}" --jq .status)
46+
if [[ "${run_status}" == "completed" ]]; then
47+
echo "::notice::Workflow run ${RUN_ID} completed before it could be cancelled"
48+
exit 0
49+
fi
50+
exit 1
51+
fi
52+
exit 0
53+
fi
54+
sleep 2
55+
done
56+
echo "::warning::Failed to find a job with conclusion=failure for run ${RUN_ID} attempt ${RUN_ATTEMPT}; not cancelling workflow"
57+
exit 1

.github/workflows/ci.yml

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ jobs:
103103
if: ${{ failure() && github.event_name == 'merge_group' }}
104104
env:
105105
GH_TOKEN: ${{ github.token }}
106-
run: |
107-
echo "::error::Cancelling merge queue workflow because maven-checks ${{ matrix.java-version }} failed"
108-
gh run cancel "${GITHUB_RUN_ID}" --repo "${GITHUB_REPOSITORY}"
106+
run: .github/bin/cancel-merge-queue-workflow.sh "maven-checks ${{ matrix.java-version }}"
109107

110108
artifact-checks:
111109
needs: path-filters
@@ -145,9 +143,7 @@ jobs:
145143
if: ${{ failure() && github.event_name == 'merge_group' }}
146144
env:
147145
GH_TOKEN: ${{ github.token }}
148-
run: |
149-
echo "::error::Cancelling merge queue workflow because artifact-checks failed"
150-
gh run cancel "${GITHUB_RUN_ID}" --repo "${GITHUB_REPOSITORY}"
146+
run: .github/bin/cancel-merge-queue-workflow.sh "artifact-checks"
151147

152148
check-commits-dispatcher:
153149
needs: path-filters
@@ -237,9 +233,7 @@ jobs:
237233
if: ${{ failure() && github.event_name == 'merge_group' }}
238234
env:
239235
GH_TOKEN: ${{ github.token }}
240-
run: |
241-
echo "::error::Cancelling merge queue workflow because error-prone-checks failed"
242-
gh run cancel "${GITHUB_RUN_ID}" --repo "${GITHUB_REPOSITORY}"
236+
run: .github/bin/cancel-merge-queue-workflow.sh "error-prone-checks"
243237

244238
test-jdbc-compatibility:
245239
needs: path-filters
@@ -286,9 +280,7 @@ jobs:
286280
if: ${{ failure() && github.event_name == 'merge_group' }}
287281
env:
288282
GH_TOKEN: ${{ github.token }}
289-
run: |
290-
echo "::error::Cancelling merge queue workflow because test-jdbc-compatibility failed"
291-
gh run cancel "${GITHUB_RUN_ID}" --repo "${GITHUB_REPOSITORY}"
283+
run: .github/bin/cancel-merge-queue-workflow.sh "test-jdbc-compatibility"
292284

293285
hive-tests:
294286
needs: path-filters
@@ -351,9 +343,7 @@ jobs:
351343
if: ${{ failure() && github.event_name == 'merge_group' }}
352344
env:
353345
GH_TOKEN: ${{ github.token }}
354-
run: |
355-
echo "::error::Cancelling merge queue workflow because hive-tests (${{ matrix.config }}) failed"
356-
gh run cancel "${GITHUB_RUN_ID}" --repo "${GITHUB_REPOSITORY}"
346+
run: .github/bin/cancel-merge-queue-workflow.sh "hive-tests (${{ matrix.config }})"
357347

358348
test-other-modules:
359349
needs: path-filters
@@ -446,9 +436,7 @@ jobs:
446436
if: ${{ failure() && github.event_name == 'merge_group' }}
447437
env:
448438
GH_TOKEN: ${{ github.token }}
449-
run: |
450-
echo "::error::Cancelling merge queue workflow because test-other-modules failed"
451-
gh run cancel "${GITHUB_RUN_ID}" --repo "${GITHUB_REPOSITORY}"
439+
run: .github/bin/cancel-merge-queue-workflow.sh "test-other-modules"
452440

453441
build-test-matrix:
454442
needs: path-filters
@@ -569,9 +557,7 @@ jobs:
569557
if: ${{ failure() && github.event_name == 'merge_group' }}
570558
env:
571559
GH_TOKEN: ${{ github.token }}
572-
run: |
573-
echo "::error::Cancelling merge queue workflow because build-test-matrix failed"
574-
gh run cancel "${GITHUB_RUN_ID}" --repo "${GITHUB_REPOSITORY}"
560+
run: .github/bin/cancel-merge-queue-workflow.sh "build-test-matrix"
575561

576562
test:
577563
runs-on: 'ubuntu-latest'
@@ -876,9 +862,7 @@ jobs:
876862
if: ${{ failure() && github.event_name == 'merge_group' }}
877863
env:
878864
GH_TOKEN: ${{ github.token }}
879-
run: |
880-
echo "::error::Cancelling merge queue workflow because test (${{ matrix.modules }}, ${{ matrix.profile }}, ${{ matrix.jdk }}) failed"
881-
gh run cancel "${GITHUB_RUN_ID}" --repo "${GITHUB_REPOSITORY}"
865+
run: .github/bin/cancel-merge-queue-workflow.sh "test (${{ matrix.modules }}, ${{ matrix.profile }}, ${{ matrix.jdk }})"
882866

883867
build-pt:
884868
needs: path-filters
@@ -1079,9 +1063,7 @@ jobs:
10791063
if: ${{ failure() && github.event_name == 'merge_group' }}
10801064
env:
10811065
GH_TOKEN: ${{ github.token }}
1082-
run: |
1083-
echo "::error::Cancelling merge queue workflow because build-pt failed"
1084-
gh run cancel "${GITHUB_RUN_ID}" --repo "${GITHUB_REPOSITORY}"
1066+
run: .github/bin/cancel-merge-queue-workflow.sh "build-pt"
10851067

10861068
pt:
10871069
runs-on: 'ubuntu-latest'
@@ -1176,9 +1158,7 @@ jobs:
11761158
if: ${{ failure() && github.event_name == 'merge_group' }}
11771159
env:
11781160
GH_TOKEN: ${{ github.token }}
1179-
run: |
1180-
echo "::error::Cancelling merge queue workflow because pt (${{ matrix.config }}, ${{ matrix.suite }}, ${{ matrix.jdk }}) failed"
1181-
gh run cancel "${GITHUB_RUN_ID}" --repo "${GITHUB_REPOSITORY}"
1161+
run: .github/bin/cancel-merge-queue-workflow.sh "pt (${{ matrix.config }}, ${{ matrix.suite }}, ${{ matrix.jdk }})"
11821162

11831163
build-success:
11841164
if: ${{ always() }} # if `failure()` would not work for cancellations, `!success()` would not work for skipped jobs

0 commit comments

Comments
 (0)