Chapter 21 of 27 · Part 9: Deployment and scaling

Continuous integration and delivery pipelines

FTGO's teams change dozens of services every day, and each change must reach production without breaking anyone else's work. This chapter builds the path from commit to production: continuous integration, build pipelines, artifacts you build once, and a code layout that maps cleanly to builds.

key idea / coordinator service / good path in progress / watch out problem / failure neutral

Where we are

In chapter 20 FTGO decided where each service runs: some as functions, most on the Kubernetes cluster from chapter 19. That left one problem open: whichever platform a service runs on, its code still needs a reliable, repeatable path from a developer's commit to production.

This chapter

How does a change to Order Service get from a laptop to customers, safely and many times a day? By the end you can tell real CI from owning a CI tool, design a staged pipeline, choose between continuous delivery and continuous deployment, promote one artifact through environments, and pick a repository layout.

Integrating late breaks the build: continuous integration

Problem

Three developers work on Order Service. Each keeps changes on their own machine for a week and merges on Friday. The merged code does not compile, and once it does, the tests that call Kitchen Service fail. Nobody knows which of the three changes broke them. The build stays red over the weekend, and Monday starts with untangling it.

The longer changes stay apart, the bigger and riskier the merge. The fix is to integrate all the time, and to let a machine check every integration.

Solution · Continuous integration (CI)

Every developer merges to the shared mainline often. On each commit, a CI server checks the code out, compiles it, runs static checks and tests, and, if all is well, builds an artifact (a deployable package, such as a container image) and stores it for later stages.

What a CI run does Commit to mainline CI server checkout, build Verify static, tests Artifact stored once Fast feedback on every change; infrastructure code can live in the same repository.

Because the build is a versioned script, anyone can rebuild an artifact, and every deployed artifact can be traced back to the exact code that produced it.

Owning a CI tool is not the same as doing CI. Jez Humble's three questions are a quick test:

Are you really doing CI? Daily to mainline? merge at least once a day Tests that check? not just "it compiles" Broken build? fix it before anything else If any answer is no, you have a CI tool, not CI. A red build left for days grows costly to fix.
Cost

CI asks for discipline: tests worth trusting, and a team that drops other work when the build turns red. It also asks everyone to merge daily, which collides with how many teams use branches.

Long-lived branches hide the conflicts: trunk-based development

A CI server that builds a branch nobody else sees is not integrating anything.

Problem

The Order Service team builds the new "scheduled orders" feature on a branch. The CI server builds the branch on every commit and it stays green. After five weeks the branch is merged, and it conflicts with dozens of changes made on the mainline in the meantime. The merge takes days, and the feature ships late.

Solution · Trunk-based development

Everyone commits to one trunk, in small changes, at least daily. Branches, if used at all, live less than a day. Unfinished work is merged but hidden behind a feature flag, a switch in configuration that keeps the code path off until it is ready.

Long-lived branches versus trunk Feature branch Trunk-based Work in isolation for weeks merge later: large, painful merge merge merge merge merge merge Small, frequent merges keep the trunk always integrated.

Feature flags pay off twice: the same switch later lets you turn the feature on for a few customers first, the progressive delivery from chapter 19.

Evidence: the 2016 State of DevOps report linked high performance to branches living under a day, fewer than three active branches, and merges to trunk at least daily. Later research found that small patches and early merging help open source projects too.
Cost

Half-finished code now sits in the trunk, guarded by flags that must be cleaned up later. And with every commit triggering a full build, the build itself becomes the bottleneck.

One slow build blocks everyone: build pipelines

Daily merges mean many builds a day, so each build has to give its answer fast.

Problem

Order Service's CI build runs unit tests, then integration and contract tests, then a load test, all in one step. It takes 50 minutes (an illustrative number). A typo that fails the first unit test is reported only after the whole run. Developers stop waiting for results and merge on top of red builds.

Solution · Build pipeline

Split the build into stages: fast tests first, slower tests after, deployment last. If an early stage fails, later stages do not run. Each commit becomes a release candidate that moves through the stages, so you can always see how far a version has got.

One pipeline, three builds Fast tests build-120 failed Slow tests not run Performance not run Production not run Fast tests build-121 passed Slow tests passed Performance failed here Production not reached Fast tests build-122 passed Slow tests passed Performance passed Production deployed Same pipeline, three builds: one version of Order Service reaches production.

