API Smoke Tests Need Receipts — a practical GitHub Actions pattern

This tutorial shows how to make API smoke tests produce compact “receipts” that explain what was called, what came back, how long it took, and why the job passed, warned, or failed. The pattern uses GitHub Actions workflow commands to create a human-sized summary while saving raw request and response artifacts for deeper debugging.

Key takeaways

  • Produce a compact JSON receipt for each smoke test run (status, http_status, latency_ms, reason, run_id).
  • Keep the human-facing summary small and push raw bodies to artifacts for debugging.
  • Separate evidence collection from summarization to make reruns and diffs simple.
  • Mask secrets before printing; keep warning budgets distinct from failure budgets.

Why receipts for smoke tests

Simple smoke tests often return a single red mark with no clear context: did the call return the wrong HTTP status, an unexpected body, or just slow down? Adding a small JSON receipt per test turn explosive, guesswork failures into a single coherent story that can be triaged in minutes. The receipt should be compact, stable across runs, and easy to diff.

Prerequisites

  • Repository with GitHub Actions enabled.
  • Ability to store a secret API token (GitHub Secrets) if the API requires authentication.
  • jq and curl (or equivalent tooling) available on the runner. The examples use bash on ubuntu-latest.
  • A small script or Node program that transforms raw outputs into a structured receipt. The tutorial shows a minimal example where such a script is invoked; the receipt shape is documented below.

Outcome

After following these steps you will have a workflow pattern that:

  • Produces artifacts: raw response body and status file.
  • Writes a compact receipt.json containing verdict, http_status, schema_ok, latency_ms, reason and run_id.
  • Creates a short, human-readable step summary using GitHub Actions step-summary and workflow commands for warnings/annotations.
  • Makes reruns and diffing simple by keeping raw details archived separately.

Create a compact JSON object representing the test judgement. The example below is intentionally small; include fields that clarify diagnosis without dumping full bodies into the summary:

{
  "check": "billing-health",
  "status": "warn",        // one of: ok, warn, fail
  "http_status": 200,
  "schema_ok": true,
  "latency_ms": 1842,
  "reason": "response passed but latency crossed warning budget",
  "run_id": "smoke-4821"
}

High-level implementation workflow

  1. Run the API call and capture raw artifacts: response body and HTTP status (or curl exit code).
  2. Run a small converter script that reads the artifacts and emits the compact receipt.json.
  3. Summarize the receipt into the GitHub Actions step summary and emit any warnings/annotations using workflow commands.
  4. Archive raw artifacts for later download and attach them to the run as job artifacts.

Step-by-step example (bash + jq + Node script)

The following numbered steps map directly to job steps in GitHub Actions. Replace API URL and authentication variables with your service values. This workflow follows the pattern: evidence collection → receipt generation → human summary.

  1. Create artifacts folder and call the API. Capture response body to a file and HTTP status to a status file.

    mkdir -p artifacts
    curl -sS \
      -H "Authorization: Bearer $API_TOKEN" \
      -o artifacts/response.json \
      -w "%{http_code}" \
      https://api.example.com/health > artifacts/status.txt
  2. Generate a receipt from the raw files. The receipt-generator can be any small script that:

    • reads artifacts/status.txt
    • reads artifacts/response.json
    • measures latency (if available from curl -w or a timing wrapper)
    • performs quick schema checks or field assertions
    • writes artifacts/receipt.json with the recommended shape

    Example invocation (assumes a Node script scripts/write-receipt.mjs):

    node scripts/write-receipt.mjs \
      --status-file artifacts/status.txt \
      --response-file artifacts/response.json \
      --out artifacts/receipt.json

    The receipt generator is intentionally left implementation-specific — it should be small, stable and deterministic so diffs between runs are useful.

  3. Summarize the receipt into a human-facing step. Read status, reason and latency from receipt.json and emit a warning annotation when appropriate. Also append a compact markdown summary into the GitHub Actions step summary. Use workflow commands to add a warning annotation so the runner surfaces it in the UI.

    status="$(jq -r '.status' artifacts/receipt.json)"
    reason="$(jq -r '.reason' artifacts/receipt.json)"
    latency="$(jq -r '.latency_ms' artifacts/receipt.json)"
    
    if [ "$status" != "ok" ]; then
      echo "::warning::${reason}"
    fi
    
    {
      echo "## API smoke test"
      echo ""
      echo "- Status: \`$status\`"
      echo "- Latency: ${latency}ms"
      echo "- Reason: $reason"
      echo "- Files: response.json, receipt.json"
    } >> "$GITHUB_STEP_SUMMARY"

    This produces a concise summary in the Actions UI while keeping long or sensitive bodies in artifacts.

  4. Upload artifacts if you want to keep the raw response and the receipt for later inspection. Use the actions/upload-artifact action to attach artifacts to the run. This keeps the main log concise while making full evidence downloadable.

