Today I learned about optional needs which a cool feature introduced to gitlab ci, months ago. If you have a job that needs another job, and the latter can be missing due to if or only directives, you can mention it as an optional dependency.

This useful in a lot of usecases. I needed it for a job that depends on two others one of them is should be optional.

job1: 
  if: "$CI_COMMIT_BRANCH == master"
job2:
  needs:
    - job: job1
      optional: true

In the example above, if the commit branch is not master job2 will still be run.

More on this issue and on the docs.