Sub topic 2
Triggers: push and pull_request
This chapter explains the two events that start most CI runs, and how each one decides which commit a workflow sees. It assumes you can read workflow YAML, know what a branch and a pull request are, and have a repository that holds several projects.
Two events, two questions
A workflow starts when an event listed under on: happens. For CI, two events matter most.
pushfires when commits are pushed to a branch or tag.pull_requestfires when a pull request is opened, updated with new commits, or reopened.
By default, pull_request runs for the activity types opened, synchronize, and reopened. You rarely need to change that.
The two events answer different questions. A push run asks whether the branch is healthy after these commits. A pull_request run asks whether the result of merging this branch into its base branch would be healthy.
What each event checks out
The events differ in the commit that actions/checkout fetches.
| Event | Commit checked out by default | Typical use |
|---|---|---|
| `push` | The pushed commit at the branch tip | Verify `main` after a merge |
| `pull_request` | A temporary merge commit of the PR head into the base branch | Verify a change before merge |
Because of the merge commit, a pull request run tests your code combined with the latest base branch. A passing PR can still fail if the base moves, but you do catch many conflicts early.
A common trigger setup
The usual pattern is to run on pull requests and on pushes to the main branch only.
name: ci
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make testHere branches is a filter on each event. For push, it matches the branch that was pushed. For pull_request, it matches the base branch the PR targets, not the branch the PR comes from.
Limiting push to main avoids running the same tests twice. Without it, every push to a feature branch with an open PR triggers both a push run and a pull_request run.
Why this matters for a monorepo
In a monorepo, each event can start every project's tests. If services/api changes, you do not want the frontend and billing jobs to run too. Both push and pull_request accept the same paths and paths-ignore filters. The next chapters cover them in detail, so keep these rules in mind:
- Filters are set per event, so a filter under
pushdoes not affectpull_request. - If you add
pathsto one event and forget the other, you get inconsistent behavior. Pull requests may be filtered while pushes tomainrun everything, or the reverse. branchesandpathsare combined with AND. Both must match for the run to start.
Common mistakes
- Using
branchesunderpull_requestto mean the source branch. It means the target branch. - Running on every push and every pull request, which doubles the work on PR branches.
- Writing a filter under only one event and assuming it applies to both.
Exercise
A workflow has on: push: branches: [main] and on: pull_request: branches: [main]. A developer pushes a new commit to the branch feature/login, which has an open pull request targeting main. Which events start a run, and which commit is tested?
Show answer
Only the pull_request event starts a run, because feature/login does not match the push branch filter of main. The commit tested is the temporary merge commit of the PR head into main, not the branch tip.