← Back to blog

October 7, 2026 · 16 min read

Building CI/CD Pipelines Your Team Actually Trusts

From Mid-Level to Senior · Part 8 of 18

  • #ci-cd
  • #devops
  • #github-actions
  • #senior-engineer
  • #engineering-practices

You can tell a lot about a team by watching what happens when the build goes red.

On some teams, somebody stops and looks. On others, somebody clicks "Re-run" without opening the log, because "that test always does that". On a few, people have learned which checks are safe to ignore, deploys happen on Tuesday mornings when one specific person is around, and nobody has touched the pipeline YAML in a year because the last person who did broke it for two days.

All three teams have CI/CD. Only the first one trusts it.

This part opens Module B of the series, where we leave the code itself and look at how it gets to production and stays alive there. If you followed Track 1, you did a first deploy by hand in Packaging and Running Your App, and I said then that a real team puts a pipeline in front of that. This is that pipeline. The examples use GitHub Actions because I've worked with it a lot, but nothing here depends on the tool.

What the pipeline is for

Forget the acronyms for a second. A pipeline answers two questions, automatically, for every change:

  1. Is this change safe to merge? That's CI: every push gets compiled, tested, and checked against the main branch.
  2. Is this commit safe to run in production? That's CD: every commit on the main branch becomes something deployable, and getting it to production is a routine step.

(The "D" can mean delivery, where a human presses the last button, or deployment, where nobody does. Both are fine. What matters is that the button is boring.)

Trust means the answers are reliable in both directions. Green means it's really safe, so people merge and deploy without a private ritual of checking by hand. Red means something is really wrong, so people stop and look. The moment either one stops being true, the team starts working around the pipeline, and then you're paying for a pipeline and still running on "it works on my machine".

Stages, ordered by what they cost

Most pipelines have roughly the same stages. The order is where the thinking goes: cheap and likely-to-fail checks first, slow and expensive ones later.

A pipeline drawn as five stages from left to right. Stage 1, Lint and compile: formatting, static checks, compilation, takes seconds. Stage 2, Unit tests: no network or database, one to three minutes. Stage 3, Build and integration: package the artifact, run tests against a real database with migrations applied, a few minutes. Stage 4, Scan: dependency vulnerabilities, secrets, container image. Stage 5, Deploy: staging, smoke test, then production. A bar under the first three stages marks them as running on every pull request with a ten minute budget. A bar under the last stages marks them as running after merge. A note says to put the cheapest checks first so most failures show up in the first minutes.

  • Lint, format, compile. Seconds. If the code doesn't compile or the formatter complains, there's no reason to boot a database to find out.
  • Unit tests. No network, no database, no clock. They should run in a minute or two and catch most logic mistakes.
  • Build the artifact. The jar or the container image. Build it once here and keep it. More on that below.
  • Integration tests. The application against real dependencies. With Testcontainers you get a real PostgreSQL in the pipeline, which means your migrations run against the same database engine as production. I mentioned in Part 7 that migrations belong in CI, and this is the place: Flyway or Liquibase applies every changeset on an empty database, then the tests run on top of it. A migration that fails here is a migration that didn't fail in production.
  • Scans. Dependency vulnerabilities, leaked secrets, the container image. Useful, and also the stage most likely to turn into noise, so tune it. A scanner that reports 140 "medium" findings on every run gets ignored on every run.
  • Deploy. To a staging environment first, then a smoke test, then production.

Not every stage has to run at the same moment. The first three or four run on every pull request. The slower scans and the deploys run after the merge. A full end-to-end suite that takes forty minutes can run on a schedule, as long as someone owns what happens when it fails.

Fast feedback, or people stop waiting

Here's a number worth defending: a pull request should get its answer in about ten minutes. Past that, people open another task while they wait, and when the build fails twenty-five minutes later they've already lost the context. Slow pipelines also push people toward bigger PRs ("since I'm waiting anyway"), which is the opposite of what Part 3 asked for.

The things that usually buy back the most time:

  • Cache dependencies. Downloading the whole Maven repository on every run is the most common waste I see.
  • Run jobs in parallel. Lint, unit tests, and the build don't need to wait for each other. Split a slow test suite across several runners.
  • Cancel runs that no longer matter. If you pushed twice in a minute, nobody needs the result of the first push.
  • Fail fast. Order the steps so the first failure stops the job.
  • Move slow things out of the PR path. Run them after merge or nightly, with a clear owner.

In GitHub Actions, the first few of those are a handful of lines:

name: ci
on:
  pull_request:
  push:
    branches: [main]

concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true

jobs:
  test:
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-java@v5
        with:
          distribution: corretto
          java-version: 25
          cache: maven
      - run: ./mvnw -B verify

The concurrency block cancels the older run when a new push arrives on the same branch. cache: maven restores ~/.m2 between runs. And timeout-minutes matters more than it looks: a job that hangs for six hours (the default limit) is a runner nobody else can use.

One warning about a popular trick. Path filters ("only run the backend tests when backend files change") are great for speed and dangerous for trust. If a filter is wrong, the pipeline is green because it tested nothing. Use them, but be careful with shared code, and run everything on the main branch regardless.

Then measure. Look at the pipeline's duration every month or so, the same way you'd look at an endpoint's latency. It only ever gets slower by itself.

Flaky tests: where trust goes to die

A flaky test is one that passes and fails on the same code. Each one looks harmless. Together they teach the whole team one habit: when it's red, click re-run.

Once that habit exists, your pipeline is worth much less than you think, because a real failure now looks exactly like the noise. Someone will re-run a build three times until a genuine race condition happens to pass, and merge it.

A flow in two rows. Top row, the bad loop: a test fails randomly, someone clicks re-run, it passes, nothing is recorded, and the team learns that red means try again. An arrow leads to the result: real failures get re-run too. Bottom row, the discipline: a test fails randomly, it is confirmed as flaky by a pass on the same commit, it is quarantined the same day with a ticket, an owner and a deadline, the root cause is fixed, and the test goes back to the required suite. A note says quarantine is a waiting room with a deadline, not a place where tests go to be forgotten.

What works is treating a flaky test as a bug with a short deadline:

  • Confirm it. Same commit, different results. Most CI tools can detect this for you if you let them record retries.
  • Quarantine it the same day. Move it out of the required suite so it stops blocking everyone else. It still runs, and it still reports.
  • Give it a ticket, an owner, and a date. This is the part teams skip. A quarantine with no deadline is just a slower way to delete tests.
  • Fix the cause or delete the test. A test nobody believes isn't protecting anything. If it isn't worth fixing, be honest and remove it.

About automatic retries: they're acceptable only if every retry is recorded and somebody looks at the report. A silent retry hides the problem and lets it grow.

The causes are nearly always the same handful, so it's worth knowing them:

  • Time. LocalDate.now() inside the code under test, time zones, tests that fail around midnight or at the end of the month. Inject a Clock.
  • Sleeping instead of waiting. Thread.sleep(500) and hoping the async thing has finished. Poll for the condition with a timeout (Awaitility does this well).
  • Shared state. Tests that pass alone and fail together because one leaves rows in the database, or a static field set.
  • Order. Tests that only pass in the order they happen to run on your machine.
  • The outside world. A real HTTP call to a sandbox that's sometimes down. Stub it at the network edge (WireMock) and test the real integration somewhere it can't block a merge.
  • Fixed ports and other shared resources on a runner that executes several jobs at once.

One artifact, many environments

Here's a rule that removes a whole category of "but it worked in staging": build once, and promote the same artifact.

Three environments in a row: staging, pre-production and production. Above them, a single build step produces one artifact, an image tagged with the commit SHA and identified by its digest, stored in a registry. The same artifact is deployed to each environment in turn. Under each environment is its own configuration box with different values for database URL, secrets and feature flags, coming from the environment and not from the image. A crossed-out alternative on the side shows rebuilding per environment, with the note that a rebuild is a different artifact that nobody tested. A note at the bottom says that rollback is just deploying the previous artifact again.

If you run mvn package again for production, you have a new artifact. Maybe a dependency with a version range resolved differently, maybe the base image moved. Whatever you tested in staging, it wasn't that one.

So the build stage produces one immutable artifact, usually a container image tagged with the commit SHA, and every later stage deploys that exact image. A few consequences follow:

  • Configuration lives outside the artifact. Database URLs, secrets, and feature flags come from the environment. Track 1 covered how Spring Boot layers this. If you need a different build per environment, something that should be configuration has leaked into the code.
  • Tag by commit, deploy by digest. A tag like latest can be moved. A digest can't. You want to be able to say exactly which commit is running in production right now.
  • Staging should look like production where it counts. Same database engine and major version, same way of running migrations, same deploy mechanism. It can be smaller. It shouldn't be different.
  • Migrations are a pipeline step. They run from the pipeline, before the new version of the application starts, with the same tool and the same scripts in every environment. Nobody runs SQL by hand on the way to production.

Required checks: the rules everybody follows

A check that isn't required is a suggestion. Protect the main branch so merging needs a pull request, an approval, and a specific list of status checks, and make that apply to everyone, admins and the CTO included. I've been the CTO in that sentence, and the bypass button is never worth what it costs the first time people see you use it.

Some advice on what goes on that list:

  • Keep it short and meaningful. Compile, tests, integration tests, and the scans you actually act on. Every required check has to deserve to block a merge.
  • Don't keep permanently red or permanently yellow checks around. If a check is "informational" and nobody reads it, delete it. It trains people to ignore the column.
  • Test what will actually be merged. Two PRs can each be green and break main together. Requiring branches to be up to date fixes that on a small team. On a busy repository, a merge queue does the same job without everyone rebasing all afternoon.
  • A red main branch is the team's top priority. Fix it or revert it, quickly. If main stays red for a day, every PR opened that day inherits a failure that isn't theirs, and we're back to people ignoring red.

