AI Hints
CI/CD with GitHub Actions · Lesson 44 of 138

needs

Jobs in order.

⏱ 2 min read▶ Video at 50:27🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

Some jobs must wait. There's no point building a package if the tests failed, and you should never deploy a broken build. The needs key makes a job wait for other jobs, and skips it if they fail.

needs
From the video: needs
  • Some jobs must wait
  • Don't build broken code
  • needs

A chain of jobs

Build needs test, so it waits for the tests to pass. Deploy needs both test and build. Together they form a chain. Test, then build, then deploy. GitHub draws this as a graph on the run's page, with arrows between the jobs.

.github/workflows/pipeline.ymlyaml
name: Pipeline
on: push
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - run: pytest -q
  build:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - run: echo "Building the package"
  deploy:
    needs: [test, build]
    runs-on: ubuntu-latest
    steps:
      - run: echo "Deploying"

When the tests fail

First, the happy path. All three jobs succeed, in order. Then we break the tax rate. The tests fail, and build and deploy are both skipped, because a job they need failed. The broken code never gets anywhere near users.

Terminal
run-steps .github/workflows/pipeline.yml | grep -E "^Job"
Job: test
Job: build
Job: deploy
sed -i 's/tax=0.2/tax=0.3/' app.py && git commit -qam Oops
run-steps .github/workflows/pipeline.yml | grep -E "Job|succeeded|failed|skipped"
Job: test
      1 failed, 2 passed in 0.01s
  Job failed
Job: build
  - skipped, because a job it needs failed
Job: deploy
  - skipped, because a job it needs failed

Summary

Needs makes a job wait for others, and skips it if they fail. That's how pipelines stop bad code. Next, passing values between jobs.

Key takeaways
  • needs: test
  • needs: [a, b]
  • Skipped on failure
Next up: Job Outputs — values across machines