Sub topic 3
Choosing when tests run: push, pull_request and more
This chapter assumes you can read basic YAML and have a workflow file in .github/workflows that runs your tests. Here you control the on: key, which decides which events start the workflow.
How the `on:` key works
Every workflow lists one or more events under on:. When GitHub sees a matching event in your repository, it starts a run. If no event matches, nothing runs. For test workflows, three events matter most:
| Event | Fires when | Typical use |
|---|---|---|
| `push` | Commits are pushed to a branch or tag | Test the code that landed on `main` |
| `pull_request` | A pull request is opened, updated, or reopened | Test a change before it merges |
| `workflow_dispatch` | Someone starts the workflow by hand | Re-run tests on demand |
Push and pull request together
The workflow from the previous chapter used this block:
on:
push:
branches: [main]
pull_request:Pushes are limited to main. The pull_request event has no filter, so it covers pull requests targeting any branch.
Limiting push to main avoids duplicate runs. Without the filter, a commit pushed to a branch with an open pull request triggers two runs: one for push and one for pull_request.
Event types and what code gets tested
By default, pull_request fires for three activity types: opened, synchronize (new commits pushed to the pull request branch), and reopened. You can list other types with types:, but the default fits most test workflows.
For pull_request, GitHub checks out a temporary merge of your branch into the base branch. The tests therefore run against what main would look like after the merge, not against your branch alone. A test can pass locally and fail in CI because main moved on after you branched.
Filtering by branch and path
Use branches and paths to skip runs that cannot affect the tests. This example runs tests only when source, test, or dependency files change:
on:
push:
branches: [main]
paths:
- "src/**"
- "tests/**"
- "package.json"
- "package-lock.json"
pull_request:
paths:
- "src/**"
- "tests/**"
- "package.json"
- "package-lock.json"Patterns use glob syntax, where ** matches across directories. You can also use branches-ignore and paths-ignore to exclude instead of include. A single event cannot use both branches and branches-ignore.
Running on demand and on a schedule
Add workflow_dispatch to get a "Run workflow" button in the Actions tab. Add schedule to run tests at set times, which catches failures caused by changing dependencies even when nobody pushes code:
on:
push:
branches: [main]
pull_request:
workflow_dispatch:
schedule:
- cron: "0 6 * * 1"The cron string uses five fields (minute, hour, day of month, month, day of week) and is evaluated in UTC. The example runs every Monday at 06:00 UTC. Scheduled workflows run only from the default branch.
Common mistakes
- Putting
branchesunder the wrong event, so the filter applies to a different trigger than you intended. - Editing a workflow on a feature branch and expecting
scheduleto pick it up. Only the default branch version is used. - Filtering by
pathsand forgetting config files such as the lockfile, so dependency changes skip the tests.
Exercise
Your workflow triggers on push with no branch filter and on pull_request. A teammate pushes three commits to a branch that has an open pull request. How many workflow runs start in total, and how would you reduce it?
Show answer
Six runs start: each push fires a push run and a pull_request run (synchronize). Restrict push to branches: [main], which leaves one run per push on the pull request branch.