Sub topic 16
When not to use if: trigger and path filters
This chapter assumes you can read a workflow file and know the on, jobs and steps structure. It also assumes you know that if skips a step or a job after the workflow has started. Some of those conditions can be written as trigger filters instead. Filters decide whether the workflow starts at all.
Two places to decide
A trigger filter lives under on:. GitHub evaluates it before any runner is assigned. If the filter does not match, no workflow run is created.
An if condition is evaluated after the run exists. The run appears in the Actions tab, and the skipped job or step is marked as skipped.
flowchart TDMoving a condition into on
Suppose you wrote this:
on: [push, pull_request]
jobs:
docs:
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- run: echo "Building docs"Every pull request starts a run only to skip the job. The same intent as a trigger filter looks like this:
on:
push:
branches: [main]
paths:
- "docs/**"
pull_request:
paths-ignore:
- "**.md"
jobs:
docs:
runs-on: ubuntu-latest
steps:
- run: echo "Building docs"This version uses three filters:
branchesandtagslimit which refs start the workflow.pathsstarts the workflow only when a changed file matches a pattern.paths-ignoredoes the opposite, and you cannot use it together withpathsfor the same event.
Choosing between them
| Situation | Use |
|---|---|
| The whole workflow is irrelevant for this branch, tag or file set | Trigger filter |
| Only one job or step in a multi-job workflow should be skipped | `if` |
| The condition depends on a result, output or status at run time | `if` |
| The condition depends on the matrix, a secret check or a fork | `if` |
| You want the skip visible in the run history | `if` |
Filters can only use information known when the event arrives: the event type, the ref and the changed paths. They cannot see step outputs or needs results.
Required checks
Filters have one common gotcha. Say a branch protection rule requires a check named docs, and a pull request changes only files excluded by paths-ignore. The workflow never starts, so the check never reports. GitHub then shows it as "Expected" and the pull request can stay blocked.
A job skipped by if is different. GitHub treats a skipped job as a passing check, so it does not block the merge. If a check is required and your files do not always match, prefer an always-triggered workflow with a job-level if, or a small always-running job that gates the rest.
Using both
The two mechanisms combine well. Use on to cut out events that can never matter, and use if for fine-grained decisions inside the run. This saves runner minutes and keeps the Actions list short.
Exercise
A workflow has a job named lint that should run only when files under src/ change, and lint is a required status check. Which approach risks leaving a pull request blocked, and why?
Show answer
Using paths: ["src/**"] under on.pull_request risks it. A pull request that touches no src/ files never starts the workflow, so the required lint check never reports and shows as "Expected". A job-level if avoids this, because a skipped job counts as passing.