The test layers from chapter 14 map onto the stages: unit tests in the fast stage, integration, contract and component tests after it, and load tests near the end. Once a pipeline exists, the last step is a choice: does a passing build go to production on its own?

TermMeaning
Continuous deliveryEvery check-in is a release candidate, and you know how production-ready each one is. Deploying stays a human decision.
Continuous deploymentEvery change that passes the automated checks is deployed with no human step. It builds on continuous delivery.
Delivery versus deployment Continuous delivery Continuous deployment Commit Pipeline passes Human decision release when ready Production Commit Pipeline passes Automatic no manual step Production Delivery makes every build releasable; deployment also removes the gate.

Pick a tool that models pipelines directly, including manual stages such as user acceptance testing, instead of bending a plain CI tool into one. Human steps on the path to production are usually the bottleneck worth automating next.

Cost

A pipeline is more to build and maintain, and splitting tests into stages means deciding which checks may run late. The stages also have to be automated end to end, or a person running commands by hand becomes the slowest stage.

Manual deploys do not scale: scripting the pipeline

A pipeline on a whiteboard does nothing. Each stage has to be a script that a machine runs.

Problem

Order Service passes its tests in CI, but one engineer still deploys it by hand: build the image on a laptop, push it, edit the Kubernetes file, run kubectl. The registry password lives on that laptop. When she is on holiday, Order Service does not ship.

Solution · Continuous deployment workflow

Describe the whole path as a workflow that the CI/CD service runs on every push: test, build the image, publish it to the registry, deploy it to the cluster. Keep settings in environment variables and credentials in the CI service's secret store, never in the repository.

The hands-on FlixTube track (chapter 27) does exactly this with GitHub Actions. A push to main starts the workflow, and plain shell scripts do the work in each step:

A continuous deployment workflow Push to main Test npm test Build Docker image Publish to registry Deploy kubectl apply GitHub Actions starts it on each push. Shell scripts do the work.

Values such as the registry address and the image version come from environment variables, and secrets such as registry credentials live in GitHub secrets. The tool envsubst fills those values into the Kubernetes deployment file before it is piped to kubectl, so one template serves every version.

Cost

Failures in a cloud runner are harder to debug than on a laptop, so keep deployment scripts simple, and reproduce an error locally by copying the workflow's environment variables. And the workflow still has to decide what exactly travels from stage to stage, and where each stage runs.

Ship what you tested: build once, promote everywhere

A release candidate passes through several environments on its way to users. What moves between them matters as much as the tests.

Problem

Order Service build-123 passes every test in pre-production. The production stage then rebuilds the image from the same commit with production settings baked in. A base image changed overnight, so the rebuilt image pulls a newer library version. Orders start failing in production with a bug that no test ever saw.

Environments also differ in what they offer. Early stages want speed; late stages want realism. A laptop gives the fastest feedback but is least like production. A full copy of production is realistic but slow and costly, so some test environments run only a few services, with fakes for the rest. The balance is a choice you revisit, and it is one reason teams also test in production (chapter 15).

Fast feedback versus production-like Laptop fast, unlike prod CI stage fast tests Shared test env slower tests Prod-like env performance faster feedback more production-like Each step trades speed for confidence.
Solution · Build once, promote the artifact

Build the artifact once, at the start of the pipeline, and deploy exactly those bytes to every later environment. Store it in a registry (Artifactory, Nexus or a container registry), and keep everything that differs per environment, such as log levels or database addresses, in external configuration.

Build once, deploy everywhere Build once build-123 Artifact store registry Slow tests Performance Production config: DEBUG here config: INFO here config: INFO here Same bytes everywhere; only external configuration changes per environment.

Container images make this natural. An image is an immutable snapshot, so tests and security checks run against it stay valid wherever it goes. A container is a running instance of an image; many containers can start from one image. Give each build its own tag and bump it for every deployment, rather than overwriting a tag such as latest.

Image versus container Image Immutable snapshot Tagged, e.g. order-service:123 Kept in a registry Container A running instance Filesystem can change Many from one image Images cannot change, so tests on one stay valid. Bump the tag per deployment.
Cost

Configuration must live outside the artifact and be managed per environment (the externalized configuration of chapter 16), and the registry becomes one more system to run. One question is still open: which commits trigger which service's pipeline?

Map code to builds: one repository or many

How FTGO lays out its source code decides which builds a commit starts, and who is blocked when one breaks.

