DevOps & CI/CD Bug Report

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.

Grace Liu Grace Liu @graceliu · November 29, 2026 · 7 min read · 0 Views
640-gitlab-ci-needs-dag-artifacts-not-downloaded

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 week and were visible on the job page, downloadable by hand.

  • Wrong path. dist/server/index.js existed in the artifact archive from build-web.

  • Runner differences. The job failed on every runner in the pool.

Root cause

  1. The e2e job declared needs: [build-api]. Before, it ran in the last stage and automatically downloaded artifacts from all jobs in earlier stages, including build-web. With needs:, a job downloads artifacts only from the jobs listed in its needs. build-web was not listed, so dist/ never arrived.

  2. e2e also needed lint-openapi, which had rules: 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 needs:

Start condition

All earlier stages done

Only listed jobs done

Artifacts downloaded

All jobs in earlier stages (or dependencies:)

Only listed jobs, unless artifacts: false

Missing upstream job

Not an error

Pipeline validation error, unless optional: true

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

needs: added to 9 jobs in one change

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 of needs:. 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:job for cross-pipeline artifacts keeps each graph readable.

How to test the fix

  1. Open a merge request: confirm e2e starts right after build-api and build-web, and the job log shows both artifact downloads:

Downloading artifacts for build-api (8123456781)...
Downloading artifacts for build-web (8123456782)...
  1. Push a tag: the pipeline must be created, and e2e must run without lint-openapi.

  2. 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: false for 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

Add a label

#gitlab-ci #pipelines #artifacts #needs #dag
Written by
Grace Liu @graceliu

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.

Advertisement

Comments

0

No comments yet. Be the first to comment!

Sign In to post a comment.