Skip to content

Commit ae601b4

Browse files
authored
Enhance CI waiting logic with timeout and status checks
Updated the CI waiting logic to include a timeout and improved status handling for auto-merge functionality.
1 parent cd0e485 commit ae601b4

File tree

1 file changed

+100
-38
lines changed

1 file changed

+100
-38
lines changed

.github/workflows/auto-merge-queue.yml

Lines changed: 100 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -112,50 +112,114 @@ jobs:
112112
env:
113113
GITHUB_TOKEN: ${{ secrets.MERGE_QUEUE_PAT }}
114114

115-
- name: Verify All Required Checks Passed
116-
id: check_status
115+
- name: Wait for CI Completion (with 10min timeout)
116+
id: wait_for_ci
117117
if: steps.check_label.outputs.has_auto_merge_label == 'true'
118118
run: |
119119
PR_NUMBER="${{ steps.get_pr.outputs.pr_number }}"
120-
echo "Checking CI status for PR #$PR_NUMBER"
120+
echo "⏳ Waiting for CI completion on PR #$PR_NUMBER (timeout: 10 minutes)"
121121
122-
# Get PR details and check status
123-
CHECKS_STATUS=$(gh api graphql \
124-
-f owner="${{ github.repository_owner }}" \
125-
-f repo="${{ github.event.repository.name }}" \
126-
-F number="$PR_NUMBER" \
127-
-f query='
128-
query($owner: String!, $repo: String!, $number: Int!) {
129-
repository(owner: $owner, name: $repo) {
130-
pullRequest(number: $number) {
131-
commits(last: 1) {
132-
nodes {
133-
commit {
134-
statusCheckRollup {
135-
state
122+
# Function to check CI status
123+
check_ci_status() {
124+
gh api graphql \
125+
-f owner="${{ github.repository_owner }}" \
126+
-f repo="${{ github.event.repository.name }}" \
127+
-F number="$PR_NUMBER" \
128+
-f query='
129+
query($owner: String!, $repo: String!, $number: Int!) {
130+
repository(owner: $owner, name: $repo) {
131+
pullRequest(number: $number) {
132+
commits(last: 1) {
133+
nodes {
134+
commit {
135+
statusCheckRollup {
136+
state
137+
}
136138
}
137139
}
138140
}
139141
}
140142
}
141-
}
142-
}' --jq '.data.repository.pullRequest.commits.nodes[0].commit.statusCheckRollup.state')
143+
}' --jq '.data.repository.pullRequest.commits.nodes[0].commit.statusCheckRollup.state'
144+
}
143145
144-
echo "CI Status: $CHECKS_STATUS"
145-
echo "checks_status=$CHECKS_STATUS" >> $GITHUB_OUTPUT
146+
# Initial status check
147+
CHECKS_STATUS=$(check_ci_status)
148+
echo "Initial CI Status: $CHECKS_STATUS"
146149
147150
if [[ "$CHECKS_STATUS" == "SUCCESS" ]]; then
148-
echo "✅ All CI checks passed"
149-
else
150-
echo "❌ CI checks not successful (status: $CHECKS_STATUS)"
151+
echo "✅ CI checks already passed"
152+
echo "checks_status=SUCCESS" >> $GITHUB_OUTPUT
153+
exit 0
151154
fi
155+
156+
# Post initial waiting message
157+
gh api repos/${{ github.repository }}/issues/$PR_NUMBER/comments \
158+
-f body="⏳ Auto-merge label detected! Waiting for CI checks to complete (timeout: 10 minutes)..."
159+
160+
# Wait up to 10 minutes (600 seconds) with 30-second intervals
161+
TIMEOUT=600
162+
INTERVAL=30
163+
ELAPSED=0
164+
165+
while [[ $ELAPSED -lt $TIMEOUT ]]; do
166+
sleep $INTERVAL
167+
ELAPSED=$((ELAPSED + INTERVAL))
168+
169+
CHECKS_STATUS=$(check_ci_status)
170+
echo "[$ELAPSED/${TIMEOUT}s] CI Status: $CHECKS_STATUS"
171+
172+
case "$CHECKS_STATUS" in
173+
"SUCCESS")
174+
echo "✅ All CI checks passed after ${ELAPSED}s"
175+
echo "checks_status=SUCCESS" >> $GITHUB_OUTPUT
176+
exit 0
177+
;;
178+
"FAILURE"|"ERROR")
179+
echo "❌ CI checks failed (status: $CHECKS_STATUS)"
180+
echo "checks_status=$CHECKS_STATUS" >> $GITHUB_OUTPUT
181+
gh api repos/${{ github.repository }}/issues/$PR_NUMBER/comments \
182+
-f body="❌ CI checks failed (status: $CHECKS_STATUS). Cannot add to merge queue."
183+
exit 0
184+
;;
185+
"PENDING"|"EXPECTED"|null)
186+
# Continue waiting
187+
if [[ $((ELAPSED % 120)) -eq 0 ]]; then # Update every 2 minutes
188+
gh api repos/${{ github.repository }}/issues/$PR_NUMBER/comments \
189+
-f body="⏳ Still waiting for CI checks... (${ELAPSED}s elapsed, status: $CHECKS_STATUS)"
190+
fi
191+
;;
192+
*)
193+
echo "⚠️ Unknown CI status: $CHECKS_STATUS, continuing to wait..."
194+
;;
195+
esac
196+
done
197+
198+
# Timeout reached
199+
echo "⏰ Timeout reached after 10 minutes"
200+
echo "checks_status=TIMEOUT" >> $GITHUB_OUTPUT
201+
gh api repos/${{ github.repository }}/issues/$PR_NUMBER/comments \
202+
-f body="⏰ Timeout: CI checks did not complete within 10 minutes. Please check CI status and re-add auto-merge label if needed."
203+
env:
204+
GITHUB_TOKEN: ${{ secrets.MERGE_QUEUE_PAT }}
205+
206+
- name: Verify Final CI Status
207+
id: check_status
208+
if: |
209+
steps.check_label.outputs.has_auto_merge_label == 'true' &&
210+
steps.wait_for_ci.outputs.checks_status == 'SUCCESS'
211+
run: |
212+
PR_NUMBER="${{ steps.get_pr.outputs.pr_number }}"
213+
CHECKS_STATUS="${{ steps.wait_for_ci.outputs.checks_status }}"
214+
echo "Final CI Status: $CHECKS_STATUS"
215+
echo "checks_status=$CHECKS_STATUS" >> $GITHUB_OUTPUT
152216
env:
153217
GITHUB_TOKEN: ${{ secrets.MERGE_QUEUE_PAT }}
154218

155219
- name: Add to Merge Queue
156220
if: |
157221
steps.check_label.outputs.has_auto_merge_label == 'true' &&
158-
steps.check_status.outputs.checks_status == 'SUCCESS'
222+
steps.wait_for_ci.outputs.checks_status == 'SUCCESS'
159223
run: |
160224
PR_NUMBER="${{ steps.get_pr.outputs.pr_number }}"
161225
@@ -202,10 +266,7 @@ jobs:
202266
203267
# Comment on PR with success
204268
gh api repos/${{ github.repository }}/issues/$PR_NUMBER/comments \
205-
-f body="🤖 Automatically added to merge queue at position **$QUEUE_POSITION** after CI success ✅
206-
207-
Event: ${{ github.event_name }}
208-
Trigger: Auto-merge label detected with successful CI"
269+
-f body="🤖 Automatically added to merge queue at position **$QUEUE_POSITION** after CI success ✅ (Event: ${{ github.event_name }}, Trigger: Auto-merge label detected with successful CI)"
209270
210271
env:
211272
GITHUB_TOKEN: ${{ secrets.MERGE_QUEUE_PAT }}
@@ -214,19 +275,22 @@ Trigger: Auto-merge label detected with successful CI"
214275
if: |
215276
steps.get_pr.outputs.should_run == 'true' &&
216277
(steps.check_label.outputs.has_auto_merge_label != 'true' ||
217-
steps.check_status.outputs.checks_status != 'SUCCESS')
278+
(steps.wait_for_ci.outputs.checks_status != 'SUCCESS' && steps.wait_for_ci.outputs.checks_status != ''))
218279
run: |
219280
PR_NUMBER="${{ steps.get_pr.outputs.pr_number }}"
220281
HAS_LABEL="${{ steps.check_label.outputs.has_auto_merge_label }}"
221-
CHECKS_STATUS="${{ steps.check_status.outputs.checks_status }}"
282+
CHECKS_STATUS="${{ steps.wait_for_ci.outputs.checks_status }}"
222283
223284
echo "❌ Conditions not met for PR #$PR_NUMBER:"
224285
echo " - Has auto-merge label: $HAS_LABEL"
225-
echo " - CI status: $CHECKS_STATUS"
286+
echo " - CI status after waiting: $CHECKS_STATUS"
226287
227-
if [[ "$HAS_LABEL" == "true" && "$CHECKS_STATUS" != "SUCCESS" ]]; then
228-
gh api repos/${{ github.repository }}/issues/$PR_NUMBER/comments \
229-
-f body="⏳ PR has auto-merge label but CI checks are not successful yet (status: $CHECKS_STATUS). Will auto-queue when CI passes."
288+
if [[ "$HAS_LABEL" == "true" && "$CHECKS_STATUS" == "TIMEOUT" ]]; then
289+
echo "CI checks timed out after 10 minutes"
290+
elif [[ "$HAS_LABEL" == "true" && "$CHECKS_STATUS" == "FAILURE" ]]; then
291+
echo "CI checks failed"
292+
elif [[ "$HAS_LABEL" != "true" ]]; then
293+
echo "Auto-merge label not found"
230294
fi
231295
env:
232296
GITHUB_TOKEN: ${{ secrets.MERGE_QUEUE_PAT }}
@@ -240,9 +304,7 @@ Trigger: Auto-merge label detected with successful CI"
240304
241305
# Comment on PR with failure
242306
gh api repos/${{ github.repository }}/issues/$PR_NUMBER/comments \
243-
-f body="❌ Failed to add PR to merge queue automatically. Please check the [action logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) or add manually using GraphQL.
244-
245-
Error occurred during: ${{ github.event_name }} event"
307+
-f body="❌ Failed to add PR to merge queue automatically. Please check the [action logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) or add manually using GraphQL. Error occurred during: ${{ github.event_name }} event"
246308
fi
247309
env:
248310
GITHUB_TOKEN: ${{ secrets.MERGE_QUEUE_PAT }}

0 commit comments

Comments
 (0)