Sub topic 3
Writing path glob patterns
This chapter assumes you can read a GitHub Actions workflow and know what on.push and on.pull_request do. It covers the glob syntax that paths and paths-ignore use, because most filter bugs come from a pattern that does not match what you expect.
What a glob matches
A glob is a pattern that matches file paths. GitHub compares each pattern to the full path of a changed file, relative to the repository root. A path such as services/api/src/main.go must match the whole pattern, not just part of it.
Three characters do most of the work:
| Pattern | Meaning | Example match |
|---|---|---|
| `*` | Zero or more characters, but never a `/` | `services/*.md` matches `services/README.md` |
| `**` | Zero or more characters, including `/` | `services/**` matches `services/api/src/main.go` |
| `?` | Exactly one character, not `/` | `v?.txt` matches `v1.txt` |
The difference between * and ** is the one to remember. A single * stays inside one directory level. ** crosses directory boundaries.
Patterns for common monorepo layouts
To cover a whole project, end the pattern with /**:
on:
pull_request:
paths:
- 'services/api/**'
- 'libs/shared/**'To match a file type anywhere in the repository, start with **:
on:
pull_request:
paths:
- '**.go'
- '**/go.mod'Quote every pattern. In YAML, a value that starts with * is read as an alias, and the workflow fails to parse. Quoting also protects patterns that contain !, [, or {.
Excluding paths
Use paths-ignore to say "run unless every changed file matches these patterns":
on:
push:
paths-ignore:
- 'docs/**'
- '**.md'If a push changes docs/guide.md and services/api/main.go, the workflow still runs, because one file falls outside the ignore list. It is skipped only when all changed files are ignored.
You cannot use paths and paths-ignore together for the same event. To combine includes and excludes, use paths with a ! pattern:
on:
pull_request:
paths:
- 'services/api/**'
- '!services/api/docs/**'Order matters here. A ! pattern removes files matched by the patterns before it. Put the exclusion after the include it narrows. If you put it first, it has nothing to remove, and the later include matches those files again.
How the pieces fit
Common mistakes
- Writing
services/apiwithout/**. The pattern then names a path that no file has, so it never matches. - Using
*when you need**, which silently skips nested files. - Forgetting quotes around a pattern that starts with
*. - Matching case incorrectly. Patterns are case-sensitive, so
Docs/**does not matchdocs/guide.md.
Exercise
A workflow uses paths: ['libs/*']. A pull request changes only libs/shared/util.py. Does the workflow run? What pattern fixes it if it should?
Show answer
It does not run. * does not cross /, so libs/* matches only files directly in libs, such as libs/README.md. Changing the pattern to libs/** matches libs/shared/util.py and the workflow runs.