CI fails if the job needed by a job not created due to rules

Hi,

I am on Gitlab CE 14.1. In my .gitlab.ci.yml I define two jobs: A and B. The job B needs the job A. I add rules to A and it runs if some files change in the repo. I also want to skip B if A is not created but in those cases CI gives YAML error saying that B needs A but A doesn’t exist.

So, I was thinking that the problem should have been solved in 14.1: In GitLab 13.9 and older, if needs: refers to a job that might not be added to a pipeline because of only, except, or rules, the pipeline might fail to create. ref But it looks like this isn’t the case. A workaround may be copying same rules on both A and B but this isn’t a scalable and easily maintainable solution.

What would be a proper solution?

Thank you!
Alper.

Hi @ayazar
use the optional keyword as described here

This doesn’t solve it, because optional will mean that B is run. But we don’t want B to run, and we don’t want for the pipeline to fail.

Just ran into the same issue. Definitely looks like a bug!

1 Like

Was this ever fixed?

Hi,

I would appreciate it if you could provide some kind of minimal setup.
Sadly, the post is a bit old, but the solution would be simple, I think.

If you add rules in job A, then you should add it in job B as well. If both jobs depend on one another, I would not see any reason not to do so.

This is what your case could look like:

job-a:
  script:
    - exit 0
  rules:
    - if: $CI_COMMIT_BRANCH == main

job-b:
  needs: ["job-a"]
  script:
    - exit 0

job-b might miss job-a because the pipeline was created in a different branch. So I would just add the rules on job-b as well.

job-b:
  needs: ["job-a"]
  script:
    - exit 0
  rules:
    - if: $CI_COMMIT_BRANCH == main