AI Hints
Home›CI/CD with GitHub Actions Tutorial›Filters, Manual and Scheduled Runs
CI/CD with GitHub Actions · Lesson 31 of 138

Branch and Path Filters

Run only when needed.

⏱ 2 min read▶ Video at 34:53🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

Running every workflow on every change wastes time and money. If someone only fixes a typo in the readme, there's no need to run the full test suite. Branch and path filters let a workflow run only when it matters.

Branch and Path Filters
From the video: Branch and Path Filters
  • Every run costs time
  • A typo in the readme?
  • Filters skip it

Filters

Branches lists the branches that count, here main, and anything starting with release slash. Two stars match any characters. Paths ignore lists files that shouldn't trigger a run. Here, markdown files, and anything in the docs folder. There's also paths, the opposite, which runs only when matching files change.

.github/workflows/tests.ymlyaml
name: Tests
on:
  push:
    branches: [main, "release/**"]
    paths-ignore:
      - "**.md"
      - "docs/**"
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - run: pytest -q

Does it run?

First, we change only the readme and commit. Run steps checks the filters, and the workflow isn't triggered, because every changed file is ignored. Then we change app dot py. This time it runs, and the tests pass. On a branch called experiment, it wouldn't run at all, because the branch isn't listed.

Terminal
echo "More docs." >> README.md && git commit -qam "Docs"
run-steps .github/workflows/tests.yml
Workflow: Tests  (event: push, branch: main)
  Not triggered: every changed file is in paths-ignore
echo '# tidy' >> app.py && git commit -qam "Tidy"
run-steps .github/workflows/tests.yml
Workflow: Tests  (event: push, branch: main)
Job: test
  ✓ Run pytest -q
      ...                                                                                [100%]
      3 passed in 0.01s
  Job succeeded
run-steps .github/workflows/tests.yml --ref experiment
Workflow: Tests  (event: push, branch: experiment)
  Not triggered: branch experiment is not in ['main', 'release/**']

Summary

Branch filters pick which branches count, and path filters skip runs when only unimportant files changed. Next, running a workflow by hand.

Key takeaways
  • branches:
  • paths / paths-ignore
  • ** matches anything
Next up: Manual Runs — workflow_dispatch