Jobs: Definition and Dependencies
What you'll learn: How to structure workflows into reusable jobs and control their execution order using dependencies.
What Are Jobs?
A job is a unit of work within a GitHub Actions workflow that groups one or more steps together. Each job runs in its own isolated environment (a fresh runner instance) and can execute independently or in coordination with other jobs.
Think of jobs like stations on an assembly line: each station performs specific tasks, and some stations must wait for others to finish before they can start their work.
Defining Jobs
Jobs are defined under the jobs: key in your workflow file. Each job needs a unique identifier:
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo "Building application"
test:
runs-on: ubuntu-latest
steps:
- run: echo "Running tests"
By default, all jobs run in parallel unless you specify otherwise.
Creating Dependencies with needs
The needs keyword establishes dependencies between jobs. When job B needs job A, job B waits until job A completes successfully:
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo "Building..."
test:
runs-on: ubuntu-latest
needs: build
steps:
- run: echo "Testing after build"
deploy:
runs-on: ubuntu-latest
needs: [build, test]
steps:
- run: echo "Deploying after build and test"
In this example, test waits for build, and deploy waits for both build and test to succeed.
Why Use Dependencies?
Dependencies let you control workflow logic: run tests only after successful builds, deploy only after tests pass, or parallelize independent tasks while serializing critical paths.
Key Takeaway: Jobs are isolated units of work in workflows; use the needs keyword to create execution dependencies and control the order in which jobs run.