Skip to content
Latchkey

GitLab CI before_script Fails - Whole Job Aborts Before script

GitLab concatenates before_script and script into one shell run. If any before_script line exits non-zero, the job fails there and your script never executes.

What this error means

The job fails during setup - installing a tool, configuring auth, or sourcing an env - and your main script output never appears. The failing command is in before_script (or a default:before_script).

Job log
$ apt-get install -y somecli
E: Unable to locate package somecli
Cleaning up project directory and file based variables
ERROR: Job failed: exit code 100

Common causes

A setup command fails

Installing a package, logging into a registry, or generating config in before_script returns non-zero, and GitLab stops the job immediately.

Inherited default:before_script breaks

A global default:before_script (or one from an include/extends) runs before every job. A change there can break jobs that never declared a before_script themselves.

How to fix it

Fix or guard the setup step

Make setup commands robust and idempotent so they do not fail the whole job.

.gitlab-ci.yml
before_script:
  - apt-get update
  - apt-get install -y curl jq
  - command -v aws >/dev/null || pip install awscli

Isolate where it failed

  1. Note that before_script runs in the same shell as script - the failing line is in the setup block.
  2. Check whether a global default:before_script or an included template is the source.
  3. Override before_script: [] in a job to opt out of an inherited setup that does not apply.

How to prevent it

  • Keep before_script minimal, idempotent, and non-interactive.
  • Pin tool versions so setup does not break on upstream changes.
  • Test changes to shared default:before_script against multiple jobs.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →