Reproducible builds and environments
Move from a laptop shell to a safe, observable delivery pipeline: version control, Linux processes and permissions, container images, networking, configuration and secrets, deployment, health checks, observability, rollback, reproducibility and security hardening.
What you will be able to do
#- Identify the inputs that make a build non-deterministic.
- Pin dependencies, base images and tool versions.
- Diagnose works-on-my-machine drift.
Overview
#A build is reproducible when the same inputs always produce the same artifact. Reproducibility is what makes a bug reproducible, a rollback trustworthy and a supply chain auditable. Every unpinned input is a source of drift.
Sources of drift
- Floating base image tags such as
node:latest. - Ranges in dependency versions such as
^1.2.3without a committed lockfile. - Build tools installed at build time with no version pin.
- Time, hostname or random values baked into the artifact.
- Different operating systems or CPU architectures.
Pinning
FROM node:20.11.1-alpine@sha256:abc... # base pinned by digest
COPY package.json package-lock.json ./
RUN npm ci --ignore-scripts # exact versions from the lockfile
npm ci installs exactly what the lockfile records; npm install can update it. Commit the lockfile and use ci in pipelines.
Hermetic builds
A hermetic build uses only declared inputs and no network at build time beyond fetching pinned dependencies. Fewer undeclared inputs means fewer surprises.
Diagnosing drift
When something works locally but not in CI, compare: the runtime version, the lockfile, environment variables, and the operating system. A node --version mismatch or an uncommitted lockfile change explains most cases.
Common pitfalls
- Committing a lockfile but running
npm installin CI, which can change it. - Assuming the same base image tag means the same image across machines and time.
- Relying on a globally installed tool that is not in the image.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
CI runs npm install and commits the updated lockfile back to the branch automatically. What problem does this create for reproducibility?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.bash# CI: npm install && git commit package-lock.jsonExercise 2Implement
Write the Dockerfile lines that pin the base image by digest and install dependencies deterministically from the lockfile without running lifecycle scripts.
starter.dockerfile# pin base and install deterministicallyReveal the reference answer
reference.dockerfileFROM node:20.11.1-alpine@sha256:<base-image-digest> WORKDIR /app COPY package.json package-lock.json ./ RUN npm ci --ignore-scriptsAuthored expected output: A build that starts from a fixed base and installs the exact locked dependency versions.
The digest fixes the base image contents. npm ci installs exactly the lockfile versions and fails if the lockfile and package.json disagree. --ignore-scripts avoids arbitrary install scripts running during the build.
Exercise 3Debug
A test passes on a developer's machine but fails in CI with a syntax error. Both use the same commit. What are the first three things to compare?
starter.text# works locally, fails in CIReveal the reference answer
reference.textCompare the runtime version (node --version), the lockfile state (is package-lock.json committed and used with npm ci), and the environment variables or OS differences. A version mismatch or an uncommitted lockfile explains most such failures.Authored expected output: A short comparison list that identifies the drifting input.
Drift comes from an unpinned input. Runtime version, dependency lockfile and environment are the highest-yield comparisons.
Common misconceptions
#- A lockfile alone guarantees reproducibility: the base image and toolchain must be pinned too.
- npm install and npm ci are equivalent: ci installs exactly the lockfile and fails if it is out of sync.
Follow-up practice
#- devops-security-hardening
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.