Files
github-readme-stats/.github/workflows/health-check.yml
T

259 lines
9.6 KiB
YAML

name: health check
on:
schedule:
# Every 15 minutes — matches the refresh cron cadence
- cron: '*/15 * * * *'
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:
concurrency:
group: health-check-${{ github.ref }}
cancel-in-progress: false
jobs:
tests:
name: pytest-${{ matrix.service }}
runs-on: ubuntu-latest
# Scheduled runs are an uptime probe only — skip the test matrix.
if: github.event_name != 'schedule'
strategy:
fail-fast: false
matrix:
service: [fetcher, generator, edge]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: pip
cache-dependency-path: ${{ matrix.service }}/requirements.txt
- name: Install dependencies
working-directory: ${{ matrix.service }}
run: pip install -r requirements.txt
- name: Run pytest
id: pytest
working-directory: ${{ matrix.service }}
run: |
set -o pipefail
pytest -v --tb=short --color=no 2>&1 | tee "/tmp/pytest-${SERVICE}.log"
env:
SERVICE: ${{ matrix.service }}
- name: Upload pytest log
if: always()
uses: actions/upload-artifact@v4
with:
name: pytest-${{ matrix.service }}
path: /tmp/pytest-${{ matrix.service }}.log
retention-days: 14
site-health:
name: site-health
runs-on: ubuntu-latest
# Only probe production on schedule / manual / push to main — not on
# PRs, where failures wouldn't indicate a prod incident.
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' || (github.event_name == 'push' && github.ref == 'refs/heads/main')
steps:
- name: Check edge homepage
id: check-edge
run: |
echo "GET https://gh-stats.com/"
HTTP_CODE=$(curl -sSL -o /tmp/edge-home.html -w "%{http_code}" --max-time 30 https://gh-stats.com/)
echo "HTTP $HTTP_CODE"
if [ "$HTTP_CODE" != "200" ]; then
echo "::error::Edge homepage returned HTTP $HTTP_CODE"
head -c 500 /tmp/edge-home.html || true
exit 1
fi
- name: Check widget SVG endpoint
id: check-widget
run: |
# Cloudflare routes /api/* to the generator directly. The bare
# /<user> and /<user>/<widget>.svg paths land on the SPA shell
# in prod, so we probe the canonical /api/<user>/<widget>.svg.
URL="https://gh-stats.com/api/ShayManor/grade.svg"
echo "GET $URL"
HTTP_CODE=$(curl -sS -D /tmp/widget-headers.txt -o /tmp/widget-body.svg \
-w "%{http_code}" --max-time 30 "$URL")
echo "HTTP $HTTP_CODE"
cat /tmp/widget-headers.txt
if [ "$HTTP_CODE" != "200" ]; then
echo "::error::Widget endpoint returned HTTP $HTTP_CODE"
exit 1
fi
CT=$(grep -i '^content-type:' /tmp/widget-headers.txt | tail -n1 | awk '{print $2}' | tr -d '\r' || true)
case "$CT" in
image/svg+xml*) : ;;
*) echo "::error::Unexpected Content-Type='$CT' (expected image/svg+xml)"; exit 1;;
esac
if ! head -c 200 /tmp/widget-body.svg | grep -q '<svg'; then
echo "::error::Response body does not look like SVG"
head -c 200 /tmp/widget-body.svg; echo
exit 1
fi
STATUS=$(grep -i '^x-widget-status:' /tmp/widget-headers.txt | tail -n1 | awk '{print $2}' | tr -d '\r' || true)
# ready / building / rate_limited are healthy; absent is fine
# on cache hits; not_found is a failure.
case "$STATUS" in
""|ready|building|rate_limited) echo "X-Widget-Status='${STATUS:-<absent>}' (ok)";;
*) echo "::error::Unhealthy X-Widget-Status='$STATUS'"; exit 1;;
esac
- name: Check generator health pipeline
id: check-api
run: |
# Generator's purpose-built liveness probe, routed through
# Cloudflare's /api/* rule.
URL="https://gh-stats.com/api/health"
echo "GET $URL"
BODY=$(curl -sS --max-time 30 -w "\n%{http_code}" "$URL")
HTTP_CODE=$(printf '%s\n' "$BODY" | tail -n1)
JSON=$(printf '%s\n' "$BODY" | sed '$d')
echo "HTTP $HTTP_CODE"
echo "$JSON"
if [ "$HTTP_CODE" != "200" ]; then
echo "::error::/api/health returned HTTP $HTTP_CODE"
exit 1
fi
STATUS=$(printf '%s' "$JSON" | python3 -c 'import json,sys; print(json.load(sys.stdin).get("status",""))' 2>/dev/null || echo "")
if [ "$STATUS" != "ok" ]; then
echo "::error::/api/health reports status='$STATUS' (expected 'ok')"
exit 1
fi
echo "generator pipeline healthy"
notify:
name: notify-on-failure
runs-on: ubuntu-latest
needs: [tests, site-health]
if: always() && (needs.tests.result == 'failure' || needs.site-health.result == 'failure')
env:
TESTS_RESULT: ${{ needs.tests.result }}
HEALTH_RESULT: ${{ needs.site-health.result }}
EVENT_NAME: ${{ github.event_name }}
REF_NAME: ${{ github.ref_name }}
COMMIT_SHA: ${{ github.sha }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
SERVER_URL: ${{ github.server_url }}
RUN_ID: ${{ github.run_id }}
steps:
- name: Download pytest logs
if: needs.tests.result == 'failure'
uses: actions/download-artifact@v4
with:
path: logs
pattern: pytest-*
continue-on-error: true
- name: Assemble email body
run: |
NOW=$(date -u '+%Y-%m-%d %H:%M:%S UTC')
RUN_URL="${SERVER_URL}/${REPO}/actions/runs/${RUN_ID}"
{
echo "gh-stats.com — health check failed"
echo "===================================="
echo ""
case "$EVENT_NAME" in
schedule) echo "Trigger: scheduled uptime probe";;
push) echo "Trigger: push to ${REF_NAME} (${COMMIT_SHA})";;
pull_request) echo "Trigger: PR #${PR_NUMBER} → main";;
workflow_dispatch) echo "Trigger: manual run";;
*) echo "Trigger: ${EVENT_NAME}";;
esac
echo "Time: $NOW"
echo "Repo: ${REPO}"
echo ""
echo "Job results"
echo "-----------"
printf " %-14s %s\n" "pytest:" "${TESTS_RESULT}"
printf " %-14s %s\n" "site-health:" "${HEALTH_RESULT}"
echo ""
if [ "$HEALTH_RESULT" = "failure" ]; then
echo "Site health"
echo "-----------"
echo " One or more endpoints on https://gh-stats.com are not"
echo " responding as expected. See the run logs for the failing"
echo " step and its HTTP code."
echo ""
fi
if [ "$TESTS_RESULT" = "failure" ] && [ -d logs ]; then
echo "Failing tests"
echo "-------------"
for dir in logs/pytest-*; do
[ -d "$dir" ] || continue
service=${dir#logs/pytest-}
log="$dir/pytest-${service}.log"
[ -f "$log" ] || continue
# Did this service actually fail? pytest prints FAILED lines
# per failure and a summary like "=== 3 failed ...". If
# neither appears, skip it.
if ! grep -qE '^(FAILED |=+ .* failed)' "$log"; then
continue
fi
echo ""
echo "[$service]"
grep -E '^(FAILED |=+ .* (failed|error))' "$log" | head -n 20
echo "..."
tail -n 60 "$log"
done
echo ""
fi
echo "Full logs"
echo "---------"
echo " $RUN_URL"
echo ""
echo "--"
echo "github-readme-stats automated monitor"
} > /tmp/email-body.txt
echo "----- email body -----"
cat /tmp/email-body.txt
echo "----------------------"
- name: Send email via ntfy
env:
NTFY_TOKEN: ${{ secrets.NTFY_ACCESS_TOKEN }}
NTFY_TOPIC: ${{ secrets.NTFY_TOPIC }}
NOTIFY_EMAIL: ${{ secrets.NOTIFY_EMAIL }}
run: |
missing=()
[ -z "$NTFY_TOKEN" ] && missing+=("NTFY_ACCESS_TOKEN")
[ -z "$NTFY_TOPIC" ] && missing+=("NTFY_TOPIC")
[ -z "$NOTIFY_EMAIL" ] && missing+=("NOTIFY_EMAIL")
if [ ${#missing[@]} -gt 0 ]; then
echo "::error::Missing required secret(s): ${missing[*]}"
echo "::error::Configure at: Settings → Secrets and variables → Actions"
exit 1
fi
if [ "$HEALTH_RESULT" = "failure" ] && [ "$TESTS_RESULT" = "failure" ]; then
SUBJECT="gh-stats.com is DOWN and tests are failing"
elif [ "$HEALTH_RESULT" = "failure" ]; then
SUBJECT="gh-stats.com is DOWN"
else
SUBJECT="gh-stats.com tests failed on ${REF_NAME}"
fi
RUN_URL="${SERVER_URL}/${REPO}/actions/runs/${RUN_ID}"
curl -fsS \
-H "Title: $SUBJECT" \
-H "Priority: high" \
-H "Tags: warning,rotating_light,gh-stats" \
-H "Email: $NOTIFY_EMAIL" \
-H "Click: $RUN_URL" \
-H "Authorization: Bearer $NTFY_TOKEN" \
--data-binary @/tmp/email-body.txt \
"https://ntfy.sh/$NTFY_TOPIC"