A pipeline is failing randomly — sometimes it passes, sometimes it fails, with no code change. How do you debug it?
IntermediateScenarioCI/CD practices
Answer
In one line: That's a flaky pipeline. Collect data from many runs, compare passing and failing runs, find whether the cause is tests, environment, timing or external dependencies, and then fix or quarantine it.
Step 1 — Gather evidence
- Find which stage and which test fails, and how often (for example 1 in 5 runs).
- Compare logs of a passing run and a failing run side by side.
- Note when it fails: certain runners? busy times of day? only in parallel runs?
Step 2 — Check the common causes
- Flaky tests
- Timing assumptions (
sleep(2)and hoping something is ready). - Tests that depend on the order they run in or on shared state.
- Tests that use the current date/time or random data.
- Timing assumptions (
- Shared or dirty agents — leftover files, ports in use or old Docker containers from a previous job.
- Resource limits — runner out of memory or disk; containers OOM-killed.
- External dependencies — package registry rate limits (Docker Hub pull limits), slow network, third-party APIs, a shared test database used by several pipelines at once.
- Unpinned versions —
latestimages or loose dependency ranges that change between runs. - Race conditions in the app itself — a real bug that only shows under concurrency.
Step 3 — Fix
- Replace sleeps with proper waits/health checks and retries with timeouts.
- Make tests independent; give each test its own data.
- Use ephemeral, clean agents.
- Pin versions; use a registry mirror or cache.
- Mock external services in unit tests.
- Increase resources if the runner is starving.
Step 4 — Protect the team while fixing
- Quarantine the flaky test (run it separately, not blocking) with a ticket and owner.
- Avoid blind automatic reruns — they hide real problems.
Example: integration tests failed about 20% of the time. Logs showed "connection refused" to PostgreSQL. The test started before the database container was ready. Adding a health check wait (pg_isready) fixed it completely.
Tip for interviews: walk through it as a methodical investigation — evidence, hypothesis, fix, prevent.
More on CI/CD practices
What is "pipeline as code", and why is it better than configuring pipelines in a UI?BeginnerWhat is a build agent or runner?BeginnerWhat are pipeline triggers?BeginnerWhat is a quality gate in a CI/CD pipeline?BeginnerHow do you make a slow CI/CD pipeline faster?BeginnerWhat are secrets in CI/CD, and how should they be handled?Beginner