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

  1. 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.
  2. Shared or dirty agents — leftover files, ports in use or old Docker containers from a previous job.
  3. Resource limits — runner out of memory or disk; containers OOM-killed.
  4. 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.
  5. Unpinned versions — latest images or loose dependency ranges that change between runs.
  6. 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.

All DevOps Fundamentals questions

More on CI/CD practices