Workflow Anatomy
Read and write every part of a workflow file with confidence.
Now letβs dissect a workflow file part by part. Once you can read each section, you can build almost any pipeline you need.
π― Learning Objectives
By the end of this lesson you will:
- Control when a workflow runs with triggers
- Structure jobs and steps correctly
- Run a job across multiple versions with a matrix
- Speed up runs with caching
Triggers β the on Key
The on key decides what events start the workflow:
on:
push:
branches: [main] # only pushes to main
pull_request:
branches: [main] # PRs targeting main
schedule:
- cron: "0 6 * * *" # every day at 06:00 UTC
workflow_dispatch: # a manual "Run" button| Trigger | Fires when |
|---|---|
push |
Commits are pushed |
pull_request |
A PR is opened/updated |
schedule |
On a cron schedule |
workflow_dispatch |
You click βRun workflowβ manually |
π‘ Filter to what matters
Restrict triggers with branches, paths, or tags so workflows only run when relevant β this saves runner minutes and avoids noise.
Jobs and Steps
A workflow has one or more jobs; each job has ordered steps:
jobs:
test:
runs-on: ubuntu-latest # the runner OS
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci # install
- run: npm test # testBy default, jobs run in parallel. Steps within a job run in order, sharing the same runner and filesystem.
Job Dependencies
Use needs to make one job wait for another β essential for build β deploy ordering:
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo "building"
deploy:
needs: build # only runs if build succeeds
runs-on: ubuntu-latest
steps:
- run: echo "deploying"β Jobs don't share files automatically
Each job runs on a fresh runner. To pass files (like build output) from one job to another, upload them as an artifact in the first job and download them in the next. Steps within the same job do share files.
Matrix Builds
A matrix runs the same job across multiple configurations in parallel β perfect for testing several language versions:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node: [18, 20, 22]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm testThis automatically creates three parallel jobs β one each for Node 18, 20, and 22.
Caching for Speed
Reinstalling dependencies every run is slow. Cache them to speed things up dramatically:
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm' # cache the npm dependency storeπ‘ Expressions and context
The double-brace expression syntax is how workflows read context values β the commit SHA, a matrix value, or a secret β and inject them into steps. Youβll see it used constantly in the pipelines ahead.
π§ͺ Hands-on Lab
Build a Matrix Test Workflow
- Write a workflow triggered on
pushtomainand onpull_request - Add a
testjob with a matrix of two Node versions - Add a
deployjob thatneedsthe test job
π§ Knowledge Check
By default, how do multiple jobs in a workflow run?
What is a matrix build used for?
πΌ Interview Preparation
How do you pass build artifacts between jobs in GitHub Actions?
Summary
You can now read and write every part of a workflow: triggers, jobs, steps, needs dependencies, matrix builds, and caching. Next, youβll assemble these into a complete build-and-test CI pipeline for a real project.