The pipeline is also a very attractive target, because it holds the keys to production. A short security checklist:

  • Prefer short-lived credentials. OIDC federation between your CI and your cloud provider means there's no long-lived access key sitting in a secrets store waiting to leak.
  • Least privilege per job. The job that runs tests on a PR doesn't need deploy credentials. Use separate environments, with approval on the production one if that fits your team.
  • Pin third-party actions to a commit SHA. A tag can be moved to point at different code. A SHA can't.
  • Be careful with self-hosted runners. They're great for speed and for reaching private networks. They're also machines inside your network that run whatever a workflow tells them to. Keep them off public repositories, make them ephemeral if you can, and don't let pull requests from forks run on them.
  • Review pipeline changes like code. A CODEOWNERS entry for .github/workflows/ is cheap.

Rollback is part of the pipeline

The last piece of trust is knowing what happens when a deploy goes wrong, because one will.

If you've built one artifact per commit, rollback is simple to describe: deploy the previous artifact again, through the same pipeline, with the same steps. It shouldn't need a special script only one person knows, or a rebuild of an old commit (which, again, would be a new artifact).

What makes it work in practice:

  • Practise it. Roll back on purpose in staging, and occasionally in production on a quiet day. A rollback you've never tried is a theory.
  • Check after you deploy. A smoke test and a look at the health endpoint and error rate right after the release, done by the pipeline. If they fail, the pipeline rolls back by itself. That depends on having good signals, which is where Part 9 comes in.
  • Keep the database compatible. This is the hard one. You can put the old code back in a minute, but you can't un-drop a column. The expand and contract pattern from Part 7 is what makes rollback possible: if every migration is compatible with the previous version of the application, the old code runs fine on the new schema. Destructive changes go in a later release, once you're sure you won't go back.
  • Separate deploy from release. With a feature flag, the code is in production but turned off. If the feature misbehaves, you flip the flag and nobody redeploys anything.
  • Know when to roll forward. Sometimes the fix is a one-line change and the pipeline is fast enough that shipping it is the quickest way out. That's a legitimate choice only if your pipeline is fast and trusted, which is one more reason to care about the ten minutes.

And deploy small and often. A release with three commits is easy to reason about and easy to revert. A release with three weeks of commits is neither. How you shift traffic during a release (rolling, blue-green, canary) is its own subject, and we'll get to it in Part 12.

Treat the pipeline as a product

Somebody has to own it. Without an owner, the pipeline is infrastructure everyone uses and nobody improves, and it decays the same way as any unowned code.

I've spent a good amount of time on this with GitHub Actions, self-hosted runners, and Liquibase running in CI, and on one of those pipelines we cut deploy time by around 30%. The lesson I took from that work is that the gains come from ordinary engineering: measuring where the time goes, removing repeated work, and fixing the thing people complain about most. You don't need a platform team to start.

A few numbers worth watching, without turning them into targets:

  • Pipeline duration on pull requests, and its trend.
  • How often main is red, and for how long.
  • Flaky rate: how many runs passed only after a retry.
  • Lead time: from merge to running in production.
  • Change failure rate: how many deploys needed a rollback or a hotfix.

And make it possible to run the same checks locally with one command. If the only way to know whether the pipeline will pass is to push and wait, people will push and wait, forty times a day.

What to try this week

  • Time your PR pipeline. If it's over ten minutes, find the slowest step. Is anything downloading or rebuilding something it could cache?
  • Count the re-runs from the last two weeks. Each one is either a flaky test or a flaky piece of infrastructure. Pick the worst and give it an owner.
  • Check your artifact. Is the thing running in production the same binary that was tested, or was it rebuilt on the way?
  • Read your branch protection settings. Which checks are required? Can anyone bypass them?
  • Ask the team how to roll back. If the answers differ, or the answer is "ask so-and-so", you've found your next task.

The short version

  • A pipeline is trusted when green means safe and red means stop. Protect both.
  • Order stages by cost: cheap checks first, so most failures arrive in the first minutes.
  • Keep PR feedback around ten minutes with caching, parallel jobs, and cancelling stale runs. Measure it regularly.
  • A flaky test is a bug. Quarantine it the same day, with an owner and a deadline, then fix it or delete it.
  • Build one artifact per commit and promote it. Configuration comes from the environment. Migrations run from the pipeline.
  • Required checks apply to everyone. Keep the list short, and fix a red main first.
  • Rollback means deploying the previous artifact again. Practise it, and keep migrations backwards compatible so it stays possible.

A pipeline can tell you a deploy finished. It can't tell you whether the system is healthy afterwards. For that you need to see inside it, and that's Part 9: logs, metrics, and traces, and what each one is actually good for.

This is Part 8 of 18 in the series. The Senior Engineer Roadmap maps the whole series, with a short self-assessment and four reading orders.