diff --git a/.github/workflows/health-check.yml b/.github/workflows/health-check.yml new file mode 100644 index 0000000..d32394c --- /dev/null +++ b/.github/workflows/health-check.yml @@ -0,0 +1,234 @@ +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: | + echo "HEAD https://gh-stats.com/ShayManor" + HEADERS=$(curl -sI --max-time 30 https://gh-stats.com/ShayManor) + echo "$HEADERS" + HTTP_CODE=$(printf '%s\n' "$HEADERS" | head -n1 | awk '{print $2}' | tr -d '\r') + STATUS=$(printf '%s\n' "$HEADERS" | grep -i '^x-widget-status:' | awk '{print $2}' | tr -d '\r' || true) + if [ "$HTTP_CODE" != "200" ]; then + echo "::error::Widget endpoint returned HTTP $HTTP_CODE" + exit 1 + fi + # ready | building | rate_limited are healthy; not_found / missing are not. + case "$STATUS" in + ready|building|rate_limited) echo "X-Widget-Status=$STATUS (ok)";; + *) echo "::error::Unexpected X-Widget-Status='$STATUS'"; exit 1;; + esac + + - name: Check generator API reachable via edge + id: check-api + run: | + # /api/settings/ is a lightweight read the edge proxies to + # the generator. A 200 or 404 both prove the pipeline is alive; + # 5xx / timeout means something is wrong upstream. + HTTP_CODE=$(curl -sS -o /dev/null -w "%{http_code}" --max-time 30 https://gh-stats.com/api/settings/ShayManor) + echo "HTTP $HTTP_CODE" + if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "404" ]; then + echo "API reachable (HTTP $HTTP_CODE)" + else + echo "::error::API returned HTTP $HTTP_CODE" + exit 1 + fi + + 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"