C.W.K.
Stream
Lesson 01 of 14 · published

Job Dependencies — needs:

~11 min · needs, dag, ordering

Level 0Apprentice
0 XP0/101 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

The DAG that needs: draws

Each job declares zero or more upstream jobs via needs:. GitHub forms a DAG out of these and runs the jobs in topological order. Jobs with no path between them run in parallel.

Common shapes:

  • Linear — A → B → C. Each waits on the previous.
  • Fan-out, fan-in — A → [B, C, D] → E. B/C/D run in parallel after A, then E waits on all three.
  • Diamond — A → [B, C] → D. Common for build-then-(test, lint)-then-deploy.
  • Disjoint chains — independent pipelines living in the same workflow.

Failure propagation

By default, a job is skipped if any of its needs: failed or was cancelled. You can override with if: always() or if: failure() for cleanup / notification jobs that must run anyway.

Code

Diamond DAG with always-run notify·yaml
jobs:
  build:
    runs-on: ubuntu-latest
    steps: [{ uses: actions/checkout@v4 }, { run: ./build.sh }]

  test:
    needs: build
    runs-on: ubuntu-latest
    steps: [{ uses: actions/checkout@v4 }, { run: ./test.sh }]

  lint:
    needs: build
    runs-on: ubuntu-latest
    steps: [{ uses: actions/checkout@v4 }, { run: ./lint.sh }]

  deploy:
    needs: [test, lint]
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps: [{ uses: actions/checkout@v4 }, { run: ./deploy.sh }]

  notify:
    needs: [test, lint, deploy]
    if: always()       # runs even if upstream failed/skipped
    runs-on: ubuntu-latest
    steps:
      - run: ./notify-slack.sh "${{ needs.deploy.result }}"

External links

Exercise

Take a workflow with serial steps and refactor it into a diamond DAG: build → (test, lint) → deploy. Time the wall-clock improvement and document it in the PR description.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.