Jobs, Steps and Matrix Builds
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
A workflow is a set of jobs; a job is an ordered list of steps on one runner. Splitting work into jobs buys parallelism, isolation (each job gets a clean machine) and the ability to use different runners; the price is that data has to be passed explicitly. This page covers dependencies between jobs, everything a step can declare, service containers, matrices and the mechanisms for moving data around. Trigger syntax and expressions are on Workflow Syntax and Triggers.
Jobs and needs
By default every job in a workflow starts at once. needs creates an ordering: a job waits for all jobs it lists, and is
skipped if any of them failed or was skipped (unless an if: says otherwise).
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- run: npx markdownlint-cli2 "**/*.md"
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- run: npm test
build:
needs: [lint, test]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- run: npm run build
deploy:
needs: build
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- run: echo "deploying"
Cycles are rejected when the workflow is parsed. The graph appears on the run page. For this site, a sensible split of
publish.yml is a build job (install, validate Mermaid, run Antora) followed by a deploy job that only runs from
the default branch; the build output travels between them as an artifact (see below).
Running regardless of earlier failures
notify:
needs: [build, deploy]
if: always() && (needs.build.result == 'failure' || needs.deploy.result == 'failure')
runs-on: ubuntu-latest
steps:
- run: echo "pipeline failed"
needs.<id>.result is success, failure, cancelled or skipped. always() keeps the job from being skipped
because of an upstream failure. Use cancelled() or failure() when you do want a more specific condition.
Job outputs
A job can expose strings produced by its steps to later jobs:
jobs:
prepare:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.v.outputs.version }}
is-release: ${{ steps.v.outputs.release }}
steps:
- id: v
run: |
echo "version=$(date +%Y.%m.%d)-${GITHUB_RUN_NUMBER}" >> "$GITHUB_OUTPUT"
echo "release=${{ startsWith(github.ref, 'refs/tags/v') }}" >> "$GITHUB_OUTPUT"
use:
needs: prepare
if: needs.prepare.outputs.is-release == 'true'
runs-on: ubuntu-latest
steps:
- run: echo "Version is ${{ needs.prepare.outputs.version }}"
Outputs are always strings and are limited in size, so use them for identifiers, versions, flags and JSON, and artifacts for files. A value that looks like a secret is masked and dropped when passed between jobs.
Steps
A step has either uses or run, never both:
steps:
- name: Checkout
uses: actions/checkout@v7
with:
fetch-depth: 0 # input for the action
- name: Build
id: build # lets later steps read steps.build.*
if: github.event_name != 'schedule'
shell: bash # bash, sh, pwsh, python, cmd, powershell
working-directory: ./site
env:
NODE_ENV: production # step-level env, wins over job and workflow env
run: |
npm ci
npm run build
continue-on-error: false # true marks the step green even if it fails
timeout-minutes: 10
| Key | Meaning |
|---|---|
|
Label in the log; |
|
An action: |
|
Inputs to the action; |
|
A shell script. On Linux and macOS the default shell is |
|
Per step; defaults can be set with |
|
Environment variables for this step only. |
|
Condition; skipped steps count as success for the job. |
|
Do not fail the job when this step fails (the step’s |
|
Cancel the step after this many minutes. |
A defaults.run block avoids repetition when most steps share a directory:
jobs:
site:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
working-directory: docs
steps:
- uses: actions/checkout@v7
- run: npm ci # runs in docs/
defaults.run applies only to run steps, not to uses steps.
Environment variable scope
env can appear at workflow, job and step level; the innermost wins. An env value can use expressions and the
github, secrets, vars, needs, inputs and matrix contexts (depending on level), but it cannot reference
another variable from the same env block. Values written to GITHUB_ENV are visible to later steps of the same job
only.
Timeouts and conditions at job level
jobs:
build:
if: github.repository == 'albertoirurueta/docs' # skip on forks
timeout-minutes: 30 # a job otherwise runs up to the platform maximum
runs-on: ubuntu-latest
steps: [ { run: echo ok } ]
Always set timeout-minutes below the platform default: a hung job otherwise burns minutes for hours. A job-level if
is evaluated before the job is scheduled, so it cannot use the steps context or secrets.
Service containers
A job can start Docker containers beside the steps, on the same network, and wait until they are healthy. They only work on Linux runners.
jobs:
integration:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:17
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: app
ports:
- 5432:5432
options: >-
--health-cmd "pg_isready -U postgres"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v7
- uses: actions/setup-java@v6
with:
distribution: temurin
java-version: 21
- name: Run integration tests
run: ./mvnw -B verify
env:
DB_URL: jdbc:postgresql://localhost:5432/app
When steps run directly on the runner, the service is reachable at localhost on the mapped port (ports maps
container to host; 5432:5432 here, or 5432 for a random host port, read back as
job.services.postgres.ports['5432']). When the job itself runs in a container, use the service name (postgres) as the
host and the container’s port. The password above is a throwaway value for a container that lives only for the job;
real credentials belong in secrets.
Container jobs
jobs:
build:
runs-on: ubuntu-latest
container:
image: node:24-bookworm
options: --cpus 2
steps:
- uses: actions/checkout@v7
- run: node --version && npm ci && npm test
All steps run inside the image, which gives a pinned toolchain independent of the runner image. The image needs git
for actions/checkout to behave fully, and some actions assume glibc-based images. Prefer a plain setup-* action on
the runner unless you need an exact OS userland.
Matrix builds
strategy.matrix expands one job definition into many, one per combination of its axes.
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false # let other combinations finish when one fails
max-parallel: 4 # cap simultaneous jobs
matrix:
os: [ubuntu-latest, windows-latest]
java: [17, 21, 25]
include:
- os: ubuntu-latest
java: 21
coverage: true # extra key for one combination
- os: ubuntu-24.04-arm
java: 21 # a brand-new combination
exclude:
- os: windows-latest
java: 17
steps:
- uses: actions/checkout@v7
- uses: actions/setup-java@v6
with:
distribution: temurin
java-version: ${{ matrix.java }}
cache: maven
- run: ./mvnw -B verify
- if: matrix.coverage
run: ./mvnw -B jacoco:report
-
The default
fail-fast: truecancels the remaining combinations as soon as one fails; turn it off to see the full picture of which versions break. -
includeentries that match an existing combination add keys to it; entries that do not match create a new combination.excludeis applied first, theninclude. -
A matrix generates a documented maximum number of jobs per workflow run, so keep axes small.
-
A job in a matrix is named after its values (
test (ubuntu-latest, 21)); branch protection rules must require each name, or require a final aggregating job (see below).
A Node variant for the docs tooling follows the same pattern:
strategy:
matrix:
node: [22, 24]
steps:
- uses: actions/setup-node@v7
with:
node-version: ${{ matrix.node }}
cache: npm
- run: npm ci && npm test
Dynamic matrices
Compute the matrix in one job and consume it with fromJSON:
jobs:
discover:
runs-on: ubuntu-latest
outputs:
modules: ${{ steps.find.outputs.modules }}
steps:
- uses: actions/checkout@v7
- id: find
run: |
modules=$(ls -d modules/*/ | xargs -n1 basename | jq -R . | jq -sc .)
echo "modules=$modules" >> "$GITHUB_OUTPUT"
check:
needs: discover
runs-on: ubuntu-latest
strategy:
matrix:
module: ${{ fromJSON(needs.discover.outputs.modules) }}
steps:
- run: echo "checking ${{ matrix.module }}"
Guard against an empty list (a matrix with no combinations fails the workflow) with an if: on the consuming job.
Passing data between jobs
| Mechanism | Use for | Limits |
|---|---|---|
Job |
Short strings: versions, flags, JSON |
Strings only, small total size |
Artifacts ( |
Files and directories, such as |
Storage counts toward quota; retention is configurable |
Cache |
Dependencies reused across runs |
Best effort, not a hand-off mechanism |
build:
steps:
- run: npx antora antora-playbook.yml
- uses: actions/upload-artifact@v7
with:
name: site
path: build/site
if-no-files-found: error
deploy:
needs: build
steps:
- uses: actions/download-artifact@v8
with:
name: site
path: build/site
Details, retention and caching strategies are on Caching and Artifacts.
Job summaries
Writing Markdown to $GITHUB_STEP_SUMMARY adds a formatted section to the run page, which is a better place for
results than the raw log:
- name: Summarize
if: always()
run: |
{
echo "### Antora build"
echo "| Pages |"
echo "|---|"
echo "| $(find build/site -name '*.html' | wc -l) |"
} >> "$GITHUB_STEP_SUMMARY"
Common mistakes
-
Expecting files, environment variables or installed tools from one job to exist in another.
-
Forgetting that a job whose
needsfailed is skipped, so cleanup and notification jobs needif: always(). -
Using a matrix with
fail-fast: trueand misreading cancelled jobs as failures. -
Requiring individual matrix job names in branch protection, then renaming an axis value.
-
Reaching a service container at the wrong host:
localhostfor runner steps, the service name for container jobs. -
Leaving out
timeout-minutesand paying for a stuck job.