GitLab needs: broke our pipeline: missing artifacts and undefined jobs
Adding needs: to speed up a GitLab pipeline made the e2e job lose files from earlier stages and made tag pipelines fail validation with a needs error. needs limits artifact downloads to listed jobs, and rules can remove a needed job. We fixed both with explicit artifact needs and optional needs.
Our GitLab pipeline had five stages and took 24 minutes, mostly waiting: unit tests for the API waited for the slow frontend build in the same stage. We added needs: to let jobs start as soon as their real dependencies were done. Wall time dropped to 13 minutes on the first merge request. Then two things broke.
The end-to-end job started failing on every branch:
$ npx playwright test
Error: Cannot find module '/builds/example-group/shop/web/dist/server/index.js'
ERROR: Job failed: exit code 1
And pipelines for version tags would not even start. The pipeline page showed a validation error instead of jobs:
'e2e' job needs 'lint-openapi' job, but 'lint-openapi' does not exist in the pipeline. This might be because of the only, except, or rules keywords. To need a job that sometimes does not exist in the pipeline, use needs:optional.
Reproducing it
Both failures were deterministic. Any pipeline with the new config reproduced the missing file; any tag pipeline reproduced the validation error.
What we ruled out
Artifact expiry. The build job's artifacts had
expire_in: 1 weekand were visible on the job page, downloadable by hand.Wrong path.
dist/server/index.jsexisted in the artifact archive frombuild-web.Runner differences. The job failed on every runner in the pool.
Root cause
The
e2ejob declaredneeds: [build-api]. Before, it ran in the last stage and automatically downloaded artifacts from all jobs in earlier stages, includingbuild-web. Withneeds:, a job downloads artifacts only from the jobs listed in itsneeds.build-webwas not listed, sodist/never arrived.e2ealso neededlint-openapi, which hadrules:limiting it to merge request pipelines. In tag pipelines that job does not exist, and GitLab refuses to create a pipeline where a job needs a job that is not there.
What is happening under the hood
Without needs:, GitLab orders jobs by stages:. A job starts when every job in the previous stages has finished, and by default it fetches artifacts from every successful job in those stages. You can narrow that with dependencies:.
With needs:, the job leaves the stage ordering and becomes a node in a directed acyclic graph (DAG). It starts when the listed jobs finish, regardless of stage, and three rules change:
Behaviour | Stage-ordered job | Job with |
|---|---|---|
Start condition | All earlier stages done | Only listed jobs done |
Artifacts downloaded | All jobs in earlier stages (or | Only listed jobs, unless |
Missing upstream job | Not an error | Pipeline validation error, unless |
needs: [] is a special case: the job starts immediately when the pipeline is created and downloads no artifacts. Since GitLab 14.2, a job can also need jobs in its own stage. A job can list up to 50 needs.
The validation error happens at pipeline creation time, after rules: are evaluated. That is why the same YAML worked in merge request pipelines and failed for tags: the needed job was removed only in tag pipelines.
Environment
Component | Version / value |
|---|---|
GitLab | GitLab.com (SaaS) |
Runner | GitLab-hosted Linux runners, Docker executor |
Pipeline | 5 stages: lint, build, test, e2e, deploy |
Pipeline sources | merge request pipelines, default-branch pipelines, tag pipelines |
Changed keyword |
|
Minimal reproduction
stages: [lint, build, test]
lint-openapi:
stage: lint
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
script: [npx @redocly/cli lint openapi.yaml]
build-api:
stage: build
script: [make api]
artifacts:
paths: [api/bin/]
build-web:
stage: build
script: [npm ci, npm run build]
artifacts:
paths: [web/dist/]
e2e:
stage: test
needs: [build-api, lint-openapi] # build-web missing, lint-openapi conditional
script:
- ls web/dist/server/index.js
- npx playwright test
Who is affected
Any GitLab pipeline that adds needs: to jobs that used to rely on implicit artifact download from earlier stages, or that needs jobs controlled by rules:, only: or except:. It shows up most often when one person converts a large stage-based pipeline to a DAG in a single change.
The fix
1. List every job whose files you use. We made the e2e job's needs explicit and split them into "wait for" and "download from":
e2e:
stage: test
- needs: [build-api, lint-openapi]
+ needs:
+ - job: build-api
+ - job: build-web
+ - job: lint-openapi
+ optional: true
+ artifacts: false
script:
- ls web/dist/server/index.js
- npx playwright test
optional: true tells GitLab to ignore the need when the job is absent from the pipeline. artifacts: false keeps ordering in merge request pipelines without downloading the lint report.
2. Stop jobs downloading what they do not use. Some jobs previously received every artifact from every earlier stage, which cost time on large archives. We set needs: with artifacts: false for pure ordering and listed only the producers each job reads from.
3. Validate every pipeline type before merging. The CI Lint tool in the GitLab UI and the API can simulate a pipeline for a given ref. We added a job that runs the lint for the default branch and for a tag:
for ref in main v0.0.0-ci-check; do
curl --silent --header "PRIVATE-TOKEN: $LINT_TOKEN" \
--data-urlencode "content@.gitlab-ci.yml" \
--data "dry_run=true" --data "ref=$ref" \
"https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/ci/lint" \
| jq -e '.valid' > /dev/null || { echo "CI config invalid for $ref"; exit 1; }
done
The ref used in a dry run must exist, so we keep a permanent v0.0.0-ci-check tag in the project for this purpose.
Alternatives and trade-offs
dependencies:instead ofneeds:. Keeps stage ordering and only narrows artifact downloads. Use it when you want fewer downloads but not a faster DAG.Remove the
rules:from the needed job. Simple, but the lint job then runs in pipelines where it was intentionally skipped.Pass files through the cache. Faster for big directories but not guaranteed; cache is best effort and runner-dependent. Build outputs that a later job must have belong in artifacts.
Parent-child pipelines. For very large graphs, a child pipeline per component with
needs:pipeline:jobfor cross-pipeline artifacts keeps each graph readable.
How to test the fix
Open a merge request: confirm
e2estarts right afterbuild-apiandbuild-web, and the job log shows both artifact downloads:
Downloading artifacts for build-api (8123456781)...
Downloading artifacts for build-web (8123456782)...
Push a tag: the pipeline must be created, and
e2emust run withoutlint-openapi.Open the pipeline's "Needs" view to visually confirm the DAG edges.
Confirming in production
Over the next two weeks, the e2e job passed on all branches, tag pipelines were created for three releases, and median pipeline duration stayed at about 13 minutes. Artifact download time across the pipeline fell by about two minutes because jobs stopped pulling archives they did not use.
Regression guard
The CI lint job above runs in every merge request that changes
.gitlab-ci.yml, for branch and tag refs.Each job that reads build output asserts its inputs exist before doing real work, with a message that points at
needs::
test -f web/dist/server/index.js || { echo "web/dist missing: is build-web listed in needs?"; exit 1; }
Pipeline config changes require review from the platform team via CODEOWNERS.
Security implications
None beyond normal pipeline hygiene. One point is worth checking during the change: narrowing artifact downloads with needs: reduces which jobs receive build outputs, which is good if some artifacts contain sensitive reports. Do not move secrets into artifacts to share them between jobs; artifacts are downloadable by anyone with access to the job.
Prevention checklist
When adding
needs:, list every job whose artifacts the job reads.Use
artifacts: falsefor ordering-only needs.Mark needs on rule-controlled jobs
optional: true.Lint the pipeline for every pipeline source you use (MR, default branch, tags, schedules).
Convert big pipelines to a DAG in small steps.
References
GitLab Docs, CI/CD YAML syntax reference, needs: https://docs.gitlab.com/ci/yaml/#needs
GitLab Docs, Job artifacts: https://docs.gitlab.com/ci/jobs/job_artifacts/
GitLab Docs, Pipeline architecture (DAG, parent-child): https://docs.gitlab.com/ci/pipelines/pipeline_architectures/
GitLab Docs, CI Lint API: https://docs.gitlab.com/api/lint/
Add a label
Embed this post
<iframe src="https://hibug.me/gitlab-ci-needs-dag-artifacts-not-downloaded" width="700" height="420" frameborder="0" loading="lazy"></iframe>
I am an application and cloud security engineer. Most of my day is spent with development teams: reviewing designs, untangling IAM policies, hardening AWS accounts and making security checks fast enough that nobody wants to skip them. I believe security works best when it is concrete. Instead of long lists of best practices, my posts look at one decision at a time: a policy that granted more than intended, a design reviewed before the first line of code, a deletion that did not delete. Each one ends with checks you can run on your own systems.
Related Stories
GitLab protected CI/CD variables empty in merge request pipelines
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.