🎓 A1Learner
🔍
0 of 50 lessons completed

GitHub Actions: Chaining Jobs with needs

Jobs run at the same time unless one job needs another. The needs keyword chains jobs in order.

Two jobs at once

With no needs line, both jobs start together.

jobs:
  test:
    runs-on: ubuntu-latest
  build:
    runs-on: ubuntu-latest

Build waits for test

The needs line chains the build job after the test job.

  build:
    needs: test
    runs-on: ubuntu-latest

A failed test skips the build

When the test job fails, the build job never starts.

  build:
    needs: test

Waiting for two jobs

A list names more than one job to wait for.

  deploy:
    needs: [test, build]

Example

name: Chained Jobs
on: push
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: '24'
      - run: npm test
  build:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: '24'
      - run: npm run build

Key points

  • Use needs to say a job must wait for another job.
  • A job with needs starts only after the named job passes.
  • A failed job skips every job that needs it.
  • Jobs with no needs run at the same time.
  • Chaining saves work: a broken build never runs on broken tests.
  • Needs is the link between jobs, stated in plain words.

In this example, the workflow is saved as .github/workflows/chained-jobs.yml. The test job runs first. The build job has a needs line naming test, so it waits for the tests to pass. The build job fetches the code again on its own fresh runner, sets up Node 24, and runs the build. The output shows both tests passing and then the build message.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

The test job fails. What happens to a build job with needs: test?