Build once, deploy many

Why you need it

The tests passed on one build of the app. Then, for production, someone built it again. The new build looked the same but was not: one piece inside had changed. Nobody tested it, and it broke.

Think of it like this

A food inspector checks one sealed jar and puts a sticker on it. The shop must sell that jar, not cook a fresh batch from the same recipe and hope it tastes the same.

Where you’ll find it

  • Docker: Every image has a digest, a fingerprint of its exact contents, so you can ship exactly the image you tested.
  • GitHub: GitHub Actions saves the built files from one step so later steps use them instead of building again.
  • JFrog: Artifactory stores builds and moves the same one from test to production.

What to know first

Continuous integration. Every change is tested with main before it goes in. See Continuous integration.

Build. Turning code into the thing that actually runs: a program, a bundle of files, an image.

Same code, different build. Two builds from the same code can differ, because a build fetches other pieces, and those can change between builds.

How it works

  1. Building twice is the bug. Build 1 was tested. Production got build 2, made later. Inside it, charts was 2.1 instead of 2.0. This is the red tile in the animation.
  2. Build once, and seal it. Build one time and store the result where nobody can change it. That sealed result is an artifact. Give it a name that never moves, like a version number or a fingerprint of its contents.
  3. Test that artifact. The tests run on the artifact itself, not on a fresh build of the same code. This is the tick sticker in the animation.
  4. Ship that same artifact. Test, staging and production all take the same artifact by its name. What was tested is what runs.
  5. Settings stay outside. Production needs different addresses and passwords than test. Those are given to the artifact when it starts, never built into it, so one artifact fits every place.

An example

Monday 10:00: build 1 is made, tested for 20 minutes and passes. It is stored as app-1.

Monday 15:00: a new charts 2.1 comes out. Building again now would quietly pick it up.

Tuesday: production is given app-1, the exact files that passed. The new charts never gets in by accident.

Further reading