Improve the error message when no pipeline is created if jobs with `needs` can't run

Summary

If a pipeline has one or more jobs with needs keyword and that jobs cannot be run due to rules or any conditions, then a pipeline is not created at all and the error message is misleading

This is closely related to the issue: #224958 (closed), but, it only talks about schedules, and it ignores the fact that a pipeline should be created with rest of the jobs that satisfy the conditions.

Steps to reproduce

  1. Create a pipeline with following .gitlab-ci.yml:
stages:          
  - build
  - test

build-job:       
  stage: build
  script:
    - echo "build"

unit-test-job:   
  stage: test    
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: always
  script:
    - echo "unit-test-job"

lint-test-job:   
  stage: test    
  needs:
      - build-job
      - unit-test-job
  script:
    - echo "lint-test-job"
  1. This will not create a pipeline and shows yaml invalid error. Reason: 'lint-test-job' job needs 'unit-test-job' job, but 'unit-test-job' is not in any previous stage.
  2. Here, the build-job job should have run as it does not have any rules.
  3. Also, as we can now use needs in the same stage, the error message is not in any previous stage does not hold true in all cases.

Example Project

Project: https://gitlab.com/psureshbabu/no-pipeline-without-needs/ Pipeline: https://gitlab.com/psureshbabu/no-pipeline-without-needs/-/pipelines/909649498

What is the current bug behavior?

error message is not clear

What is the expected correct behavior?

Provide a clear error message which indicates that a needed job was not created and point users to optional: need section in our documentation

Error/log entry here: is not in any previous stage

Relevant logs and/or screenshots

Output of checks

This bug happens on GitLab.com

Possible fixes

Edited by Dov Hershkovitch