'Adds needs relations to GitLab CI yaml but got an error: the job was not added to the pipeline

I am trying to add needs between jobs in the Gitlab CI yaml configuration file.


stages:
  - build
  - test
  - package
  - deploy

maven-build:
  stage: build
  only:
    - merge_requests
    - master
    - branches
  ...
test:
  stage: test
  needs: [ "maven-build" ]
  only:
    - merge_requests
    - master
  ...
docker-build:
  stage: package
  needs: [ "test" ]
  only:
    - master
  ...
deploy-stage:
  stage: deploy
  needs: [ "docker-build" ]
  only:
    - master
  ...
deploy-prod:
  stage: deploy
  needs: [ "docker-build" ]
  only:
    - master
  when: manual
  ...

I have used the GitLab CI online lint tools to check my syntax, it is correct.

But when I pushed the codes, it always complains:


    'test' job needs 'maven-build' job
    but it was not added to the pipeline

You can also test your .gitlab-ci.yml in CI Lint

The GitLab CI did not run at all.

Update: Finally I made it. I think the needs position is sensitive, move all needs under the stage, it works. My original scripts included some other configuration between them.



Solution 1:[1]

CI-jobs that depend on each other need to have the same limitations!

In your case that would mean to share the same only targets:

stages:
  - build
  - test

maven-build:
  stage: build
  only:
    - merge_requests
    - master
    - branches

test:
  stage: test
  needs: [ "maven-build" ]
  only:
    - merge_requests
    - master
    - branches

that should work from my experience^^

Solution 2:[2]

Finally I made it. I think the needs position is sensitive, move all needs under the stage, it works

Actually... that might no longer be the case with GitLab 14.2 (August 2021):

Stageless pipelines

Using the needs keyword in your pipeline configuration helps to reduce cycle times by ignoring stage ordering and running jobs without waiting for others to complete.

Previously, needs could only be used between jobs on different stages.

In this release, we’ve removed this limitation so you can define a needs relationship between any job you want.

As a result, you can now create a complete CI/CD pipeline without using stages by including needs in every job to implicitly configure the execution order.
This lets you define a less verbose pipeline that takes less time to create and can run even faster.

https://about.gitlab.com/images/14_2/need.png -- Stageless pipelines

See Documentation and Issue.

Solution 3:[3]

The rule in both jobs should be that same or otherwise GitLab cannot create job dependency between the jobs when the trigger rule is different.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dniwdeus
Solution 2 VonC
Solution 3 ayakout