Skip to content

Commit dc60da8

Browse files
committed
add wait to avoid race conditions
Signed-off-by: Alyssa Goins <agoins@redhat.com>
1 parent 28f79e1 commit dc60da8

1 file changed

Lines changed: 33 additions & 14 deletions

File tree

.github/workflows/wait-for-gatekeeper.yml

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ jobs:
1515
1616
if [ "$EVENT" = "pull_request" ] || [ "$EVENT" = "pull_request_target" ]; then
1717
SHA="${{ github.event.pull_request.head.sha }}"
18+
MIN_WAIT=30
1819
elif [ "$EVENT" = "workflow_run" ]; then
1920
SHA="${{ github.event.workflow_run.head_sha }}"
21+
MIN_WAIT=0
2022
else
2123
echo "Event '${EVENT}' is not PR-related, skipping Gatekeeper check"
2224
exit 0
@@ -26,31 +28,48 @@ jobs:
2628
TIMEOUT=600
2729
INTERVAL=15
2830
ELAPSED=0
31+
SEEN_PENDING=false
2932
3033
echo "Waiting for '${CHECK_NAME}' check on ${SHA}..."
3134
3235
while [ $ELAPSED -lt $TIMEOUT ]; do
3336
RESULT=$(gh api "repos/${REPO}/commits/${SHA}/check-runs?per_page=100" \
34-
--jq "[.check_runs[] | select(.name == \"${CHECK_NAME}\")] | sort_by(.started_at) | last | \"\(.status) \(.conclusion)\"" \
35-
2>/dev/null || true)
37+
--jq "
38+
[.check_runs[] | select(.name == \"${CHECK_NAME}\")] |
39+
if length == 0 then \"not_found\"
40+
elif any(.status != \"completed\") then \"pending\"
41+
else sort_by(.started_at) | last | \"\(.status) \(.conclusion)\"
42+
end
43+
" 2>/dev/null || true)
3644
37-
if [ -n "$RESULT" ] && [ "$RESULT" != "null null" ]; then
38-
RUN_STATUS=$(echo "$RESULT" | awk '{print $1}')
39-
CONCLUSION=$(echo "$RESULT" | awk '{print $2}')
40-
41-
if [ "$RUN_STATUS" = "completed" ]; then
42-
if [ "$CONCLUSION" = "success" ]; then
45+
case "$RESULT" in
46+
"not_found")
47+
SEEN_PENDING=true
48+
echo "Gatekeeper check not found yet (${ELAPSED}s elapsed, polling every ${INTERVAL}s)"
49+
;;
50+
"pending")
51+
SEEN_PENDING=true
52+
echo "Gatekeeper: in progress (${ELAPSED}s elapsed, polling every ${INTERVAL}s)"
53+
;;
54+
"completed success")
55+
if [ "$SEEN_PENDING" = "true" ] || [ $ELAPSED -ge $MIN_WAIT ]; then
4356
echo "Gatekeeper passed"
4457
exit 0
45-
else
58+
fi
59+
echo "Gatekeeper: found completed check before current run started, waiting for fresh result (${ELAPSED}s elapsed)"
60+
;;
61+
"completed "*)
62+
if [ "$SEEN_PENDING" = "true" ] || [ $ELAPSED -ge $MIN_WAIT ]; then
63+
CONCLUSION="${RESULT#completed }"
4664
echo "Gatekeeper failed (conclusion: $CONCLUSION)"
4765
exit 1
4866
fi
49-
fi
50-
echo "Gatekeeper: status=${RUN_STATUS} (${ELAPSED}s elapsed, polling every ${INTERVAL}s)"
51-
else
52-
echo "Gatekeeper check not found yet (${ELAPSED}s elapsed, polling every ${INTERVAL}s)"
53-
fi
67+
echo "Gatekeeper: found completed check before current run started, waiting for fresh result (${ELAPSED}s elapsed)"
68+
;;
69+
*)
70+
echo "Unexpected result: ${RESULT} (${ELAPSED}s elapsed)"
71+
;;
72+
esac
5473
5574
sleep $INTERVAL
5675
ELAPSED=$((ELAPSED + INTERVAL))

0 commit comments

Comments
 (0)