This tutorial shows a practical, testable pattern to avoid flaky email API checks in GitHub Actions by freezing an “inbox contract” at the start of each run, using that contract for all subsequent steps, and uploading a compact final verdict as an artifact. The goal is consistent evidence so reruns and handoffs are straightforward.
- Write a single inbox contract JSON at the start of each workflow run and have every step read it.
- Persist provider responses (inbox metadata) to artifacts so later steps use canonical identifiers.
- Produce one compact verdict.json at the end describing pass/fail, why, and timing; upload it as an artifact for reruns and handoffs.
Why an inbox contract helps
Email checks commonly break not because sending failed, but because the workflow around the inbox is fuzzy: generated addresses aren’t saved, different steps assume different address formats, retries are invisible, or the run context is missing. Freezing a single JSON contract at the start ensures every step (inbox creation, trigger, poll, assert) reads the same source of truth. Uploading a small verdict artifact at the end makes failures rapidly comparable across runs.
Outcome and prerequisites
After following this tutorial you will have a GitHub Actions pattern that:
- Creates a small inbox contract file at the start of a run.
- Keeps generated inbox metadata isolated in artifacts for reproducibility.
- Produces a compact verdict.json artifact describing pass/fail and context.
Prerequisites
- An existing repository with GitHub Actions enabled.
- Access to an email API or test inbox provider reachable from Actions runners.
- Basic shell scripts or small programs to create an inbox, trigger the email-sending action, poll the inbox, and assert message contents.
Design decisions and trade-offs
Keep the contract tiny and explicit to minimize accidental drift and make diffs useful. Trade-offs:
- Compact contract vs capturing full context: store only the fields you need (run id, scenario, inbox label/identifier, expected subject, max wait). Store full message bodies only when you are testing email content.
- Explicit retries vs hidden resilience: prefer a small set of explicit attempts surfaced in the verdict to avoid unpredictable hidden retries.
- Artifact retention vs storage costs: upload minimal artifacts (JSON + short log snippet) and rely on configurable retention-days if you need temporary persistence; GitHub supports per-artifact retention-days when uploading artifacts.
Step-by-step implementation
- Decide contract fields. Minimal recommended schema (JSON):
{ "run_id": "gha-1842", "scenario": "signup-verification", "inbox_label": "signup-verification-1842", "expected_subject": "Confirm your account", "max_wait_seconds": 45 }Adjust fields to match your provider (for example, include “inbox_id” or “inbox_address” if required).
- Create a run directory for artifacts early in the workflow. Example shell snippet used by subsequent steps:
RUN_DIR="artifacts/${GITHUB_RUN_ID}" mkdir -p "$RUN_DIR"This keeps every run’s artifacts isolated.
- Write the contract file at workflow start. Implement as a short script or inline step that emits JSON to artifacts/inbox-contract.json. Example (conceptual):
./scripts/make-contract.sh > "$RUN_DIR/inbox-contract.json"
The script should include run-level metadata like GITHUB_RUN_ID and the scenario name. Ensure all later steps read this file instead of relying on ephemeral environment variables.
- Create the inbox using the contract. Keep the creation command dumb and deterministic: it reads the contract and writes provider response to artifacts/inbox.json.
./scripts/create-inbox.sh --contract "$RUN_DIR/inbox-contract.json" > "$RUN_DIR/inbox.json"
Do not let the create step write unrelated state into logs; capture the provider response so later steps can use exact inbox identifiers.
- Trigger the action that sends the email using the contract and the inbox metadata.
./scripts/trigger-signup.sh --contract "$RUN_DIR/inbox-contract.json" --inbox "$RUN_DIR/inbox.json"
Using the contract in the trigger step ensures the expected subject and recipient align exactly with assertions later.
- Poll and assert. The polling step should load both contract and inbox.json, wait up to max_wait_seconds, and produce a structured verdict file (verdict.json) describing the outcome.
./scripts/assert-message.sh --contract "$RUN_DIR/inbox-contract.json" --inbox "$RUN_DIR/inbox.json" > "$RUN_DIR/verdict.json"
Recommended verdict fields: outcome (pass|fail), reason (e.g., “timed out after 45s waiting for Confirm your account”), attempts, timestamps, found_message_meta (subject, to, host), and a short log_excerpt. Keep the verdict succinct — full bodies only when needed.
- Upload artifacts. Use actions/upload-artifact to save the contract, inbox.json, and verdict.json. Use a small retention-days if runs are ephemeral. Example conceptual inputs when using the upload-artifact action: name: inbox-contract path: “$RUN_DIR/inbox-contract.json” retention-days: 5
Concrete GitHub Actions workflow sketch
Do not copy exact action versions unless you consult official docs. The workflow flow is the important part: create RUN_DIR ➜ write contract ➜ create inbox ➜ trigger email ➜ assert message ➜ upload artifacts.
Validation and edge cases
Validation checks each script should perform:
- Contract JSON schema validation before use. Fail fast if required keys are missing.
- Inbox creation step should verify the provider returned an inbox identifier and persist it in inbox.json.
- Polling step must respect max_wait_seconds and report elapsed time in the verdict.
- Assert step should normalize address formats if your system accepts multiple formats, and explicitly surface mismatches.
Common failures and troubleshooting
- Message not found: verdict should state whether it timed out and include the expected subject, attempts made, and the max_wait_seconds. This removes guesswork about how long the runner waited.
- Mismatched inbox identifier formats: preserve the provider’s returned inbox identifier in inbox.json and reference it in the verdict. If different steps assumed different formats, the mismatch becomes visible immediately.
- Invisible retries causing flakiness: log each attempt in verdict attempts[] and avoid hidden retry logic. If retry helpers are used, make their behavior visible in the verdict.
- Too much data in artifacts: keep artifacts minimal — a compact contract and a verdict plus a short log excerpt are usually sufficient. Store full message bodies only when needed for content assertions.
Practical use cases and when to store full message bodies
Store only metadata (subject, recipient, timestamp, verification link host) unless the test asserts content or rendering. Examples:
- Signup verification flows: subject, recipient, link host, and timing are usually enough.
- Password reset emails under visual/content test: store full bodies or snippets for assertions.
- Provider contract tests: when verifying headers or raw message format, preserve the full MIME payload.
Troubleshooting checklist
- Open the run’s artifact folder. The contract and verdict should be present and explain intent and outcome.
- If verdict says timed out, check inbox.json for the actual inbox identifier and any provider error hints preserved there.
- Compare contract vs inbox.json for address format mismatches (for example, “label” vs “email_address”).
- Inspect the short log excerpt from the verdict to see API responses; avoid dumping full logs into artifacts.
FAQ
Should the workflow store the full email body?
Only when the body is under test. For most API checks, subject, recipient, timing, and a single verification link host are enough.
How many retries are reasonable?
Prefer a small, explicit number (e.g., 3 attempts) surfaced in the verdict rather than unlimited or hidden retries. Make retry counts and intervals visible.
How long should artifacts be retained?
Use short retention for routine runs (GitHub Actions supports per-artifact retention-days). Increase retention only for runs that require postmortem investigation.
Summary and next steps
Freeze an inbox contract at the beginning of each run, persist provider responses, and write a compact verdict at the end. Upload these three small artifacts (contract, inbox.json, verdict.json) for reproducible runs and clear handoffs. This pattern reduces guesswork, surfaces contract drift quickly, and makes CI evidence easy to diff and compare across reruns.
Suggested next steps: implement the small scripts referenced here with clear JSON schema validation; add a contract linter to your pipeline; and configure per-artifact retention-days when uploading artifacts.
