We’re using GitHub Actions to run tests when pushing to a given branch. GitHub Actions are a convenient way for testing because the tests can run on the same platform where the code lives.
We also wanted to run the CI pipeline for pull requests. And GitHub Actions come with an
on: pull_request trigger. We noticed that GitHub Actions runs all test suites twice when activating the pull request trigger besides the
on: push trigger. That’s not what we wanted: we want to run tests only for one trigger, push or pull request.
This tutorial shows you how to run a GitHub Action workflow on push or pull request, but not on both.
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
Run GitHub Actions Once for Pushes and Pull Requests
Running tests in GitHub Actions for pushes OR pull requests is doable. We were able to find a configuration that is does it. The trick we use: don’t use the
pull_request trigger, only rely on the
Here’s our workflow file to run tests:
name: Run tests on: push: paths-ignore: - 'README.md'
As you can see we use the
push trigger to run the testing workflow. This configuration does the following:
- run tests for every push, no matter which branch
- no need to create a pull request to run the tests
- when creating a pull request for a branch, it’s already tested (or tests are currently running)
- when updating a pull request the pushed changes trigger a new run
- when merging a pull request the merge commit triggers a build on your main branch
- pushing directly to your main branch triggers the workflow as well, because all branches are build on push
If you’re wondering if the
path-ignore: README.md option is import: no it’s not. We don’t run the CI pipeline when changing the
README.md file because it’s a manually crafted markdown file. Changes in this file have no impact on the actual code and don’t need to trigger any tests. Running tests for changes on the readme would occupy resources and don’t add benefit to the project.
Enjoy running your CI pipeline in GitHub Actions on pushes and pull requests!