Course contentsShow
← Cloud and Infrastructure
Lesson 1747 of 2,98936. GitLab CI/CDFree lesson

Dependencies and Needs

Controlling job dependencies with dependencies and needs for DAG-based execution.

Dependencies and Needs in GitLab CI/CD

You'll learn: How to control job execution order and optimize pipeline performance using dependencies and needs keywords.

Understanding Job Relationships

By default, GitLab CI/CD jobs in different stages run sequentially: all jobs in stage 1 complete before stage 2 begins. But what if your test job only needs artifacts from one build job, not all of them? This is where dependencies and needs become powerful.

The dependencies Keyword

The dependencies keyword controls which artifacts a job downloads from previous stages:

build:job1:
  stage: build
  script: echo "output1" > file1.txt
  artifacts:
    paths: [file1.txt]

test:job:
  stage: test
  script: cat file1.txt
  dependencies:
    - build:job1  # Only downloads artifacts from build:job1

Think of it like this: You're organizing a potluck, but you only want dishes from specific friends, not everyone.

The needs Keyword (DAG Execution)

The needs keyword creates a directed acyclic graph (DAG)—jobs run as soon as their specified dependencies complete, ignoring stage order:

build:job1:
  stage: build
  script: make build1

test:fast:
  stage: test
  needs: [build:job1]  # Starts immediately after build:job1
  script: run-fast-tests

This dramatically speeds up pipelines by enabling parallel execution across stages.

Combining Both

Use needs for execution control and dependencies to limit artifact downloads:

deploy:
  stage: deploy
  needs: [build:job1, test:fast]
  dependencies: [build:job1]  # Run after both, download from one

Key Takeaway: Use dependencies to control artifact downloads and needs to create efficient DAG-based pipelines that break free from strict stage ordering, running jobs as soon as their specific prerequisites complete.