Manual deploys are a trust fall: one person's memory, one Friday evening, one typo away from a silent outage. This lesson replaces the SSH session with a pipeline that tests, builds, and ships CityOps on every push — and proves the deployed version is the tested one.
The customer problem
Friday, 6pm. Dev deploys the way he's always deployed: copy the new files to the city server over SSH, restart the service, curl the health endpoint, drive home. This time he mistypes an environment variable name — DB_PASSWROD — and the intake worker starts, fails to connect, and dies quietly. Nobody notices until Monday, when Maria asks why the weekend's complaint counts are zero.
Rollback means remembering which files changed, in which order, on a server he can't fully see from his laptop. He reconstructs it from memory. Mostly.
Monday, Maria asks the question that ends manual deploys: "How do we know the version running on the server is the version you tested?"
Dev's honest answer: "I don't, exactly."
Notice what Maria is not asking. She's not asking for faster deploys — Dev's SSH session took four minutes. She's asking for provable deploys. Four fast minutes that nobody can audit are worth less than ten slow minutes with a paper trail. Manual deployment has three failure modes, and Dev just demonstrated all of them: it's unrepeatable (the steps live in memory), unverifiable (no record of what shipped), and untestable (the artifact on the server was assembled by hand, never the artifact that passed tests).
The FDE principle for this lesson:
"Every push is a release candidate."
Not "every push ships to production" — Maria would (rightly) veto that. It means every push could ship: it's tested, built, and packaged to the same standard as a release, automatically. Whether it actually deploys is a business decision, recorded in the pipeline. The pipeline makes the candidate; humans make the call.
Clarify the ask
Before writing YAML, pin down what "provable deploys" requires. Maria's question decomposes into four concrete requirements:
- Tests run on every push — not "Dev ran them Friday," but a machine ran them against this exact commit, with a visible pass/fail.
- Build once, deploy the build. The artifact on the server must be the artifact that was tested — not a re-assembly, not a copy with fixes applied by hand afterward.
- Deploys are recorded. Who shipped what, when, from which commit. An audit trail, not a memory.
- Rollback is a redeploy. Going back means pointing at the previous artifact — a known-good, previously tested thing — not reconstructing files from memory.
The deliverable isn't "deployments happen automatically." It's a system where the answer to "what's running in production?" is a commit SHA, a test report, and an image digest — all three pointing at each other.
Must know
- CI vs CD, precisely. Continuous integration: every push is automatically built and tested. Continuous delivery/deployment: the tested artifact is automatically made releasable (delivery) or released (deployment). You can have CI without CD; you can't have trustworthy CD without CI.
- The artifact is the contract. Tests prove the code; the image digest proves what shipped. The pipeline's job is to bind the two together so tightly that Maria's question answers itself.
The minimum concept
GitHub Actions has five nouns, and like Docker's four, the lesson hangs on keeping them straight:
| Term | What it is |
|---|---|
| Workflow | A YAML file in .github/workflows/ describing automated steps. The pipeline definition, versioned with the code. |
| Event | What triggers the workflow: a push, a pull request, a schedule, a manual click. |
| Job | A unit of work in the workflow. Jobs run in parallel unless you declare dependencies with needs:. |
| Step | One action inside a job: run a shell command, or use a pre-built action from the marketplace. |
| Runner | The fresh virtual machine each job executes on. Clean every time — which is a feature, not a limitation. |
The key insight is the runner: every job starts on a machine that has never seen your code. That kills an entire class of "works on my machine" — if the tests pass on a fresh runner, they pass because the repo is complete, not because Dev's laptop had the right library installed three years ago. The pipeline is, among other things, a continuous audit of your packaging.
Read the arrows as the four requirements from the previous section: the push triggers tests; the build only runs if tests pass; the deploy only runs if the build produced an artifact; production runs the artifact. Each gate is declared in the file, visible to everyone, enforced by the machine.
Build
Start with CI alone — test every push. This file lives at .github/workflows/ci.yaml in the repo:
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
test:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: pip
- name: Install dependencies
run: pip install -r requirements.txt -r requirements-dev.txt
- name: Run tests
run: pytest -q
Push, and within a minute or two there's a green check (or a red X) on the commit. A few decisions worth noticing:
- Both
pushandpull_request. Testing PRs before merge is where this earns its keep — the red X appears before the code reaches main, not after. - Pinned runner and action versions (
ubuntu-24.04,@v4,@v5). The pipeline is code; pin it like code. A floating@mainreference means someone else's commit can change your builds. cache: pip. Dependency installs are the slowest step; caching them between runs keeps the feedback loop short. Fast feedback is what makes developers actually wait for the check instead of merging blind.
Now extend it: after tests pass, build the Docker image from the previous lesson and push it to the GitHub Container Registry, tagged with the commit SHA:
build:
needs: test
runs-on: ubuntu-24.04
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- uses: docker/login-action@v5
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v6
id: build
with:
context: . # the checked-out workspace, explicitly
push: true
tags: ghcr.io/cityops/api:${{ github.sha }}
# The digest is the IMMUTABLE identity. The tag is a nickname —
# capture the digest and deploy THAT, not the tag.
- name: Record image digest
run: echo "DIGEST=${{ steps.build.outputs.digest }}" >> $GITHUB_ENV
The digest output (e.g. sha256:9f3c...) is what Maria's question demands: not "the image tagged with this commit" but "these exact bytes." The deploy job receives the digest and pulls ghcr.io/cityops/api@sha256:9f3c... — a reference that cannot be retagged out from under you.
needs: test is the whole game in one line: no green tests, no image. The image is built from the exact commit that passed, by a machine, with no human hands in between. ${{ github.sha }} tags the image with the commit's identity — which sets up the answer to Maria's question, coming in the Break section.
The pieces, named
The workflow leans on four pre-built actions. Know what each one does — "the marketplace did it" is not an explanation you can give Lisa:
| Action | What it does | Why this one |
|---|---|---|
actions/checkout@v4 | Clones the repo into the runner at the triggering commit. | The official one; pinned to a major version. |
actions/setup-python@v5 | Installs the requested Python and caches pip between runs. | Guarantees 3.12 regardless of what the runner image ships. |
docker/login-action@v5 | Authenticates to a container registry. | Keeps credentials out of run: scripts. |
docker/build-push-action@v6 | Builds the image and pushes it with the given tags. | BuildKit under the hood; layer caching available when builds get slow. |
A word on trusting the marketplace: these are among the most-used actions in existence, maintained by GitHub and Docker respectively. "Most-used" is not a security argument — pinning to a full commit SHA is the stronger one, and the productionize section covers it — but it's a reasonable starting point while you're learning the shape of a pipeline.
Break
Three failures, each one a lesson the pipeline teaches better than any lecture.
Break 1: The red X that saved main
Dev pushes a refactor that passes on his laptop and fails in CI: a test imports a helper that exists in his site-packages but was never added to requirements.txt. On the laptop, invisible. On the fresh runner, a loud ModuleNotFoundError and a red X on the commit.
This is the pipeline auditing the packaging, exactly as promised. The fix is one line in requirements.txt — but the real fix is structural: with branch protection requiring the test job to pass before merge, this commit cannot reach main until it's fixed. The pipeline doesn't just detect the bug; it enforces the standard. Dev's laptop is no longer a supported deployment source, and nobody had to have an awkward conversation about it.
Break 2: The :latest lie
Dev's first version tagged the image ghcr.io/cityops/api:latest and deployed "latest." Then Maria asked her question, and Dev realized he couldn't answer it: latest is a nickname that gets reassigned on every build. The image running on the server on Monday might not be the image that passed tests on Friday — "latest" moved over the weekend.
The fix is the SHA tag from the build job. ghcr.io/cityops/api:9f3a2c1… is an identity, not a nickname: it names one commit, one test run, one image. And behind the tag, the registry stores the image digest — a content hash. Same digest, same bytes, guaranteed. When Maria asks "is the deployed version the tested version?", the answer is: the deploy references digest sha256:…, which was built from commit 9f3a2c1, whose test job passed in run #87. Three pointers, all agreeing. That's what "provable" means.
Break 3: The secret in the log
Debugging a failed deploy, Dev adds a step that echoes the environment to "see what's set." The build log now contains the database password — and build logs are visible to everyone with repo access, retained indefinitely.
Lisa's voice, which you should now hear preemptively: "Logs are a liability surface. Who can read that log, and for how long?" GitHub automatically masks values that arrive through secrets.* — the log shows *** instead of the password — but only if the value actually came through secrets. The durable rules: never echo secrets or whole environments in steps; pass credentials through the secrets context; rotate anything that was ever printed. Masking is a safety net, not a strategy.
Productionize
CI builds the candidate. Now make the release to production deliberate, recorded, and reversible.
Gate the deploy
Add a deploy job that only runs on main, only after a successful build, and only into a production environment — GitHub's mechanism for "a human must approve this":
deploy:
needs: build
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-24.04
environment: production
# Deployments are NOT cancelled by newer pushes — a half-deployed
# release is worse than a queued one. (CI uses cancel-in-progress;
# the deploy job opts out.)
concurrency:
group: production-deploy
cancel-in-progress: false
steps:
- name: Deploy to city server
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.CITY_HOST }}
username: ${{ secrets.CITY_USER }}
key: ${{ secrets.CITY_SSH_KEY }}
# Verify the server's identity — no blind trust on first connect.
keyscan: ${{ secrets.CITY_HOST }}
script: |
cd /srv/cityops
# Deploy the DIGEST, not the tag — and BOTH api and worker,
# from the same immutable image. (compose.prod.yaml sets
# image: ghcr.io/cityops/api@${IMAGE_DIGEST} and contains
# NO build: stanza — production never builds locally.)
export IMAGE_DIGEST=${{ needs.build.outputs.digest }}
docker compose -f compose.prod.yaml pull api worker
docker compose -f compose.prod.yaml up -d api worker
# Health: BOTH services must answer, or the release fails.
for i in $(seq 1 30); do
curl -sf http://localhost:8000/health && break || sleep 5
done
curl -sf http://localhost:8000/health || \
(echo "DEPLOY FAILED: api unhealthy" && exit 1)
docker compose -f compose.prod.yaml exec worker \
python -c "import app.main" || \
(echo "DEPLOY FAILED: worker unhealthy" && exit 1)
echo "deployed ${IMAGE_DIGEST} — api and worker healthy"
What changed and why: the deploy pulls image@sha256:... — the exact bytes CI built — not a retaggable tag. Both api and worker move together; a split-brain release (new API, old worker) is a real incident class. The compose.prod.yaml contains no build: stanza at all — production consumes published artifacts, full stop. SSH host identity is verified via keyscan. And the release fails loudly if either service is unhealthy after deploy.
The moving parts:
environment: production— configure it in the repo settings with required reviewers (Tom, plus Maria for visibility). The pipeline pauses here and waits for a human click. Automation builds the candidate; a person authorizes the release. That separation is what lets Lisa sleep: no commit, however green, ships to the city without a named approver.if: github.ref == 'refs/heads/main'— feature branches get tested and built, but onlymaindeploys. Every push is a release candidate; onlymainis a release.compose.prod.yaml— a small override file on the server that swapsbuild: .for the registry image:
# compose.prod.yaml — lives on the server, not in the dev loop
services:
api:
image: ghcr.io/cityops/api:${IMAGE_TAG:?IMAGE_TAG is required}
pull_policy: always
${IMAGE_TAG:?…} fails loudly if the variable is unset — better a clear error at deploy time than a silent fallback to a stale image. pull_policy: always makes sure the server actually fetches the new digest instead of running a cached older one with the same tag.
Rollback is a redeploy
Friday's deploy misbehaves? The fix isn't SSH archaeology — it's a dedicated rollback workflow that redeploys a previous digest. Rerunning the deploy workflow won't work: it hardcodes ${{ github.sha }}, so a re-run just redeploys the same broken release.
# .github/workflows/rollback.yml — manual, audited, digest-pinned
name: Rollback production
on:
workflow_dispatch:
inputs:
digest:
description: "Image digest to restore (sha256:...)"
required: true
jobs:
rollback:
runs-on: ubuntu-24.04
environment: production # same human approval as a deploy
concurrency:
group: production-deploy
cancel-in-progress: false
steps:
- name: Roll back to digest
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.CITY_HOST }}
username: ${{ secrets.CITY_USER }}
key: ${{ secrets.CITY_SSH_KEY }}
keyscan: ${{ secrets.CITY_HOST }}
script: |
cd /srv/cityops
# Validate the digest format before touching production.
echo "${{ inputs.digest }}" | grep -qE '^sha256:[0-9a-f]{64}$' \
|| (echo "invalid digest" && exit 1)
export IMAGE_DIGEST=${{ inputs.digest }}
docker compose -f compose.prod.yaml pull api worker
docker compose -f compose.prod.yaml up -d api worker
curl -sf http://localhost:8000/health || exit 1
echo "rolled back to ${IMAGE_DIGEST} by ${{ github.actor }}"
The previous image is still in the registry, still the artifact that passed tests, still byte-identical. The rollback is recorded (who, when, which digest) on the environment page like any deploy. One honest caveat: rolling back code does not roll back schema — if the bad release ran a migration, the rollback needs a migration plan too, not just an old image.
Pipeline hygiene
- Cancel stale CI, never stale deploys. Two pushes in a minute shouldn't test twice —
cancel-in-progress: truebelongs on the CI workflow. The deploy job uses its own concurrency group withcancel-in-progress: false: cancelling a production mutation mid-flight leaves a half-deployed release, which is worse than a queued one. - Pin every action.
@v4,@v5,@v6— or, for the truly careful, full commit SHAs. Your pipeline is supply chain; treat it like one. - Keep the deploy step boring. All the cleverness belongs in the image (tested) and the compose files (reviewed). The deploy job should be five lines that pull and restart. If deploying needs judgment, the judgment belongs in the approval click, not in a shell script.
One more productionize habit: keep the pipeline fast enough that developers don't route around it. The cache: pip line, the layer-cached image builds, and cancel-in-progress all serve the same goal — a red X in two minutes gets fixed; a red X in forty minutes gets ignored, and then someone merges with a failing check "just this once." Speed is a compliance feature.
Useful later
- OIDC instead of stored keys — the SSH key in
secrets.CITY_SSH_KEYis a long-lived credential. Cloud providers let the runner mint short-lived tokens via OpenID Connect; no stored secrets to rotate or leak. Worth the migration once deploys are routine. - Staging environments — a second
environment: stagingthat auto-deploys everymainpush gives Maria a place to click through the candidate before approving production. Same artifact, earlier gate. - Deployment attestations — signed statements binding the image digest to the commit and the test run. The audit trail, cryptographically sealed, for the day a regulator asks.
Communicate
The pipeline is built; now it has to answer Maria's question in a form she'll actually look at. She will never read the YAML. She will read three things:
- The commit list. Every commit on
maincarries a green check or a red X. Green means "a machine built and tested this exact code." She doesn't need to know what pytest is; she needs to know the check exists and it's not Dev's word. - The production environment page. GitHub shows which run deployed, when, which commit, and who approved it. "Deployed
9f3a2c1at 14:02, approved by Tom, pipeline run #87." That page is the audit trail from requirement three. - The README badge. A small green badge next to the repo name — the current state of
main, public to the whole team. Social pressure, automated. One line of Markdown:
[](https://github.com/cityops/cityops/actions)
None of these require Maria to understand YAML, containers, or runners. They require her to trust a system where the evidence is visible — which is the entire point of the pipeline.
And when she asks the question from the scene — "How do we know the deployed version matches what we tested?" — Dev finally has a one-sentence answer with evidence behind it:
"Production runs image digest sha256:…, built from commit 9f3a2c1, whose tests passed in run #87 before the image was built — and the production environment page shows Tom approved that exact run."
Notice what changed since Friday's SSH session: every claim in that sentence is checkable by someone who isn't Dev. That's the difference between trust and proof, and it's what Maria was actually buying.
CityOps
Put it all together: the full path from Dev's laptop to the city server, with no human hands on the artifact in between.
The complete workflow file, for reference — this is the whole pipeline in one place:
name: CityOps pipeline
on:
push:
branches: [main]
pull_request:
concurrency:
group: cityops-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: pip
- name: Install dependencies
run: pip install -r requirements.txt -r requirements-dev.txt
- name: Run tests
run: pytest -q
build:
needs: test
runs-on: ubuntu-24.04
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- uses: docker/login-action@v5
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v6
with:
push: true
tags: ghcr.io/cityops/api:${{ github.sha }}
deploy:
needs: build
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-24.04
environment: production
# Deployments are NOT cancelled by newer pushes — a half-deployed
# release is worse than a queued one. (CI uses cancel-in-progress;
# the deploy job opts out.)
concurrency:
group: production-deploy
cancel-in-progress: false
steps:
- name: Deploy to city server
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.CITY_HOST }}
username: ${{ secrets.CITY_USER }}
key: ${{ secrets.CITY_SSH_KEY }}
# Verify the server's identity — no blind trust on first connect.
keyscan: ${{ secrets.CITY_HOST }}
script: |
cd /srv/cityops
# Deploy the DIGEST, not the tag — and BOTH api and worker,
# from the same immutable image. (compose.prod.yaml sets
# image: ghcr.io/cityops/api@${IMAGE_DIGEST} and contains
# NO build: stanza — production never builds locally.)
export IMAGE_DIGEST=${{ needs.build.outputs.digest }}
docker compose -f compose.prod.yaml pull api worker
docker compose -f compose.prod.yaml up -d api worker
# Health: BOTH services must answer, or the release fails.
for i in $(seq 1 30); do
curl -sf http://localhost:8000/health && break || sleep 5
done
curl -sf http://localhost:8000/health || \
(echo "DEPLOY FAILED: api unhealthy" && exit 1)
docker compose -f compose.prod.yaml exec worker \
python -c "import app.main" || \
(echo "DEPLOY FAILED: worker unhealthy" && exit 1)
echo "deployed ${IMAGE_DIGEST} — api and worker healthy"
What changed and why: the deploy pulls image@sha256:... — the exact bytes CI built — not a retaggable tag. Both api and worker move together; a split-brain release (new API, old worker) is a real incident class. The compose.prod.yaml contains no build: stanza at all — production consumes published artifacts, full stop. SSH host identity is verified via keyscan. And the release fails loudly if either service is unhealthy after deploy.
Trace Maria's four requirements through it: tests run on every push (the test job, on a fresh runner); the artifact is built once, from the tested commit (needs: test, SHA tag); the deploy is recorded (the environment page: who, when, which SHA); rollback is a redeploy (previous digests wait in the registry). And the previous lesson's packaging is what makes any of this meaningful — the pipeline ships images because images are the reproducible unit.
One honest boundary: this pipeline proves the software is what was tested. It doesn't prove the data is safe, the secrets are rotated, or the server is patched — those are Tom's and Lisa's domains, and the coming lessons (secrets management, monitoring) take them up. A pipeline is necessary infrastructure for trustworthy deploys, not sufficient infrastructure for trustworthy operations. Say that to Maria before she assumes otherwise.
The roadmap from here writes itself: the next lessons take up the pieces this pipeline deliberately leaves out. Secrets management covers the credentials the pipeline consumes — how they're stored, rotated, and scoped so Lisa stops worrying about the SSH key in secrets. Monitoring covers the system the pipeline ships — how Tom knows Friday's deploy is healthy without waiting for Maria's Monday question. The pipeline is the spine; operations are the body. You now have the spine.
Field check
- The
buildjob declaresneeds: test. What breaks — concretely — if you remove that line and let the build run in parallel with the tests? - Maria asks the scene's question one more time: "How do we know the deployed version matches what we tested?" Give the one-sentence proof, in terms of digests.
- Why tag the image with the commit SHA instead of
:latest? - The deploy job only runs on
mainand requires theproductionenvironment's approval. Who approves, and why does that separation matter to Lisa? - Friday's deploy is misbehaving and you need to roll back. What exactly do you rerun, and why is it safe?
Answers
1. The image could be built — and then deployed — from a commit whose tests are still running or have failed. The needs: gate is what makes "the artifact was tested" a true statement rather than a hopeful one; without it, you've rebuilt the Friday-evening SSH session with nicer YAML.
2. Production runs image digest sha256:…, built from commit 9f3a2c1 after its tests passed in run #87 — the digest is a content hash, so the bytes on the server are provably the bytes that were built from the tested commit.
3. :latest is a nickname reassigned on every build — it names no particular commit, test run, or image, so "we deployed latest" can't answer which code is running. A SHA tag (backed by the content digest) is an identity: one commit, one test run, one image.
4. Tom (with Maria having visibility) approves via the environment's required reviewers. It matters to Lisa because it separates building the candidate (automatic, every push) from authorizing the release (human, named, recorded) — no commit reaches the city without an accountable approver, and the approval is in the audit trail.
5. Rerun the deploy steps with IMAGE_TAG set to the previous commit's SHA. It's safe because the previous image is still in the registry, byte-identical to what was built and tested then — rollback returns to a known-good artifact instead of reconstructing one from memory.
Comments
Post a Comment