Sub topic 9
The dependency graph: rules and cycles
This chapter assumes you can read a workflow file and already know how needs orders jobs. Here you will learn the rules GitHub applies to the graph that needs builds, and the errors you get when you break them.
Jobs are nodes, needs are edges
Every job in a workflow is a node. Each entry in a needs list is a directed edge from the job that must finish first to the job that waits. GitHub reads all of these edges before it starts anything and checks that the result is a valid graph.
The graph must be a directed acyclic graph (DAG). Directed means each edge has a direction. Acyclic means you cannot follow the edges and return to where you started.
The rules
- Every name in
needsmust be a job ID defined in the same workflow file. A misspelled or missing ID makes the workflow invalid. - A job cannot list itself.
- The graph cannot contain a cycle, directly or through other jobs.
needsaccepts a single string or a list.needs: buildandneeds: [build]mean the same thing.- Dependencies are by job ID, which is the key under
jobs:. The job'sname:display label is not used. - A job with no
needsis a root and starts right away. The graph can have several roots.
Order in the file does not matter. A job may name a dependency that appears later in the file.
What a cycle looks like
Suppose build waits for deploy, and deploy waits for build. Neither could ever start.
name: Broken
on: [push]
jobs:
build:
needs: deploy
runs-on: ubuntu-latest
steps:
- run: echo "build"
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- run: echo "deploy"GitHub rejects this file when it validates the workflow. No run starts, and the error names the jobs involved in the circular dependency. The same happens with longer loops such as a needs b, b needs c, and c needs a.
Transitive dependencies
If deploy needs build, and build needs lint, then deploy also waits for lint indirectly. You do not need to list lint again. Listing it is harmless but redundant.
There is one catch. The needs context only contains the jobs you list directly. If deploy needs build only, then needs.lint.outputs.x is not available in deploy. To read outputs or results from lint, add it to the list.
Fixing a cycle
A cycle usually means two jobs share a concern that belongs in a third job. Break the loop by moving the shared work into a new upstream job and having both original jobs depend on it.
jobs:
prepare:
runs-on: ubuntu-latest
steps:
- run: echo "shared setup"
build:
needs: prepare
runs-on: ubuntu-latest
steps:
- run: echo "build"
deploy:
needs: [prepare, build]
runs-on: ubuntu-latest
steps:
- run: echo "deploy"Common errors
| Symptom | Likely cause |
|---|---|
| Workflow is invalid and no run starts | A cycle, a self-reference, or a `needs` entry with no matching job ID |
| `needs` entry not found | You used the job's `name:` label instead of its ID, or misspelled the ID |
| Expression for `needs.x.outputs` is empty | `x` is not listed in this job's `needs` |
Exercise
A workflow has jobs a, b and c. Job a needs c, job b needs a, and job c needs b. Does it run? If not, how do you fix it?
Show answer
It does not run, because a, b and c form a cycle (a waits for c, c waits for b, b waits for a) and GitHub rejects the workflow. Break the loop by removing one edge or by moving shared work into a new job that all three can depend on in one direction, for example making c a root job with no needs.