Email verification tests are often the first thing to become unreliable when a CI pipeline starts running in parallel. One build reads another build's message, a retry finds an already-consumed token, or cleanup removes the inbox before the failure can be investigated.
I have had better results by treating an email fixture like a short-lived infrastructure resource. Each build gets an identity, a bounded lifetime, and an artifact trail. The pattern works in Docker locally and in AWS-hosted CI, without turning the test suite into a shared mailbox scavenger hunt.
Why shared inboxes make CI look random
A shared inbox creates hidden coupling between jobs. The test code may ask for the newest verification email, but "newest" is not the same as "mine" when four pull requests are active. Timing then becomes the accidental coordination system.
The failures is usually reported as a flaky test, even though the underlying problem is ownership. A build needs to know which inbox it created, which messages belong to that inbox, and whether the message was consumed already. A subject prefix helps, but it is not a complete boundary by itself. The real fix are ownership rules, not another sleep statement.
I also avoid putting real user data into these fixtures. If a team uses a disposable mail address for a controlled test account, the address should be scoped to the build and never reused for production workflows.
Give every build an isolated fixture boundary
Start with a build identifier that is safe for logs and URLs:
export FIXTURE_ID="${CI_COMMIT_SHA:-local}-$$" export FIXTURE_PREFIX="ci-${FIXTURE_ID:0:16}" Store the returned address and message identifier in the job workspace, not in a global environment variable shared by later jobs. A job should be able to answer three questions from its own files:
- Which inbox did this build lease?
- Which message or token did the test accept?
- When should the fixture be deleted?
This is an execution boundary, not just a naming convention. The API client should reject a message whose fixture ID does not match the current build. In practice, that check caught more cross-job contamination than increasing polling delays. It is easy to loose the lease context when a helper hides the request details, so keep it visible in the test output.
For a deeper look at the other side of this problem, I keep deletion proof for temporary inbox tests in the same review checklist. Creation and cleanup are one lifecycle, so they should be observed together.
Run the fixture beside Docker tests
In local development, put the fixture adapter and the application under test on a private Docker network. The test container receives only the adapter URL and its fixture ID:
services: app: build: . environment: EMAIL_FIXTURE_URL: http://mail-fixture:8080 CI_FIXTURE_ID: local-compose depends_on: mail-fixture: condition: service_healthy mail-fixture: image: example/mail-fixture:stable healthcheck: test: ["CMD", "wget", "-q", "-O", "/health", "http://localhost:8080/health"] interval: 2s timeout: 2s retries: 10 Do not use depends_on as proof that an email is available. It only controls startup order. The test still needs a bounded poll with a correlation ID, and it should report the last observed message IDs when the poll expires. Otherwise the logs say only "timeout", which is not enough to fix the issue.
The same boundary works in CI. Each parallel job starts its own adapter or requests a unique namespace from a shared adapter. The seperate choice depends on cost and throughput: isolated processes are easier to reason about, while a shared service needs strict tenant filtering and quotas. The logs should be clear enought that an on-call can follow one build without opening every job.
Keep AWS artifacts useful after failure
When this runs on AWS, retain a small diagnostic bundle on failure. It can include the fixture lease, sanitized message metadata, the test report, Docker logs, and the correlation ID. Avoid storing message bodies if they can contain tokens or personal data.
For an ephemeral runner, upload the bundle before the cleanup trap runs. A runner can dissapear quickly after a failed step, so the trap must do useful work in a small window:
cleanup() { status=$? if [ "$status" -ne 0 ]; then tar -czf "artifacts/${FIXTURE_ID}.tgz" \ "test-results" "docker-logs" "fixture.json" 2>/dev/null || true aws s3 cp "artifacts/${FIXTURE_ID}.tgz" \ "s3://$CI_ARTIFACT_BUCKET/email-fixtures/${FIXTURE_ID}.tgz" fi ./scripts/release-fixture.sh "$FIXTURE_ID" || true exit "$status" } trap cleanup EXIT The bucket needs a short retention policy and restricted access. Do not put tokens in object names, and do not assume that a deleted local file means the uploaded artifact is gone. This is where teams often get a bit surprised: observability can quietly become data retention. Keep the cleanup path simple and repeatable, even if it feels slightly over careful.
A typed client in the test harness also helps keep the states explicit. A typed decision boundary in the test client is a useful model for separating pending, received, expired, and rejected states. The exact language is less important than making an expired lease impossible to treat as a valid message.
Questions worth answering before rollout
How long should a fixture live?
Long enough for the slowest supported CI region and retry path, with a hard upper limit. Pick the value from observed pipeline timings, then add a margin. Never let an abandoned job keep an inbox forever.
Should retries reuse the same inbox?
Usually yes within one test attempt, but give each retry a new correlation ID. Reusing a message token across attempts can hide a race and makes the test result harder to replay.
What belongs in logs?
Log IDs, timestamps, state transitions, and counts. Redact addresses and message contents unless a controlled debugging mode explicitly requires them. A temp mailid in a log can still be sensitive if it is live.
A practical pipeline checklist
- Create a fixture lease per build or per isolated test namespace.
- Put the fixture ID on every request and reject mismatches.
- Poll by correlation ID with a deadline, not by newest message.
- Save sanitized metadata and Docker logs before cleanup.
- Release the fixture from an exit trap, including failed jobs.
- Apply retention and access controls to AWS artifacts.
- Test parallel jobs and an interrupted job, not only the happy path.
This approach adds a little setup, but it removes a large amount of mystery. CI becomes a sequence of owned resources: create, verify, observe, and release. That is calmer than asking a shared inbox what happened after the pipeline is already red.