It is 18:40, the merge request has two approvals, and the test job fails with ModuleNotFoundError on a module that imports perfectly on your laptop. You rerun the job. Red again, same line. You run pytest locally for the third time: 312 passed in 41 seconds. The temptation is to blame GitLab, add allow_failure: true and go home.

Do not. A job that passes locally and fails in a pipeline is almost never random. Your machine and the runner differ in a small, finite set of ways, and the error message usually points at one of them once you know the map.

This article lays out that map, explains why each difference produces the failures it does, and ends with a decision path you can walk in about fifteen minutes. The same skill comes up in technical interviews for DevOps and platform roles, where "it works on my machine" is a favourite opening for a debugging conversation.

Six differences between your laptop and a runner

Execution environment: everything a job can observe besides the code itself. That includes the container image, the operating system user, the shell, the filesystem contents, the environment variables, the CPU architecture and the resource limits. Your laptop and a GitLab runner almost never agree on all of them.

Fresh clone: a GitLab runner using the default fetch strategy starts each job from a clean checkout of the commit, usually shallow. Nothing you did not commit exists there.

With those two terms in place, here are the six differences that account for nearly every case, ordered by how often they show up in practice.

Difference Your laptop Typical runner Symptom it produces
Untracked files .env, generated code, node_modules present Only committed files Module not found, missing config, empty secrets
Clone depth Full history Shallow clone of a few dozen commits git describe fails, version tags missing, diff against main breaks
Variables Your shell profile and exports Project and group CI variables, some protected Empty token on feature branches, 401 from registries
Image and shell macOS with zsh, or your distro with bash The job image, often Alpine with ash Command not found, bashisms fail, exit code 127
CPU architecture Apple Silicon arm64 Shared runners on amd64 Native wheels or binaries fail, exec format error
Resources and time 16 GB RAM, 10 cores, local timezone A few GB, 2 vCPU, UTC Exit code 137, timeouts, date assertions fail near midnight

The untracked-files case is the classic. A test imports a module that a code generator created on your machine weeks ago; the generator is not part of the pipeline, so the file never exists on the runner. The ModuleNotFoundError in the opening scene usually comes from exactly this.

Protected variables deserve a separate note. In GitLab, a CI/CD variable marked as protected is only exposed to pipelines running on protected branches or tags. Your feature branch pipeline gets an empty string, and the tool that needs the token fails with an authentication error that looks like a credentials problem when it is really a scoping one.

Why each difference produces the failure it does

The table tells you where to look. The mechanism tells you why a fix works, which matters when the obvious fix does not.

Shallow clones break anything that reads history. Version tools such as setuptools-scm or git describe look for the nearest tag; if the tag is 80 commits back and the clone holds 20, the tool either errors or falls back to a placeholder version like 0.0.0.

Diff-based steps, for example running linters only on files changed against main, fail because main is not in the clone at all. Setting the GIT_DEPTH variable to 0 for that job fetches the full history, at the cost of a slower checkout on large repositories.

Shell differences are subtle because the script looks correct. GitLab runs your script: lines with the shell the image provides. On Alpine that is BusyBox ash, which does not understand [[ ]], arrays or set -o pipefail in older versions. A script written and tested in zsh fails with a syntax error or, worse, succeeds while skipping a check. Exit code 127 means the shell could not find the command; it is a strong hint the image lacks a tool your laptop has installed globally.

Architecture mismatches show up as exec format error or as a package manager quietly compiling from source and running out of time. An Apple Silicon laptop pulls arm64 images and arm64 wheels; the runner pulls amd64. If you build a Docker image locally and push it for the pipeline to use, it may be an arm64 image that the amd64 runner cannot execute. Building with an explicit platform flag removes the ambiguity.

Resource limits produce the most confusing failures. A Kubernetes or Docker executor caps memory per job; when the test process exceeds it, the kernel kills it with SIGKILL and the job reports exit code 137 with no stack trace. Locally the same test suite fits easily in 16 GB. Parallel test runners make this worse because they spawn one worker per detected CPU, and inside a container the detected count may be the host's 32 cores while the job is allowed 2.

Time is the last trap. Runners almost always run in UTC. A test that compares a local "today" with dates produced on the runner passes all winter in Lisbon, where local time equals UTC, but in summer it fails between midnight and 01:00 local time, when UTC is still on the previous day. These failures appear for an hour a day and vanish on rerun, which is why they get labelled flaky.

A decision path for finding your cause

Walk these questions in order. Each one either identifies the cause or eliminates a branch.

  1. Does the error mention a file, module or config that exists on your machine? If yes, run git status --ignored locally and check whether that file is untracked or ignored. If it is, the pipeline needs a step that generates it, or it must be committed. If no, go to 2.
  2. Does the error involve tags, versions, or a diff against another branch? If yes, set GIT_DEPTH: 0 on the job and rerun. If no, go to 3.
  3. Does a token, password or registry login come back empty or unauthorized? If yes, check whether the variable is protected and whether the branch is protected. If no, go to 4.
  4. Is the exit code 127, or does the error say "not found" or "syntax error"? If yes, run the job's image locally with docker run and execute the script line by line in its own shell. If no, go to 5.
  5. Is the exit code 137, or does the job hit its timeout? If yes, look at memory limits and worker counts before touching the code. If no, go to 6.
  6. Does the failure depend on time of day, locale or file ordering? If yes, pin the timezone and locale in the job and sort any directory listings the code relies on.

If you reach the end without an answer, reproduce the job exactly: pull the same image tag, mount a fresh clone of the same commit, export only the variables the job receives, and run the script. The older gitlab-runner exec command used to do this for you, but GitLab deprecated it, and a plain docker run with a clean checkout is now the more reliable reproduction.

Typical mistakes that cost an evening:

  • Rerunning the job three times before reading the full log, including the lines above the red one.
  • Adding allow_failure: true "temporarily" and finding it still there six months later.
  • Caching node_modules or a virtual environment across jobs and then debugging a stale cache instead of the code.
  • Confusing cache with artifacts: cache is a best-effort speedup and can be missing; artifacts are the reliable way to pass build output to later stages.
  • Testing a fix only on a feature branch when the failing variable is protected and only exists on main.
  • Installing a tool in before_script without pinning its version, so the pipeline breaks on the day upstream releases a new major version.

Making local runs and pipeline runs agree for good

Fixing one failure is half the job. The other half is narrowing the gap so the next one does not happen.

The strongest single change is to develop inside the same image the pipeline uses. A dev container or a Makefile target that runs tests in that image means your local run and the runner see the same OS, shell, libraries and architecture flag. The "passes locally" class of incident shrinks for a simple reason: there is less "locally" left.

Second, make the pipeline the only place that generates code, and fail loudly when generated files are stale. A check that runs the generator and then git diff --exit-code catches the untracked-file problem at the merge request instead of at 18:40.

Third, set resource requests and limits on runners deliberately and tell test runners how many workers to use, for example two workers on a 2 vCPU job, instead of letting them guess from the host.

There is a fair objection: dev containers add friction, slow down editors on some laptops, and engineers resist them. For a small team with one service and a simple stack, a well-written README and a pinned image tag may be enough. My recommendation depends on scale.

If you have fewer than 5 engineers and one language runtime, pin the image, set GIT_DEPTH where history matters, and keep the decision path above in your runbook. If you have more engineers, more services or any native dependencies, invest the two days in running tests inside the CI image locally; the first avoided evening of debugging pays for it, and the architecture and shell classes of failure mostly disappear.