Sub topic 5
Running on pull requests
This chapter covers the pull_request event. It assumes you can read YAML, know how pull requests and branches work in GitHub, and have seen the on key and the push trigger from the earlier chapters.
What the event does
The pull_request event fires when something happens to a pull request. By default it fires for three activity types: opened, synchronize (new commits pushed to the head branch) and reopened.
on:
pull_request:
branches: [main]This workflow runs for pull requests that target main. The branches filter on pull_request matches the base branch, which is the branch the pull request merges into. It does not match the branch you are working on. This differs from push, where branches matches the branch that was pushed.
The merge commit
A pull_request run does not check out your branch tip by default. actions/checkout fetches a temporary merge commit that combines the pull request head with the current base branch. The ref is refs/pull/<number>/merge, and github.sha is the merge commit's SHA.
This means your tests run against the code as it would look after merging. A pull request that passes on its own branch can still fail here if main has moved on and the two changes conflict. If the pull request has merge conflicts, GitHub cannot create the merge commit, and the workflow does not run.
Path filters
You can combine branches with paths or paths-ignore, as you did for push. The workflow runs only when at least one changed file in the pull request matches.
on:
pull_request:
branches: [main]
paths:
- 'src/**'
- 'package.json'Activity types
Use types to run on a different set of activities. When you write types, only the listed types trigger the workflow, so list every one you need.
on:
pull_request:
branches: [main]
types: [opened, synchronize, reopened, ready_for_review]Adding ready_for_review lets you skip draft pull requests until they are marked ready. To do that, add if: github.event.pull_request.draft == false to the job.
Pull requests from forks
When a pull request comes from a fork, pull_request runs with a read-only GITHUB_TOKEN and without access to repository secrets. This protects your secrets from untrusted code. A first-time contributor's run may also wait for a maintainer to approve it.
The pull_request_target event runs in the context of the base repository, with secrets and a write-capable token. It checks out the base branch by default, not the fork's code.
Common mistakes
- Expecting
branchesto match the source branch. It matches the target. - Writing
typesand forgettingsynchronize, so new commits no longer trigger runs. - Wondering why no run appeared on a pull request with conflicts. Resolve the conflicts first.
Exercise
A workflow has on: pull_request: types: [opened]. A developer opens a pull request and the workflow runs. They then push a fix to the same branch. Does the workflow run again? How would you change it?
Show answer
No. Pushing a commit creates a synchronize activity, and only opened is listed. Change the list to types: [opened, synchronize, reopened], or remove types to use the defaults.