The Analogy: The Water-Treatment Plant and the Miswired Pass Light
A municipal water-treatment plant takes in river water at one end and delivers drinking water at the other, and between those two points the water passes through a fixed sequence of stages in one direction only. Screening pulls out debris, coagulation clumps the fine particles, sedimentation drops them out, filtration catches what is left, and disinfection finishes the job. The order is not a suggestion. Water is never allowed to skip a stage or run them out of sequence, because each stage assumes the one before it did its work. Filtration is designed for water that has already been settled; feed it raw river water and it clogs in an hour.
Every stage ends in a test, and a batch that fails its test is diverted, not passed along. If the turbidity reading after filtration is too high, that water goes back to be re-treated; it does not flow onward to disinfection on the hope that the next stage will fix it. The reason to test at every stage, rather than once at the end, is cost. A contamination caught at the intake screen costs pennies and a few minutes. The same contamination caught after the water has entered the distribution mains costs a city-wide boil-water order, flushing of every pipe, and weeks of lost public trust. The earlier the catch, the cheaper the fix, and the whole plant is arranged so that the cheapest, fastest tests run first.
Between the plant and the public taps sits a treated-water reservoir, a clearwell that holds finished water. Water arriving there is complete and drinkable, but it is not yet released. An operator draws a sample, the lab certifies it, and only then is the water let into the mains. Some plants hold that final step for a human to authorize; others open the release valve automatically the instant a sample passes. That is the whole difference between having water ready to deliver and actually delivering it: the same certified water sits in the same reservoir either way, and the only question is whether a person or a passing sample opens the valve.
The disaster at a water plant is almost never a failed filter, because a failed filter fails safely -- it diverts the batch and raises an alarm. The disaster is a sample gauge whose "pass" light is wired to stay lit. The lab result says the water is contaminated, but the readout the release valve watches says pass, and the valve opens on the strength of a signal that does not reflect reality. Nothing in the treatment process broke. The readout and the true state simply disagreed, and untreated water reached the mains because the gate trusted the readout. In a deployment pipeline this exact failure is a runner that prints "the build is red" while reporting "success" to the system that guards production -- and the valve to your live servers opens on a build that failed its tests.
Syntax or Core Structure
Continuous integration means every commit is built and tested automatically, immediately, on a clean machine, so that integration problems surface within minutes of the commit that caused them rather than months later on a release day. Continuous delivery and deployment carry that verified code onward toward production. Underneath all three is one mechanism: an ordered list of stages, run in sequence, where the first failure stops the line. That mechanism is small enough to write in a dozen lines, and writing it makes the behavior concrete.
# a pipeline is an ordered list of "name:command" stages.
# run them in order, stop at the first failure, report an honest exit code.
run_pipeline() {
local failed=
for stage in "$@"; do
local name=${stage%%:*} cmd=${stage#*:}
if [ -n "$failed" ]; then
printf '%-18s %s\n' "$name" skipped
continue
fi
if eval "$cmd" >/dev/null 2>&1; then
printf '%-18s %s\n' "$name" pass
else
printf '%-18s %s\n' "$name" FAIL
failed=$name
fi
done
if [ -n "$failed" ]; then
echo "pipeline=red first-failure=$failed"
return 1
fi
echo pipeline=green
return 0
}
run_pipeline lint:true unit:true build:true deploy:true
echo "rc=$?"
Output:
lint pass
unit pass
build pass
deploy pass
pipeline=green
rc=0
Four stages, each a name and a command; here every command is true, so every stage passes, the pipeline is green, and the exit code is zero. The rc=0 line matters more than it looks: it is the signal a surrounding script reads to decide whether to proceed. A pipeline that cannot report its result honestly through that exit code is a gauge with its pass light wired on, no matter how correct the text above it reads.
Technical Depth: Fail-Fast, the Gate, and the Honest Exit Code
CI, delivery, and deployment are three ideas, not two
The letters blur together, but they name three distinct commitments, and a team can adopt them one at a time. Continuous integration is about the commit: build and test everything, always, on every change. Continuous delivery is about readiness: keep the main branch in a state that could be released at any moment, with the release itself gated on a human. Continuous deployment removes that human: a passing pipeline ships to production on its own.
| Practice | What is automated | Who opens the valve to production | Analogy |
|---|---|---|---|
| Continuous integration | build and test on every commit | nobody, it does not deploy | testing each stage |
| Continuous delivery | build, test, and package, held ready | a person approves the release | certified water held in the clearwell |
| Continuous deployment | build, test, package, and release | a passing pipeline, no human | the valve opens on a passing sample |
Fail-fast: order is a cost decision
When a stage fails, the stages after it do not run. This is not laziness; it is the water plant diverting a bad batch rather than paying to disinfect water it will throw away. Watch the same pipeline with a failing unit stage:
run_pipeline lint:true unit:false build:true deploy:true
echo "rc=$?"
Output:
lint pass
unit FAIL
build skipped
deploy skipped
pipeline=red first-failure=unit
rc=1
One stage passed, one failed, and two were skipped, never started at all. Because the pipeline stops at the first failure, the order of stages is a direct statement about cost: put the cheapest, fastest, most-likely-to-fail checks first. Linting takes a second and catches the most commits, so it runs before the build that takes five minutes, which runs before the deploy that touches real infrastructure. The exit code is now 1, and that is the honest signal a deploy gate must read.
The gate: delivery holds, deployment releases
The difference between delivery and deployment is one conditional. After a green pipeline, deployment runs the release stage unconditionally; delivery holds it behind an approval:
APPROVE=0
run_pipeline lint:true unit:true build:true
echo "green, artifact ready"
if [ "$APPROVE" = 1 ]; then run_pipeline deploy:true; else echo "deploy=held (awaiting approval)"; fi
Output:
lint pass
unit pass
build pass
pipeline=green
green, artifact ready
deploy=held (awaiting approval)
The build is green and the artifact is ready -- certified water in the clearwell -- but the release valve is held because no one approved it. Set APPROVE=1 and the same green pipeline flows straight through to a passing deploy stage. The verified artifact is identical in both cases; the only variable is who, or what, opens the valve.
Real World Example: The Gate That Opened on a Red Build
A team wants their local pre-push check to behave exactly like CI, so they extend the runner: same stages, same fail-fast, and a final one-line summary that reads nicely in the terminal. The rewrite is small and looks harmless.
# the team's runner: same behavior, "tidier" summary line
run_pipeline_buggy() {
local failed=
for stage in "$@"; do
local name=${stage%%:*} cmd=${stage#*:}
if [ -n "$failed" ]; then printf '%-18s %s\n' "$name" skipped; continue; fi
if eval "$cmd" >/dev/null 2>&1; then printf '%-18s %s\n' "$name" pass
else printf '%-18s %s\n' "$name" FAIL; failed=$name; fi
done
[ -z "$failed" ] && echo pipeline=green || echo "pipeline=red first-failure=$failed"
}
Both runners are then used the same way -- as the condition of an if, which is how every deploy gate in the world is written:
echo '-- clean runner as a gate:'
if run_pipeline lint:true unit:false build:true; then echo DEPLOYING; else echo BLOCKED; fi
echo '-- buggy runner as the same gate:'
if run_pipeline_buggy lint:true unit:false build:true; then echo DEPLOYING; else echo BLOCKED; fi
Output:
-- clean runner as a gate:
lint pass
unit FAIL
build skipped
pipeline=red first-failure=unit
BLOCKED
-- buggy runner as the same gate:
lint pass
unit FAIL
build skipped
pipeline=red first-failure=unit
DEPLOYING
Both runners printed exactly the same thing: unit failed, the build was skipped, the pipeline is red. A human reading either transcript reaches the same conclusion. But the if does not read the text; it reads the exit code, and the two runners return different ones. The clean runner returns 1 on a red pipeline, so the gate takes the else branch and prints BLOCKED. The buggy runner prints the identical red summary and then returns 0, so the gate takes the then branch and prints DEPLOYING -- it deployed a build whose tests failed.
The design flaw is the last line of run_pipeline_buggy. The construct [ -z "$failed" ] && echo green || echo red was written to choose which summary to print, and it does that correctly, but a function's exit code is the exit code of its last command, and the last command here is always a successful echo. On a red pipeline the && test fails, the || echo runs and succeeds, and the function returns 0. The text says red; the exit code says green; the pass light is wired on. The clean runner avoids this only because it ends with an explicit return 1, tying the exit code to the true state rather than to the last thing it printed.
Two smaller gaps sit alongside the fatal one, and both would mislead in their own way. Every stage runs with >/dev/null 2>&1, so when a stage fails you get the word FAIL and nothing else -- no test output, no compiler error, no diagnostic at the one moment you need it, which turns every red run into a second run with the redirection removed. And the runner executes each stage with eval, so the stage list is not data but code: a pipeline definition pulled from a config file or an environment variable is arbitrary command execution, and a stage string like deploy:rm -rf / is not caught by anything here.
Common Mistakes
- Reading the pipeline's text instead of its exit code. A gate is an
if, andifreads the return status, not the summary line. A runner that prints "red" but returns 0 will deploy every time, and the transcript will look correct while it does.
- Ending a checker function with an
echo. A function returns the exit code of its last command. If that is a summaryecho, the function always returns success regardless of what it found. End with an explicitreturntied to the actual result.
- Ordering stages by convenience instead of cost. Fail-fast means later stages never run, so a slow build placed before a one-second lint wastes minutes on commits the lint would have rejected instantly. Cheapest and most-likely-to-fail goes first.
- Swallowing stage output unconditionally. Redirecting every stage to
/dev/nullmakes green runs tidy and red runs useless. On failure you want the diagnostics; suppress output only for stages that passed, or capture and print it on failure.
- Confusing continuous delivery with continuous deployment. Delivery keeps production-ready artifacts behind a human approval; deployment removes the human. Turning on auto-deploy while believing you have delivery ships every green commit straight to production, approved or not.
- Treating a green CI run as proof the code is correct. CI proves the tests passed, nothing more. A pipeline with thin tests is a pass light that lights easily; the pipeline is only as trustworthy as the checks inside it.
- Passing the stage list through
evalfrom untrusted input. If the pipeline definition is code, then whoever controls the config controls the machine. A stage string from an environment variable or a fetched file is remote code execution wearing a pipeline's clothes.
- Deploying straight to production with no staging between. The clearwell exists so finished water is sampled before it reaches the mains. Skipping staging means the first environment your release meets is the one your users are in.
- Making the pipeline so slow that people route around it. A check that takes an hour is a check developers learn to skip with
--no-verifyor a merge override. Speed is not a luxury; a pipeline only gates what people are willing to wait for.
Best Practices
- Tie the runner's exit code to the true result with an explicit
return, and never let a trailingechodecide what a gate sees.
- Order stages cheapest-first and most-likely-to-fail-first, so fail-fast spends the least time to reject the most commits.
- Capture each stage's output and print it only when that stage fails, so green runs stay quiet and red runs are debuggable on the first try.
- Decide deliberately between delivery and deployment, and make the approval gate an explicit, visible step rather than an accident of configuration.
- Keep a staging environment that mirrors production, and require a release to pass there before the production valve can open.
- Treat the pipeline definition as data, not code: parse a fixed schema rather than
eval-ing stage strings, so a config file cannot execute arbitrary commands.
- Measure and defend pipeline speed as a first-class concern, because a pipeline people wait for is the only pipeline that gates anything.
- Judge the pipeline by the strength of its checks, not the color of its badge, and add the test that would have caught the last incident.
Practice Exercises
- Expose the exit-code bug. Run both
run_pipelineandrun_pipeline_buggyon a stage list where the middle stage fails, capture$?after each, and report the two exit codes. Explain in one sentence why the buggy one returns 0 while printing "red".
- Make failures debuggable. Modify the clean runner so each stage's output is captured and printed only if that stage fails. Run it with a failing stage that echoes a diagnostic and show that the diagnostic now appears, while a passing run stays silent.
- Build the approval gate. Extend the runner so a
deploystage runs only when anAPPROVEvariable is set, and show two runs of the same green pipeline: one held awaiting approval and one released. State which of continuous delivery and continuous deployment each run models.
- Reorder for cost. Given four stages with running times of 1s (lint), 90s (build), 5s (unit), and 30s (integration), and a commit that fails the unit tests, write the order that reaches the failure fastest and explain how many seconds each of the best and worst orderings would spend before stopping.