Run email tests in Bitbucket Pipelines
Email tests are easy to run on a laptop and easy to break in CI: the key leaks into a log, two builds read each other's messages, or a parallel step waits for a mail that another step consumed. This guide sets up Playwright Test in Bitbucket Pipelines with the AssertInbox API key stored as a secured repository variable, a tag that is unique for every build and every parallel step, and a step that fails with a readable message. It uses a complete bitbucket-pipelines.yml, a helper and a spec.
How it works
Mail sent to <namespace>.<tag>@assertinbox.com is stored for your key. GET /v1/wait long-polls for up to 25 seconds and returns the newest matching message as JSON, including id, subject, otp and magic_link. Messages are deleted after 24 hours. The tag is the isolation boundary: if every job builds its tags from the build number and their own position in the parallel group, no two jobs can read the same mail.
Get an API key
Get one in a click at /start, or with the request below. One request creates a key and your private namespace. No email address or card is needed. The key is shown once.
curl -s -X POST https://assertinbox.com/v1/keys
# {"api_key":"ai_...","namespace":"k3x9a1bz","address_template":"k3x9a1bz.<tag>@assertinbox.com"}
If the call returns 403 signups_closed, the beta is not open for new keys. Use inboxes only for services you own or control; see the terms. Limits and error codes are on the limits page.
Store the key as a secured repository variable
In your repository, open Repository settings, then Pipelines, then Repository variables. Add ASSERTINBOX_KEY with the api_key value and tick Secured. A secured variable is masked in logs and its value cannot be read back in the settings page. The namespace is not secret, since it is only the first part of an address, so add it as a normal variable named ASSERTINBOX_NAMESPACE. Add APP_URL too if your tests run against a staging URL. Workspace variables work the same way if several repositories share a key.
Pipelines started from forks do not receive secured variables. Decide whether those pipelines should skip the email tests; do not work around it by printing the key.
The pipeline
Save this as bitbucket-pipelines.yml. The step is defined once, with a YAML anchor, and used four times inside a parallel group so the four copies run at the same time. Inside such a group, Bitbucket sets BITBUCKET_PARALLEL_STEP (counting from 0) and BITBUCKET_PARALLEL_STEP_COUNT. Playwright's --shard option counts from 1, so the command adds one. RUN_TAG combines BITBUCKET_BUILD_NUMBER with the step index, so every build and every parallel step get their own tags. The step uses the Playwright image, so no browser install is needed; keep its version in line with @playwright/test in your package.json.
image: mcr.microsoft.com/playwright:v1.49.0-jammy
definitions:
steps:
- step: &e2e
name: Email tests
max-time: 15
script:
- npm ci
- export RUN_TAG="bb-${BITBUCKET_BUILD_NUMBER}-${BITBUCKET_PARALLEL_STEP}"
- npx playwright test --shard=$((BITBUCKET_PARALLEL_STEP + 1))/${BITBUCKET_PARALLEL_STEP_COUNT}
artifacts:
- playwright-report/**
- test-results/**
pipelines:
default:
- parallel:
- step: *e2e
- step: *e2e
- step: *e2e
- step: *e2e
The steps read ASSERTINBOX_KEY, ASSERTINBOX_NAMESPACE and APP_URL from the repository variables. The helper adds a counter and a random suffix to every tag, so even a re-run of the same build number cannot read mail from the first attempt. The artifacts contain the Playwright report and traces, which show the page state when a test fails. They do not contain the key, as long as your tests never print it.
The helper and the test
Save the helper as tests/inbox.js. It needs Node 18 or newer for the built-in fetch. The tag combines RUN_TAG with a counter and a random suffix.
const API = "https://assertinbox.com";
const KEY = process.env.ASSERTINBOX_KEY;
const NAMESPACE = process.env.ASSERTINBOX_NAMESPACE;
if (!KEY || !NAMESPACE) {
throw new Error("Set ASSERTINBOX_KEY and ASSERTINBOX_NAMESPACE");
}
let counter = 0;
function newInbox(label) {
const base = process.env.RUN_TAG || "local-" + Date.now();
const tag = (base + "-" + label + "-" + ++counter + "-" + Math.random().toString(36).slice(2, 6)).toLowerCase();
return { tag, email: NAMESPACE + "." + tag + "@assertinbox.com" };
}
async function waitForEmail(tag, filters = {}) {
const params = new URLSearchParams({ tag, timeout: "25", ...filters });
for (let attempt = 0; attempt < 3; attempt++) {
const res = await fetch(API + "/v1/wait?" + params, {
headers: { Authorization: "Bearer " + KEY },
});
if (res.status === 200) return res.json();
if (res.status !== 408) throw new Error("AssertInbox returned HTTP " + res.status + " for tag " + tag);
}
throw new Error("No matching email arrived for tag " + tag);
}
async function deleteEmails(tag) {
await fetch(API + "/v1/messages?tag=" + encodeURIComponent(tag), {
method: "DELETE",
headers: { Authorization: "Bearer " + KEY },
});
}
module.exports = { newInbox, waitForEmail, deleteEmails };
Save this as tests/signup.spec.js. Replace the page, labels and subject with those of your app.
const { test, expect } = require("@playwright/test");
const { newInbox, waitForEmail, deleteEmails } = require("./inbox");
const APP = process.env.APP_URL || "http://localhost:3000";
test("sign-up with an emailed code", async ({ page }) => {
const inbox = newInbox("signup");
try {
await page.goto(APP + "/signup");
await page.fill("input[name=email]", inbox.email);
await page.click("button[type=submit]");
const mail = await waitForEmail(inbox.tag, { subject: "verify" });
expect(mail.otp).toMatch(/^[0-9]{4,8}$/);
await page.fill("input[name=code]", mail.otp);
await page.click("button[type=submit]");
await expect(page.getByText("Email verified")).toBeVisible();
} finally {
await deleteEmails(inbox.tag);
}
});
Keeping the key out of the logs
- Secured variables are masked by exact value. Do not build the key from pieces or transform it before use.
- Do not echo variables to debug a failing step; print the HTTP status and the tag instead.
- Never run
env,printenvorset -xin a job that has the key. Do not log request headers. - Send the key only in the
Authorizationheader, never in a URL, which can end up in logs and error messages. - If the key leaks, call
POST /v1/keys/me/rotatewith it to get a new one, then update the stored value.
Parallel jobs and the daily allowance
Each key has 2,000 API requests per UTC day, and a wait counts as one request however long it polls. A few parallel jobs that each run a few dozen email tests stay far below that, but a pipeline that runs on every push of a busy repository can add up. If you want to separate pipelines from local runs, create a second key for CI. If a job fails with HTTP 429, the Retry-After header gives the seconds until 00:00 UTC.
Troubleshooting
- "Set ASSERTINBOX_KEY and ASSERTINBOX_NAMESPACE". The variable was added to another repository, or the pipeline comes from a fork that cannot read secured variables.
- The wait times out in CI but not locally. The build container cannot reach your staging app, or the app's mail settings point at a service that is not available from staging. Check the app's log for the send error.
- Two steps read the same mail. The tag does not include
BITBUCKET_PARALLEL_STEP. Check thatRUN_TAGis exported before the test command. - Shard error: index out of range. Playwright counts shards from 1 and Bitbucket counts parallel steps from 0; the command adds one.
Next steps
For unique tags, the trigger-then-wait order, retries and cleanup in one place, read Reliable email tests. More stacks are on the guides page.