Improve CI workflow polling: skip when no workflow files, lock onto run by ID, prefer push events
This commit is contained in:
52
gp
52
gp
@@ -543,6 +543,11 @@ wait_for_workflow() {
|
||||
return 0 # silently skip if not a Gitea remote or no token
|
||||
fi
|
||||
|
||||
# Skip if no workflow files exist in the repo
|
||||
if ! ls .gitea/workflows/*.yml .gitea/workflows/*.yaml .github/workflows/*.yml .github/workflows/*.yaml 2>/dev/null | grep -q .; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
local spinner_chars='⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'
|
||||
local spin_idx=0
|
||||
local poll_interval=3
|
||||
@@ -581,24 +586,38 @@ wait_for_workflow() {
|
||||
fi
|
||||
|
||||
# Poll API for the run
|
||||
local run_info=""
|
||||
if [ "$found" = true ] && [ -n "$run_id" ]; then
|
||||
# Already locked onto a run - query it directly by ID
|
||||
run_info=$(curl -sf "${GITEA_API_BASE}/actions/runs/${run_id}" -H "Authorization: ${GITEA_AUTH_HEADER}" 2>/dev/null \
|
||||
| python3 -c "
|
||||
import json, sys
|
||||
r = json.load(sys.stdin)
|
||||
print(r['id'], r['status'], r.get('conclusion',''), r.get('html_url',''))
|
||||
" 2>/dev/null) || true
|
||||
else
|
||||
# Search for a run matching our commit SHA (prefer push events)
|
||||
local response
|
||||
response=$(curl -sf "${GITEA_API_BASE}/actions/runs?limit=5" -H "Authorization: ${GITEA_AUTH_HEADER}" 2>/dev/null) || true
|
||||
|
||||
response=$(curl -sf "${GITEA_API_BASE}/actions/runs?limit=20" -H "Authorization: ${GITEA_AUTH_HEADER}" 2>/dev/null) || true
|
||||
if [ -n "$response" ]; then
|
||||
# Find the run matching our commit SHA
|
||||
local run_info
|
||||
run_info=$(echo "$response" | python3 -c "
|
||||
import json, sys
|
||||
d = json.load(sys.stdin)
|
||||
for r in d.get('workflow_runs', []):
|
||||
if r.get('head_sha','').startswith('${head_sha}'):
|
||||
print(r['id'], r['status'], r.get('conclusion',''), r.get('html_url',''))
|
||||
break
|
||||
runs = [r for r in d.get('workflow_runs', []) if r.get('head_sha','').startswith('${head_sha}')]
|
||||
# Prefer push-triggered runs over workflow_run-triggered ones
|
||||
push_runs = [r for r in runs if r.get('event') == 'push']
|
||||
pick = push_runs[0] if push_runs else (runs[0] if runs else None)
|
||||
if pick:
|
||||
print(pick['id'], pick['status'], pick.get('conclusion',''), pick.get('html_url',''))
|
||||
" 2>/dev/null) || true
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -n "$run_info" ]; then
|
||||
if [ "$found" = false ]; then
|
||||
found=true
|
||||
run_id=$(echo "$run_info" | awk '{print $1}')
|
||||
fi
|
||||
local run_status
|
||||
run_status=$(echo "$run_info" | awk '{print $2}')
|
||||
local run_conclusion
|
||||
@@ -667,7 +686,7 @@ for j in d.get('jobs', []):
|
||||
elif status == 'completed' and conclusion == 'failure':
|
||||
icon = '\033[0;31m✗\033[0m'
|
||||
label = ''
|
||||
elif status == 'running':
|
||||
elif status in ('running', 'in_progress'):
|
||||
icon = '${spin_char}'
|
||||
label = ' running'
|
||||
elif status == 'waiting':
|
||||
@@ -691,20 +710,27 @@ for j in d.get('jobs', []):
|
||||
elapsed=$((elapsed + poll_interval))
|
||||
continue
|
||||
fi
|
||||
|
||||
# No run found yet - give up after 15 seconds (no workflow configured)
|
||||
if [ "$elapsed" -ge 15 ]; then
|
||||
local i
|
||||
for ((i=0; i<prev_lines; i++)); do
|
||||
printf "\033[A\033[K"
|
||||
done
|
||||
print_info "No CI workflow found"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# No run found yet - show waiting spinner
|
||||
# Show waiting spinner
|
||||
local spin_char="${spinner_chars:$spin_idx:1}"
|
||||
spin_idx=$(( (spin_idx + 1) % ${#spinner_chars} ))
|
||||
local mins=$((elapsed / 60))
|
||||
local secs=$((elapsed % 60))
|
||||
|
||||
# Clear previous output
|
||||
local i
|
||||
for ((i=0; i<prev_lines; i++)); do
|
||||
printf "\033[A\033[K"
|
||||
done
|
||||
printf " %s waiting for workflow to start (%d:%02d)\n" "$spin_char" "$mins" "$secs"
|
||||
printf " %s waiting for workflow to start\n" "$spin_char"
|
||||
prev_lines=1
|
||||
|
||||
sleep "$poll_interval"
|
||||
|
||||
Reference in New Issue
Block a user