Sub topic 4
Choosing triggers: pull_request and push
This chapter assumes you can read basic YAML and know how pull requests work on GitHub. It also assumes you have seen the on:, jobs:, and steps: keys from the previous chapter. Here you decide which events start your lint and format workflow.
What a trigger does
The on: key lists the events that start a workflow run. Each event can carry filters that narrow when it fires. For lint and format checks, two events matter most: pull_request and push.
The pull_request event
pull_request fires when something happens to a pull request. By default it runs for three activity types:
opened, when the pull request is created.synchronize, when new commits are pushed to its branch.reopened, when a closed pull request is opened again.
The run checks out a temporary merge commit. That commit is your branch merged into the target branch. The check therefore tests the code as it would look after merging, not only your branch in isolation.
For pull requests from forks, pull_request runs with a read-only token and no access to repository secrets. That suits lint checks, which need neither.
The push event
push fires when commits arrive on a branch. Without a filter it fires for every branch and tag. For checks you usually restrict it to the branch that must stay clean:
on:
pull_request:
push:
branches: [main]This is the trigger block from the basic workflow. Pull requests are checked before merging. Pushes to main are checked after merging, which catches problems from direct pushes or from two pull requests that pass separately but conflict in combination.
Avoiding duplicate runs
A common mistake is this shorthand:
on: [push, pull_request]It looks equivalent but is not. Push events fire on every branch. When you push a commit to a branch with an open pull request, both push and pull_request start a run, so the same code is linted twice. Limiting push to main removes the duplicate.
Filtering by path
You can skip runs when irrelevant files change:
on:
pull_request:
paths:
- "src/**"
- "package.json"
- "package-lock.json"
- ".eslintrc.*"Use this carefully. If a workflow is skipped because of a paths filter, its status check is never reported. A required status check that never reports stays pending, and the pull request cannot merge. Unless you have a plan for this, leave paths off lint workflows.
Other events worth knowing
workflow_dispatchadds a "Run workflow" button, which helps when you debug a configuration.pull_request_targetruns in the context of the base branch with access to secrets. Do not use it to check out and run code from a pull request, because untrusted code could then read those secrets. Lint checks have no need for it.
Common errors
| Symptom | Likely cause |
|---|---|
| Every commit lints twice | `push` has no `branches` filter alongside `pull_request` |
| Workflow never starts on a pull request | The workflow file exists only on the feature branch, or has a YAML syntax error |
| Required check stuck on "Expected" | A `paths` filter skipped the workflow |
| No run after a push to a branch | The branch is not listed under `push.branches` |
Exercise
A repository uses on: [push, pull_request]. A developer pushes a new commit to a branch that has an open pull request. How many lint runs start, and what change to the trigger block prevents the extra one?
Show answer
Two runs start, one from push and one from pull_request. Restricting push to branches: [main] leaves only the pull_request run for feature branches.