GitLab needs and artifacts: preserve required evidence when jobs run early
Use needs to express GitLab job dependencies and artifact transfer. Distinguish optional producers, required release gates and caches that cannot serve as release evidence.
TL;DR:
needslets a GitLab job start after its declared prerequisites instead of waiting for a whole earlier stage. Declare the producer and artifact transfer explicitly, and keep mandatory checks mandatory when rules remove jobs from a pipeline.
Earlier execution changes the evidence boundary
Stage order is a broad barrier. A dependency graph can start a packaging job as soon as its own tests finish, while unrelated tests continue. The performance benefit is real only if every prerequisite required for that consumer remains represented.
A job using needs downloads artifacts from the jobs named in its needs configuration, with artifact transfer controlled by the artifacts option. Do not assume it receives outputs from every preceding stage. GitLab's needs guide and YAML reference define this behavior.
Transfer a particular producer's result
This pipeline fragment demonstrates a build-output dependency using only shell operations. Run it in a disposable GitLab project with an available compatible runner to validate platform behavior.
stages: [build, verify]
bundle:
stage: build
script:
- mkdir -p dist
- printf 'candidate-17\n' > dist/release.txt
artifacts:
paths: [dist/release.txt]
inspect:
stage: verify
needs:
- job: bundle
artifacts: true
script:
- test "$(cat dist/release.txt)" = 'candidate-17'
The expected result is a successful comparison in a separate job workspace. Change the expected value to candidate-18: inspect must fail. Disable artifact transfer and the consumer must fail because the file is absent in a clean workspace. The local shell comparison can be tested separately, but that does not prove GitLab transferred the artifact.
| Mechanism | Purpose | Unsuitable assumption |
|---|---|---|
needs edge | Scheduling dependency | It implicitly includes every release check |
| Job artifact | Producer's retained output | Its contents are automatically trusted |
| Cache | Reusable acceleration data | It identifies the reviewed release |
optional: true | Tolerate an absent producer | A required scan may disappear safely |
Absence is a policy decision
Imagine a release job requires a security report, but rules:changes excludes the scan for a documentation-only diff. If release is still possible, either the scan must be present or the release must be excluded too. Treat “no scan job” separately from “scan succeeded.” Making the edge optional repairs graph creation by weakening the requirement.
Optional needs are suitable when the consumer can correctly proceed without that producer. A site preview may attach screenshots when available while remaining useful without them. State that contract explicitly and verify both cases.
Also distinguish missing artifacts from an absent job. A producer can run successfully without publishing the expected path, or retained artifacts may no longer be available when a delayed consumer starts. Inspect producer logs, artifact paths, retention and consumer download results before changing the dependency structure.
Self-check: a deployment starts after unit-tests succeeds but while a required license check still runs. Is that a runner scheduling bug? Usually the graph permitted it. Add the required dependency and test that a failed license check prevents promotion, while preserving concurrency for unrelated work.
Try a pipeline with a disappearing scan and compare it with the artifact promotion model.