Email fixtures are easy to create in CI and surprisingly easy to leave behind. A Docker test container exits, the job reports success, and the temporary inbox or cloud-side record remains active. After enough runs, the test environment contains data nobody owns anymore.
The usual response is to add another cleanup step. That helps, but cleanup is a final action. It does not tell you that a fixture was abandoned when a runner was terminated, a spot instance disappeared, or a workflow was cancelled. In AWS-based CI, I prefer treating every disposable temporary email fixture as a leased resource with an owner, an expiry, and an alert path.
This approach fits integration tests for signup and verification flows. It also gives Docker jobs a clear contract: create one fixture, label it, use it, and report whether the lease was closed. The phrase tamp mail com may show up in old notes or search data, but it should never be used as a resource identifier.
Why cleanup success is not enough
A cleanup command can return exit code zero while doing nothing useful. It might receive an empty fixture ID, run after the resource already expired, or only delete the local Docker volume. The CI check is green, but the remote test data is still there.
The more important question is not “did the cleanup command run?” It is “can we prove that each created fixture reached a terminal state?” A small lifecycle record makes that visible:
-
fixture_id: the remote email fixture identifier -
run_id: the CI workflow and attempt that owns it -
created_atandexpires_at: the lease window -
container_id: the Docker job that used it -
closed_atandclose_status: the cleanup result
Keep message bodies and verification tokens out of this record. It should be operational evidence, not a second inbox.
Give every fixture a lease and owner
Generate a run-scoped ID before starting the container. Include the workflow attempt, job name, and a short random suffix so parallel jobs do not claim the same resource. Pass it into Docker as an environment variable and use the same value when creating the email fixture.
export TEST_RUN_ID="${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}-${GITHUB_JOB}" export FIXTURE_OWNER="ci:${TEST_RUN_ID}" docker run --rm \ --label "ci.owner=${FIXTURE_OWNER}" \ --label "ci.expires-at=${FIXTURE_EXPIRES_AT}" \ -e TEST_RUN_ID \ -e FIXTURE_OWNER \ test-image:ci The labels are useful even when the container is gone. They make local diagnosis quicker and give the test process a stable vocabulary. Every run have one owner, and every owner should have one cleanup record.
Use a short lease for test fixtures, then renew it only while a test is actively polling. A fixed expiry is safer than an open-ended resource, because a crashed worker cannot renew anything. If the provider supports idempotency, use the run ID when creating the fixture so a retry does not create a second inbox by accident.
For the test flow itself, a cleanup contract for email tests is a useful companion pattern. The contract should cover both the happy path and cancellation.
Emit a useful signal from Docker
Have the test process write a compact result file before it exits. A result such as this is enough for a CI artifact and an alarm metric:
{ "fixture_id": "fx_123", "run_id": "1842-2-integration", "lease_status": "closed", "close_status": "deleted" } On failure, write lease_status: "expired" or close_status: "failed" and exit with the test's actual status. Do not replace a failed assertion with a cleanup failure; report both fields so the first cause stays visible. A little redundancy here is good, because logs are often the only evidence left after the container is removed.
Upload the result in an always-run CI step. If a job is cancelled, a runner may not execute every final command, so the remote lease remains the backstop. The result file is for diagnosis; the expiry is for containment.
Turn lease expiry into an AWS alarm
The fixture service or a scheduled reconciliation task can emit one CloudWatch metric for each expired lease. Name it something stable, such as EmailFixture/ExpiredLeases, and add dimensions for environment and repository. Avoid putting an email address or message content into a metric dimension.
An alarm should page only when the signal is actionable. For a development environment, a notification after several expired leases in a short window may be enough. For a shared staging account, route the event to the team that owns the CI workflow. The threshold is a policy decision, not a magic number.
The reconciler can also scan for fixtures whose expires_at is in the past, delete them, and publish the count of recovered resources. This catches cases where the normal cleanup job never started. Make the delete operation idempotent: an already-removed fixture should become a recorded success, not a new incident.
This is where AWS tags and Docker labels work well together. The cloud resource answers “who owns this?” while the container evidence answers “which test created it?” Without both sides, the on-call engineer has to guess, and guesses make cleanup slower.
Keep cleanup idempotent
Cleanup should run in three places: after a successful test, after a failed test, and in the scheduled reconciler. All three paths should call the same close operation. It also catchs expired resources that were missed by the CI runner.
Use bounded retries for temporary API failures, but stop retrying when the service returns a permanent not-found response. Record the final result with a timestamp. If the close request times out, mark it as unknown and let reconciliation check the resource later; dont claim deletion just because the client stopped waiting.
The result is a simple operational loop:
- Create a fixture with a run-scoped owner and expiry.
- Run the Docker test and emit a compact lifecycle record.
- Close the fixture in the normal and failure paths.
- Alarm on expired or unknown leases.
- Reconcile and record what was recovered.
That loop keeps cloud clutter from becoming invisible background debt. It is a bit more safer than relying on a final shell command, and it makes the next failure easier to explain.
If the flow validates authentication or account creation, keep the email check explicit; email checks in signup flows should fail for the right reason, not because an old fixture happened to receive the message.
Q&A
Should every test use a separate fixture?
Every parallel or independently retried scenario should have its own owner. Sequential tests can share a fixture only when the lease and cleanup boundary are still unambiguous. Sharing usually makes a failed run more harder to investigate.
Do I need CloudWatch for a small project?
Not immediately. Start with a result artifact and a scheduled reconciliation command. Add a metric when expired leases need a visible owner or when the team can act on an alarm.
What should be retained?
Retain identifiers, timestamps, status, and ownership metadata. Do not retain full email content or tokens in long-lived CI artifacts. Operational evidence should answer what happened without becoming sensitive data.
The key rule is simple: a temporary fixture should have a deadline before it has a mailbox. Docker makes ownership portable, and AWS gives the lease a place to be observed after CI is gone.
