Email verification tests become unreliable when a Docker-based CI job treats a test mailbox as a global service. A retry can read the previous run's message, two branches can claim the same address, and a failed runner can leave data behind.

The pattern that has worked for me is small: make every fixture run-scoped, keep its credentials outside the image, and give cleanup two owners. The test container owns the normal path. A short-lived AWS cleanup task owns the recovery path when the runner disappears.

Why Docker email tests leak across CI runs

A Docker image should contain the test code and its dependencies. It should not contain a mailbox address, API token, or a default inbox that every parallel job uses. Those values create hidden state in what should be a repeatable job.

The usual symptoms are familiar:

  • A test passes alone but reads a stale verification message in the full pipeline.
  • A retry creates a second fixture because the first request was not idempotent.
  • A cancelled job leaves a mailbox that later tests can still discover.
  • CI logs contain a token or message body because debugging was added in a hurry.

This is also why I prefer correlating deploy emails with the right run. The run identifier should travel with the fixture request, the application logs, and the final test artifact. It makes a wrong recipient easier to spot than a generic "message not found" error.

Create a run-scoped fixture contract

Give the fixture service an explicit contract instead of making the test guess which inbox is current. At minimum, include an owner, purpose, expiration, and correlation token:

{ "run_id": "github-18402-attempt-2", "purpose": "email-verification", "expires_at": "2026-12-03T13:00:00Z", "correlation_id": "checkout-7f21" } 

The fixture service should return a lease ID and address. Every read should present the lease ID or correlation ID, not only a subject line. Subject-only matching are a race waiting to happen when two tests send similar messages.

In the test container, pass the values as runtime environment variables or a mounted short-lived file. Do not bake them into Dockerfile layers. Docker history can preserve values that were deleted in a later layer, which makes a cleanup commit less useful than people expect.

If the application consumes email events through an API, keep the event version explicit. Versioning email events in a Node API is a useful companion pattern because a retry should not silently change the meaning of an event that the test already saw.

Keep secrets out of images and logs

Use AWS Secrets Manager or an equivalent secret store for the fixture service credential. The CI role should be allowed to read one secret path, and the test job should receive only the value it needs. A broad read permission is convenient at first, but it makes a test failure into a much larger incident.

The container entrypoint can fetch configuration before starting the test process:

set -euo pipefail export FIXTURE_API_URL="https://mail-fixtures.internal" export FIXTURE_TOKEN="$(aws secretsmanager get-secret-value \ --secret-id ci/email-fixtures/test-run \ --query SecretString --output text)" exec ./run-email-tests 

Redact the token from shell tracing and test output. Log the secret ARN, run ID, lease ID, and timestamps, but never the token or full message body. The dashboard dont need every field to be useful; a small receipt is easier to retain and search.

Validate generated addresses before they reach the application. A value such as fake e mail com can be a deliberate invalid-input case, but it should not accidentally become a real fixture identifier. The validation rule are part of the test contract, not a last-minute string check in one browser test.

Clean up from the runner and the service

The normal CI step should release the lease in an always or equivalent block, even when the test command fails:

cleanup() { if [ -n "${LEASE_ID:-}" ]; then curl --fail-with-body -X DELETE \ -H "Authorization: Bearer ${FIXTURE_TOKEN}" \ "${FIXTURE_API_URL}/leases/${LEASE_ID}" || true fi } trap cleanup EXIT 

That is not enough by itself. Hosted runners can be terminated before the trap runs. The fixture service should enforce expires_at and have a scheduled AWS task that deletes expired leases. Record the deletion result separately from test status, because a green test with failed cleanup is still an operational defect.

When a lease expires, retain a small failure receipt: run ID, lease ID, created time, expiry time, and deletion outcome. Keep message content and tokens out of the receipt. This gives enough evidence to debug without turning CI artifacts into a second mailbox.

A practical AWS and Docker checklist

Before merging an email-driven CI workflow, check the following:

  • Does every parallel job receive a unique run ID and lease?
  • Are fixture credentials injected at runtime rather than stored in the image?
  • Can the service reject reads from the wrong lease or correlation ID?
  • Are retries idempotent, with a bounded timeout and an explicit failure state?
  • Does cleanup run on the normal path and through an expiry controller?
  • Do logs include timing and ownership data while redacting tokens and bodies?
  • Can an engineer find the exact Docker image digest used by a failed run?

The last point matters more than it looks. CI debugging gets slow when the image, fixture, and deploy evidence are recorded in different systems. A run-scoped contract gives each layer the same identity. It makes throwaway email fixtures disposable in the right sense: short-lived, isolated, and explainable when they fail.