Sub topic 15
Common Pitfalls and Anti-Patterns
This chapter assumes you know basic Git usage, can read simple YAML, and have followed the earlier chapters on pipelines, artifacts, environments, continuous delivery, deployment strategies, and delivery metrics. Here you will learn the mistakes that make a CI/CD setup slow, flaky, or dangerous, and how to fix each one.
An anti-pattern is a common practice that looks reasonable but causes more problems than it solves. Most CI/CD anti-patterns grow out of a good intention. A team adds a manual check to be safe, or a retry to keep the build green. Over time, the check or retry hides the real problem.
Pitfalls in the integration stage
Long-lived branches
Continuous integration means merging small changes into the main branch often. A branch that lives for two weeks defeats this. Its changes are tested only against an old copy of main, so conflicts and broken assumptions appear all at once at merge time.
Signs of this problem:
- Pull requests with hundreds of changed files.
- "Merge day" before a release.
- Bugs that appear only after a big merge.
The fix is to keep changes small and merge at least daily. If a feature is not ready for users, merge it anyway and hide it behind a feature flag, as covered in the chapter on rollbacks and feature flags.
A broken main branch that nobody fixes
If the build on main is red and people keep merging, the pipeline stops being a signal. Developers learn to ignore failures. Treat a red main as the top priority. Either fix forward quickly or revert the offending commit. Branch protection rules that require passing checks before merge prevent most of these cases.
Flaky tests
A flaky test is a test that sometimes passes and sometimes fails on the same code. Common causes are timing assumptions, shared state between tests, dependence on the network, and test order.
The tempting response is to add automatic retries until the job goes green. Retries hide the flakiness and slow every run. Over time, nobody trusts a failure, and real bugs get dismissed as "just flaky."
Better responses:
- Quarantine the test: move it to a separate, non-blocking job, and open a ticket to fix it.
- Track which tests fail intermittently so you can see the worst offenders.
- Remove shared state and real network calls from the test.
Slow pipelines
A pipeline that takes 45 minutes teaches developers to batch their changes, which brings back the large merges CI was meant to remove. Common fixes:
- Cache dependencies. The
cache: npmline in the earlier workflow does this. - Run independent jobs in parallel instead of in sequence.
- Run fast checks (lint, unit tests) first so failures appear early.
- Run slow end-to-end tests less often, for example only on main or on a schedule.
Pitfalls in building and promoting
Rebuilding for each environment
If you build once for staging and build again for production, the production code is not the code you tested. A changed dependency or build tool can produce a different result. The two-job workflow from earlier avoids this: the build job uploads the dist artifact, and deploy downloads that same artifact.
The rule is to build once and promote the same artifact through every environment. Differences between environments belong in configuration, not in the build.
Environment drift
Drift happens when staging and production slowly become different: different versions of a database, different operating system patches, settings changed by hand. Then a release passes in staging and fails in production. Define environments as code, create them from the same templates, and avoid manual changes on servers.
Unpinned dependencies and actions
The line uses: actions/checkout@v4 pins the action to a major version. A third-party action pinned to a branch name such as @main can change under you without warning. For anything that handles secrets or deploys, pin to a full commit SHA. For dependencies, commit the lock file and install with npm ci, which fails if the lock file and package.json disagree.
Pitfalls in delivery and deployment
Manual steps hidden inside the "automated" pipeline
A pipeline that ends with "then someone logs in to the server and restarts the service" is not continuous delivery. Manual steps are slow, easy to get wrong, and not recorded. Automate each step. Keep human decisions, such as approval, as an explicit gate, like the production environment approval in the earlier example, rather than as an undocumented task.
Continuous deployment without safety nets
Releasing automatically on every merge is only safe if the pipeline can catch problems and recover from them. Teams that skip this get outages. Before you automate production releases, you need:
- Tests you trust, with meaningful coverage of critical paths.
- Health checks and monitoring that detect a bad release.
- A fast rollback path, or a feature flag that turns the change off.
- A gradual strategy such as canary or rolling releases.
If any of these is missing, continuous delivery with manual approval is the safer choice.
Treating rollback as an afterthought
Rolling back an application is usually easy if you keep old artifacts. Rolling back a database change is not. Dropping a column and then reverting the code leaves the old code without its data. Use the expand and contract approach: first add the new column while the old one still works, deploy code that uses the new one, and only later remove the old one. Each step stays compatible with the version before it.
Testing only in production-like happy paths
A pipeline that checks only that the app starts will let regressions through. A pipeline that runs every possible test on every commit will be too slow. Balance this with a test pyramid: many fast unit tests, fewer integration tests, and a small number of end-to-end tests.
Pitfalls in security
Secrets in the repository or in logs
Putting an API key in the YAML file or in the code exposes it to everyone with read access, and it stays in Git history after you delete it. Store secrets in the CI system's secret store or an external vault, and pass them in at run time. Rotate any secret that has been committed, since deleting it from history is not enough.
name: deploy-with-secret
on:
push:
branches: [main]
permissions:
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- run: ./scripts/deploy.sh
env:
DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}Two details in this example matter. The permissions block limits the default token to read-only access, which follows least privilege. The secret is passed only to the one step that needs it, not to the whole job.
Avoid printing environment variables for debugging, because secrets that are not masked can leak into logs.
Overly broad permissions
Giving every pipeline an admin credential means a single compromised job can damage everything. Give each pipeline the smallest set of permissions it needs, and use separate credentials for production.
Running untrusted code with secrets
Pull requests from forks contain code you did not review. Running it with access to secrets lets an attacker print them. Most CI systems withhold secrets from fork builds by default. Do not change that setting without understanding the risk.
Pitfalls in measurement and culture
Measuring the wrong things
Counting commits, lines of code, or builds run says little about delivery. The metrics from the previous chapter, such as deployment frequency, lead time for changes, change failure rate, and time to restore service, describe outcomes. Use them to find bottlenecks in the system, not to rank individual people. Once a metric is used as a target, people tend to game it.
Treating the pipeline as someone else's job
When only one "build engineer" understands the pipeline, every change waits on that person. Keep the pipeline definition in the same repository as the code, review changes to it like any other code, and make sure the whole team can read it.
Summary table
| Anti-pattern | Why it hurts | Better practice |
|---|---|---|
| Long-lived branches | Large, risky merges | Small changes merged daily, feature flags |
| Ignored red builds | Failures lose meaning | Fix or revert quickly, protect main |
| Retrying flaky tests | Hides real bugs | Quarantine and fix |
| Rebuilding per environment | Production differs from what was tested | Build once, promote the artifact |
| Manual steps in the pipeline | Slow and error-prone | Automate, keep explicit approval gates |
| Secrets in code | Leaked credentials | Secret store, least privilege |
| Auto-deploy without monitoring | Outages go unnoticed | Health checks, canary, rollback |
How the pitfalls relate
Exercise
A team's tests fail about one run in ten with no code change. To keep merges moving, they configure the pipeline to retry the test job up to three times. Explain why this is an anti-pattern and describe two better actions.
Show answer
Retries hide the flakiness instead of removing it. Runs get slower, and developers learn to distrust failures, so real bugs get dismissed as flaky. Better actions: quarantine the flaky test in a separate non-blocking job and open a ticket to fix it, and track which tests fail intermittently so the worst ones are fixed first. Fixing the cause, such as shared state, timing assumptions, or real network calls, is the lasting solution.
Exercise
Your pipeline builds the app separately in the staging job and in the production job. What can go wrong, and how do you change the pipeline?
Show answer
The production build may differ from the one tested in staging, for example because a dependency or tool changed between the two builds, so production runs code that was never verified. Build once, upload the result as an artifact, and have every deploy job download that same artifact. Put environment differences in configuration, not in the build.
Exercise
A developer commits a cloud API key to a private repository, then deletes it in the next commit. Is the problem solved? What should the team do?
Show answer
No. The key remains in Git history and is visible to anyone with read access. The team should revoke and rotate the key, store the new one in the CI secret store or a vault, pass it only to the steps that need it, and apply least-privilege permissions.