GitHub Actions have different triggers. You can run an action when pushing code to a repository, or when creating a new tag. When building open source packages, you may receive pull requests from users. Typically, you want to run your test suite against the changed code in the pull request.
This tutorial shows you how to use pull requests as a trigger to run a GitHub Action.
GitHub Actions Series Overview
- Getting Started With Node.js
- Create a Testing Matrix
- Using MongoDB
- Using Redis
- Using MongoDB Replica Sets
- Run on Pull Request
- Customize the Job Name
- Run a Workflow When Creating a Tag
- Testing Against an Exact Node.js Version
- Trigger Builds on Schedule (Cron)
- How to Test Lerna Monorepos
- How to Add Comments in YAML Files
- Clone Another Repository
- Run on Push or Pull Request, but Not Both
- Limit Concurrency and Cancel In-Progress Jobs
- Test Against the Latest Node.js Version
Run Actions on Pull Requests
When creating a new workflow in GitHub’s action builder the default trigger is the push
event. You want to extend this to push and pull request events.
Search the line on: [push]
in your GitHub Action workflow file. Extend it to on: [push, pull_request]
and you’re done.
Here’s a sample configuration to run your action when pushing code to the repository or on pull requests:
name: Run tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: …
This workflow configuration runs on every push and every pull request. Even for pull requests on feature branches or against a playground branch.
Refining the Pull Request Trigger
Open source projects receive unlimited GitHub Action minutes. Private repositories include 2,000 minutes each month. You may want to restrict action runs to save minutes.
For example, you can restrict actions on pull requests to only run when created against the master
branch:
name: Run tests
on:
push:
pull_request:
branches:
- master
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: …
This workflow configuration runs the jobs on each push and pull requests against the master
branch.