Problem

FTGO started with one big repository and one build for every service. A flaky test in Kitchen Service turns the build red, so Order Service's urgent fix cannot ship. Worse, because everything is built and released together, teams start to assume that services deploy together, and quietly lose independent deployability.

There are three common layouts:

Three ways to map repos to builds One giant repo, one build One repo every service One build all services Repo per service Order repo Build Kitchen repo Build Monorepo One repo order/ kitchen/ build A build B Folders map to separate builds; shared folders can trigger many builds.
Solution · One build per service

Whatever the repository layout, give each service its own build, triggered only by changes to that service's code. Then a red build blocks one team, not all of them.

Mapping code to builds One repo, one build Any change rebuilds all One break blocks all Rare except early on Repo per service Change maps to one build Clear ownership Cross-repo changes hurt Monorepo Atomic commits Needs tooling at scale Hard to leave later A monorepo should still map each folder to its own build, or you deploy everything together.
Giant repo + one buildOne repo per serviceMonorepo
GoodSimplest startClear commit-to-build map; per-repo ownershipAtomic commits, fine-grained code reuse, visibility
BadOne red build blocks everyone; encourages deploying togetherCross-repo changes are awkward; shared code needs versioned librariesNeeds tooling (such as Bazel) at scale; blurs ownership
A common pickRarely used beyond day onePreferred as you scaleFine for 10 to 20 developers or very large, tool-rich firms

Giants such as Google and Microsoft make monorepos work by investing heavily in tooling, and small groups of 10 to 20 developers manage too. Organizations in between tend to struggle. Sam Newman, for one, prefers one repository per microservice at scale: the benefits of reuse and atomic commits do not outweigh awkward cross-service changes and blurred ownership. The small FlixTube app in the hands-on track takes the other route that works: one monorepo, with a separate CD workflow file for each microservice.

Atomic commit is not atomic deploy. Changing Order and Kitchen Service in one commit does not mean they roll out together. Wanting them to is a sign you are losing independent deployability.
Cost

Per-service repositories make cross-service changes and shared code harder; a monorepo needs build tooling and ownership rules. Either way, FTGO can now ship each service many times a day. Shipping is not the same as coping with load, though: when Saturday lunch brings ten times the usual orders, the services must scale.

Practices in this chapter

PracticeProblem it solvesWhat it costs
Continuous integrationLate, large merges that break the build and hide who broke itDaily merges, trusted tests, fixing red builds first
Trunk-based development with feature flagsLong-lived branches that conflict when finally mergedUnfinished code in trunk; flags to clean up
Build pipeline with stagesSlow all-in-one builds give feedback too latePipeline to maintain; choosing which checks run late
Continuous delivery / deploymentNot knowing whether a build is releasable; manual release gatesDelivery keeps a human step; deployment needs full trust in tests
Scripted CD workflow with external secretsManual deploys from one person's laptopCloud runs are harder to debug; scripts to keep simple
Build once, promote the artifactRebuilt artifacts that differ from what was testedExternalized configuration per environment; a registry to run
One build per service (repo per service or monorepo)One red build blocks every team; services drift into deploying togetherAwkward cross-repo changes, or monorepo tooling and ownership rules

Recap

Quick check

FTGO's production stage rebuilds the Order Service image from the tested commit, and orders start failing with a bug no test caught. What went wrong, and which practice fixes it?
Show answer

The rebuilt image is not the artifact that was tested: a changed base image or dependency slipped in. Build the image once, store it in a registry, and promote those exact bytes through every environment, varying only external configuration.

The Kitchen team's feature branch has been building green in CI for a month. Are they doing continuous integration?
Show answer

No. CI means integrating with everyone else's work on the mainline at least daily. A green branch only proves the branch works on its own; the conflicts wait for the merge. Merge small changes to trunk daily and hide unfinished work behind a feature flag.

FTGO keeps all services in one repository with one build. A flaky Kitchen test blocks an urgent Order fix. What should change?
Show answer

Give each service its own build, triggered only by changes to its own code, either by moving to one repository per service or by mapping each folder of the monorepo to a separate build. Then a red Kitchen build blocks only the Kitchen team.

Still open

FTGO can now take any service from commit to production quickly and safely. But production load is not constant: a Saturday lunch rush brings many times the usual orders, and each service and its database must grow to meet it.

Next: 22 Scaling microservices →