पाठ 5 / 25
Branch and Path Filters
Skip work that is not needed.
paths, paths-ignore, branches
Filters keep workflows from running unnecessarily: branches limits which branches trigger, paths runs only when matching files change, and paths-ignore skips changes that only touch matching files (documentation, for example). Patterns support *, ** and ! negation, evaluated in order so later patterns override earlier ones. Beware: if a workflow is a required status check but is skipped by a path filter, the pull request may wait forever for it; configure required checks with that in mind.
Which changes trigger a path-filtered workflow? run
I ran this with Python 3. It is a simplified model of GitHub Actions behaviour for learning, not GitHub's implementation. With paths [src/, package.json, !/*.md], a source change and a package.json change run the workflow; documentation-only changes, including a Markdown file inside src/, are skipped.
from fnmatch import fnmatch
filters = ["src/**", "package.json", "!**/*.md"] # on: push: paths: [...]
def matches(path):
included = False
for f in filters: # later patterns override earlier ones
neg = f.startswith("!")
pat = f[1:] if neg else f
if fnmatch(path, pat.replace("**/", "*").replace("/**", "/*")):
included = not neg
return included
for changed in [["src/app.ts"], ["docs/guide.md"], ["src/README.md"], ["package.json", "README.md"]]:
print(changed, "->", "workflow runs" if any(matches(p) for p in changed) else "skipped")
Output:
['src/app.ts'] -> workflow runs ['docs/guide.md'] -> skipped ['src/README.md'] -> skipped ['package.json', 'README.md'] -> workflow runs
Mind required checks
Path-filtered workflows that are required checks can block merges when skipped; use a small always-running gate job or conditional logic instead.
त्वरित जाँच: With paths [src/**, !**/*.md], does changing only src/README.md trigger the workflow?
- Yes, because it is under src/
- No, the later negation excludes Markdown files
- Only on Mondays
- Only for forks
Answer
No, the later negation excludes Markdown files — Later patterns win.