|
| 1 | +name: 'Service Smoke Check' |
| 2 | + |
| 3 | +# Polls a JSON /health endpoint until it returns an accepted status (and optional |
| 4 | +# version match). `soak-passes` controls how many CONSECUTIVE good probes are |
| 5 | +# required: 1 = first-success smoke after a deploy; N = an N-probe soak window for |
| 6 | +# a canary gate. Fails the job if the target never satisfies the criteria. |
| 7 | + |
| 8 | +on: |
| 9 | + workflow_call: |
| 10 | + inputs: |
| 11 | + url: |
| 12 | + description: 'Base URL of the service (e.g. https://api.creativetoolkits.com)' |
| 13 | + required: true |
| 14 | + type: string |
| 15 | + path: |
| 16 | + description: 'Health endpoint path' |
| 17 | + required: false |
| 18 | + type: string |
| 19 | + default: '/health' |
| 20 | + expected-version: |
| 21 | + description: 'Require payload.version to equal this value (optional)' |
| 22 | + required: false |
| 23 | + type: string |
| 24 | + default: '' |
| 25 | + accept-status: |
| 26 | + description: 'Comma-separated payload.status values treated as healthy' |
| 27 | + required: false |
| 28 | + type: string |
| 29 | + default: 'ok,degraded' |
| 30 | + expected-http: |
| 31 | + description: 'Required HTTP status code' |
| 32 | + required: false |
| 33 | + type: number |
| 34 | + default: 200 |
| 35 | + retries: |
| 36 | + description: 'Maximum number of probe attempts' |
| 37 | + required: false |
| 38 | + type: number |
| 39 | + default: 20 |
| 40 | + interval-seconds: |
| 41 | + description: 'Seconds between probes' |
| 42 | + required: false |
| 43 | + type: number |
| 44 | + default: 15 |
| 45 | + soak-passes: |
| 46 | + description: 'Consecutive successful probes required (1 = smoke, N = soak)' |
| 47 | + required: false |
| 48 | + type: number |
| 49 | + default: 1 |
| 50 | + runs-on: |
| 51 | + description: 'Runner label to execute the job on' |
| 52 | + required: false |
| 53 | + type: string |
| 54 | + default: 'ubuntu-latest' |
| 55 | + outputs: |
| 56 | + ok: |
| 57 | + description: 'true when the smoke check passed' |
| 58 | + value: ${{ jobs.smoke.outputs.ok }} |
| 59 | + version: |
| 60 | + description: 'Last observed payload.version' |
| 61 | + value: ${{ jobs.smoke.outputs.version }} |
| 62 | + |
| 63 | +permissions: |
| 64 | + contents: read |
| 65 | + |
| 66 | +jobs: |
| 67 | + smoke: |
| 68 | + name: Service Smoke Check |
| 69 | + runs-on: ${{ inputs.runs-on }} |
| 70 | + timeout-minutes: 20 |
| 71 | + outputs: |
| 72 | + ok: ${{ steps.poll.outputs.ok }} |
| 73 | + version: ${{ steps.poll.outputs.version }} |
| 74 | + steps: |
| 75 | + - name: Poll health endpoint |
| 76 | + id: poll |
| 77 | + env: |
| 78 | + BASE_URL: ${{ inputs.url }} |
| 79 | + HEALTH_PATH: ${{ inputs.path }} |
| 80 | + EXPECTED_VERSION: ${{ inputs.expected-version }} |
| 81 | + ACCEPT_STATUS: ${{ inputs.accept-status }} |
| 82 | + EXPECTED_HTTP: ${{ inputs.expected-http }} |
| 83 | + RETRIES: ${{ inputs.retries }} |
| 84 | + INTERVAL: ${{ inputs.interval-seconds }} |
| 85 | + SOAK_PASSES: ${{ inputs.soak-passes }} |
| 86 | + run: | |
| 87 | + python3 - <<'PY' |
| 88 | + import json, os, sys, time, urllib.error, urllib.parse, urllib.request |
| 89 | +
|
| 90 | + base = os.environ["BASE_URL"].rstrip("/") |
| 91 | + path = os.environ["HEALTH_PATH"] |
| 92 | + url = base + (path if path.startswith("/") else "/" + path) |
| 93 | + expected_version = os.environ.get("EXPECTED_VERSION", "") |
| 94 | + accept = [s.strip() for s in os.environ.get("ACCEPT_STATUS", "ok,degraded").split(",") if s.strip()] |
| 95 | + expected_http = int(os.environ.get("EXPECTED_HTTP", "200")) |
| 96 | + retries = int(os.environ.get("RETRIES", "20")) |
| 97 | + interval = int(os.environ.get("INTERVAL", "15")) |
| 98 | + soak = int(os.environ.get("SOAK_PASSES", "1")) |
| 99 | +
|
| 100 | + def probe(): |
| 101 | + sep = "&" if urllib.parse.urlsplit(url).query else "?" |
| 102 | + probe_url = f"{url}{sep}ci_probe={time.time_ns()}" |
| 103 | + req = urllib.request.Request(probe_url, headers={"Cache-Control": "no-cache"}) |
| 104 | + try: |
| 105 | + with urllib.request.urlopen(req, timeout=10) as resp: |
| 106 | + code, body = resp.status, resp.read().decode("utf-8") |
| 107 | + except (OSError, urllib.error.URLError) as err: |
| 108 | + return False, f"request failed: {err}", "" |
| 109 | + if code != expected_http: |
| 110 | + return False, f"HTTP {code}: {body[:300]}", "" |
| 111 | + try: |
| 112 | + payload = json.loads(body) |
| 113 | + except json.JSONDecodeError as err: |
| 114 | + return False, f"invalid JSON: {err}", "" |
| 115 | + status = payload.get("status") |
| 116 | + version = payload.get("version") or "" |
| 117 | + if accept and status not in accept: |
| 118 | + return False, f"status={status!r} not in {accept}", version |
| 119 | + if expected_version and version != expected_version: |
| 120 | + return False, f"version={version!r} != {expected_version!r}", version |
| 121 | + return True, f"status={status}, version={version}", version |
| 122 | +
|
| 123 | + def write_output(key, value): |
| 124 | + with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as handle: |
| 125 | + handle.write(f"{key}={value}\n") |
| 126 | +
|
| 127 | + passes = 0 |
| 128 | + last_version = "" |
| 129 | + for attempt in range(1, retries + 1): |
| 130 | + ok, message, version = probe() |
| 131 | + last_version = version or last_version |
| 132 | + print(f"attempt {attempt}/{retries}: {'OK' if ok else 'FAIL'} - {message}") |
| 133 | + if ok: |
| 134 | + passes += 1 |
| 135 | + if passes >= soak: |
| 136 | + write_output("ok", "true") |
| 137 | + write_output("version", last_version) |
| 138 | + print(f"smoke passed ({passes}/{soak} consecutive)") |
| 139 | + sys.exit(0) |
| 140 | + else: |
| 141 | + passes = 0 |
| 142 | + if attempt < retries: |
| 143 | + time.sleep(interval) |
| 144 | +
|
| 145 | + write_output("ok", "false") |
| 146 | + write_output("version", last_version) |
| 147 | + print("::error::smoke check did not pass within the retry budget") |
| 148 | + sys.exit(1) |
| 149 | + PY |
| 150 | +
|
| 151 | + - name: Summary |
| 152 | + if: always() |
| 153 | + run: | |
| 154 | + { |
| 155 | + echo "## 🩺 Service Smoke Check" |
| 156 | + echo "" |
| 157 | + echo "**URL:** \`${{ inputs.url }}${{ inputs.path }}\`" |
| 158 | + echo "**Soak passes:** \`${{ inputs.soak-passes }}\`" |
| 159 | + echo "**OK:** \`${{ steps.poll.outputs.ok }}\`" |
| 160 | + echo "**Version:** \`${{ steps.poll.outputs.version }}\`" |
| 161 | + } >> "$GITHUB_STEP_SUMMARY" |
0 commit comments