TIL: Gitlab CI: The needs Keyword#

Gitlab CI has the needs keyword since 2021. It allows building stageless pipelines, but has many uses.

Example (before)#

Sometimes we run different test suites in parallel (e.g. unit-tests and integration tests), but want aggregated coverage results. This results in a pipeline like this:

It would be nicer, if we had a single stage called “test” that contains all of theses jobs.

Example (after)#

Using needs, our .gitlab-ci.yml looks like this:

service-tests-unit:
  ...

service-tests-integration:
  ...

coverage:
  stage: test
  needs:
    - job: service-tests-unit
    - job: service-tests-integration

This results in a single stage, where coverage depends on tests: