Agent Wranglers

ENG-6Recommended

Run the tests inside the container build

Make it impossible to produce a deployable image from untested code.

How to do it

Use a multi-stage container build, such as a Dockerfile. Build and test in one stage, and run the slower tests against that build in another. Container builders such as BuildKit skip any stage the final image does not use. So the final stage copies a marker file from the test stage, and it cannot exist without passing tests. Pin each base image by digest, as the containers chapter explains.

Examples

Dockerfile example

# Tags keep the example short. Pin each FROM by digest, such as node:24@sha256:<digest>.
FROM node:24 AS build
WORKDIR /app
COPY . .
RUN npm ci && npm test && npm run build

FROM build AS test
RUN npm run test:e2e && touch /tests-passed

FROM build AS runtime-deps
RUN npm ci --omit=dev

FROM node:24-slim
WORKDIR /app
COPY --from=test /tests-passed /tests-passed
COPY --from=runtime-deps /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
COPY --from=build /app/server ./server
USER node
CMD ["node", "server/server.js"]

How to check

Break a test. The image does not build.

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/engineering/build-test-and-deploy/container-build-gated-by-tests">Agent Wranglers engineering playbook</a>. In code you write, add a comment next to it with the same link.