Job Graphs, Conditions, Caches & Artifacts
A workflow's jobs form a graph: needs draws the edges, if: decides whether a job runs at all, outputs and artifacts carry data along the edges, and caches carry data between runs. This lesson puts them together.
needs, outputs and if
jobs:
lint:
runs-on: ubuntu-24.04
steps: [{ uses: actions/checkout@v7 }, { run: make lint }]
test:
runs-on: ubuntu-24.04
strategy:
fail-fast: false # let the other Python versions finish
matrix:
python: ['3.12', '3.13']
steps:
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with: { python-version: '${{ matrix.python }}' }
- run: make test
build:
needs: [lint, test] # waits for both, all matrix legs included
runs-on: ubuntu-24.04
outputs:
version: ${{ steps.v.outputs.version }}
steps:
- uses: actions/checkout@v7
- id: v
run: echo "version=1.4.$GITHUB_RUN_NUMBER" >> "$GITHUB_OUTPUT"
- run: make build VERSION=${{ steps.v.outputs.version }}
- uses: actions/upload-artifact@v7
with:
name: dist
path: dist/
retention-days: 7
deploy-staging:
needs: build
if: github.ref == 'refs/heads/main' # only from main
runs-on: ubuntu-24.04
steps:
- uses: actions/download-artifact@v8
with: { name: dist, path: dist }
- run: ./ci/deploy.sh staging "${{ needs.build.outputs.version }}"
notify-failure:
needs: [lint, test, build, deploy-staging]
if: failure() # runs only when something above failed
runs-on: ubuntu-24.04
steps:
- run: echo "pipeline failed on $GITHUB_REF_NAME" # e.g. post to chatMatrix expansion (include, exclude, max-parallel) is modelled step by step in Matrix Builds.
Status functions
A job's if: without a status function implicitly means success() && …: it runs only if every job it needs succeeded. The functions change that:
| Function | True when |
|---|---|
success() |
all needed jobs succeeded (the default) |
failure() |
any needed job (or ancestor) failed |
always() |
always, even when the run was cancelled; use for clean-up and report upload |
cancelled() |
the run was cancelled |
The model below evaluates a small graph with these rules, including the subtle one: a job skipped because its dependency failed counts as "not successful" for the jobs after it, so skips spread down the graph:
JOBS = { # name: (needs, if-condition)
"lint": ([], None),
"test": ([], None),
"build": (["lint", "test"], None),
"deploy-staging": (["build"], "main"),
"report": (["test"], "always()"),
"notify-failure": (["lint", "test", "build", "deploy-staging"], "failure()"),
}
def ancestors(name):
found = set()
for n in JOBS[name][0]:
found |= {n} | ancestors(n)
return found
def run(outcomes, ref):
result = {}
for name, (needs, cond) in JOBS.items(): # dict order is a valid topological order here
deps = [result[n] for n in needs]
all_ok = all(d == "success" for d in deps)
if cond == "always()":
go = True
elif cond == "failure()": # any ancestor failed
go = any(result[a] == "failure" for a in ancestors(name))
elif cond == "main":
go = all_ok and ref == "main"
else:
go = all_ok
result[name] = outcomes.get(name, "success") if go else "skipped"
return result
for title, outcomes, ref in [
("all green on main", {}, "main"),
("test fails on main", {"test": "failure"}, "main"),
("all green on a PR", {}, "pull/42"),
]:
print(title)
for name, state in run(outcomes, ref).items():
print(f" {name:<15} {state}")all green on main
lint success
test success
build success
deploy-staging success
report success
notify-failure skipped
test fails on main
lint success
test failure
build skipped
deploy-staging skipped
report success
notify-failure success
all green on a PR
lint success
test success
build success
deploy-staging skipped
report success
notify-failure skipped
When test failed, build and deploy-staging were skipped, report still ran (always()), and notify-failure ran (failure()). Use always() for uploading reports and cleaning up, and !cancelled() when you want "always, except when someone pressed Cancel".
Artifacts vs caches
| Artifacts | Caches | |
|---|---|---|
| Purpose | pass results between jobs of one run; keep evidence (reports, binaries) | reuse inputs between runs (downloaded dependencies) |
| Actions | actions/upload-artifact@v7 / actions/download-artifact@v8 |
actions/cache@v6, or cache: in setup-node/setup-python |
| Lifetime | retention days (default 90, configurable) | evicted when unused for a week, or when the repository's cache quota is full |
| Correctness | must be exactly what the producer made | a miss or stale hit must not change the result |
The cache key logic (lockfile hash + restore prefixes) is modelled in Build Verification & Fast Feedback, and Caching & Artifacts shows the full actions/cache syntax. One security rule: caches written by pull-request runs are scoped to that PR's ref and can't poison main, but a cache restored from the base branch into a PR is readable there; never cache secrets.
Useful job-level keys
| Key | Use |
|---|---|
timeout-minutes |
default 360; set it low (10–30) so a hung job fails fast |
continue-on-error: true |
the job (or step) may fail without failing the run; good for experimental matrix legs |
concurrency |
one deploy at a time (Environments, Approvals & Concurrency) |
defaults.run.shell, working-directory |
per-job script defaults |
services |
sidecar containers such as Postgres (Tests in CI) |
container |
run all steps inside a container image on a Linux runner |
Test yourself
test fails. What happens to a job with needs: build (and no if:) when build needs test?
- It runs
- It is skipped: build was skipped, so its dependents are skipped too
- It fails
- It waits forever
Which condition makes a job upload test reports even when tests failed?
if: success()if: always()(or!cancelled())if: failure() && success()- No
if:
You need the built binary in the deploy job of the same run. Artifact or cache?
- Cache
- Artifact
- Either
- Neither: rebuild