Troubleshooting and common mistakes

  • Mask secrets before printing them: never print API tokens in the logs. Use GitHub’s add-mask workflow command (echo “::add-mask::$VALUE”) if the value must appear anywhere in a job’s log. Treat masked values as secrets—once masked, outputs containing them are redacted.
  • Don’t dump raw body into the summary: keep the step summary human-sized; store raw JSON in artifacts/response.json for debugging.
  • Separate warnings from failures: define clear budgets (latency, error rate, schema tolerances) and map them to either warn or fail in the receipt. Mixing both makes dashboards misleading.
  • Keep the receipt small and stable: adding noisy fields like precise timestamps in multiple formats makes diffs noisy. Prefer a run_id that changes per run and a minimal set of diagnostic fields.
  • Make reruns easy: keep the evidence collection step separate from the summarizer so a developer can rerun just the summary step locally against archived artifacts.

Edge cases and trade-offs

Trade-offs to consider when adopting receipts:

  • Frequency vs. detail: more frequent smoke tests with tiny receipts give faster feedback but may increase storage for artifacts. Only archive raw bodies for checks that can fail for multiple reasons.
  • Security vs. evidence: masking secrets is essential. If a raw response contains PII, do not archive it; instead extract non-sensitive indicators into the receipt.
  • False positives from schema checks: keep schema assertions targeted to critical fields; overly strict schemas cause brittle failures that drown out real incidents.

Validation checklist before rolling out

  1. Receipt contains: check name, status (ok/warn/fail), http_status, latency_ms, reason, run_id.
  2. Step summary shows a short verdict and points to artifacts without including full bodies.
  3. Secrets are masked before any printing in the job.
  4. Artifacts are uploaded only when useful; retention policy is enforced for storage control.
  5. Test the workflow by intentionally producing warn and fail verdicts to confirm annotations and summaries appear as expected.

Practical use cases

  • API health endpoints that may degrade in latency before failing: warn on latency, fail on schema mismatch.
  • Flows with side effects (email invites, password resets): treat mailbox checks as sub-checks and include polling attempts and matched subject lines in the receipt.
  • Type-safety or validation checks where the raw output is large: keep the receipt with pass/fail and a short reason; archive the full output for debugging.

FAQ

Should every smoke test write a JSON artifact?

No. Prefer receipts for checks that can fail for multiple reasons or where the diagnosis path has branches. Very trivial checks that only can be “up/down” may not need them.

Can I include retry attempts in one receipt?

Yes. If a test performs retries or spans multiple sub-checks, stitch those attempts into one coherent receipt and keep a single run_id so evidence is easy to correlate.

Is it ok to put the full response in the step summary?

No. Keep the summary human-sized. Save full bodies in artifacts and reference them from the summary.

Conclusion

Adding small, consistent receipts transforms smoke tests from opaque failures into triageable evidence. The pattern requires only minor workflow changes: collect raw outputs, run a tiny receipt generator, show a short human summary, and archive the rest. Teams using this approach report faster reruns and much quicker incident triage for API-related checks.

Stay in Touch

spot_img

Related Articles