Jobs and Job Dependencies
What you'll learn: How to organize your GitHub Actions workflows into discrete jobs and control their execution order using the needs keyword.
Understanding Jobs
A job is a collection of steps that run on the same runner (virtual machine). By default, workflows can contain multiple jobs that run in parallel, letting you execute different tasks simultaneously—like running tests for multiple platforms at once or performing independent build steps.
Think of jobs like assembly line stations: each station (job) performs specific tasks, and you can have multiple stations working at the same time or arrange them so one waits for another to finish.
Defining Jobs in a Workflow
Jobs live under the jobs: key in your workflow file. Each job gets a unique identifier:
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo "Building application"
test:
runs-on: ubuntu-latest
steps:
- run: echo "Running tests"
Without dependencies, build and test run simultaneously.
Controlling Order with Dependencies
The needs keyword creates dependencies between jobs. A job with needs waits for its dependencies to complete successfully before starting:
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo "Building..."
test:
needs: build
runs-on: ubuntu-latest
steps:
- run: echo "Testing after build"
deploy:
needs: [build, test]
runs-on: ubuntu-latest
steps:
- run: echo "Deploying after both complete"
Here, test waits for build, and deploy waits for both. This creates a sequential workflow where each stage depends on the previous one's success.
Key Takeaway: Jobs let you organize workflow steps into logical units; use the needs keyword to create explicit dependencies that control execution order and ensure tasks run only after their prerequisites succeed.