GitHub Actions: avoid double runs from on: [push, pull_request]

Artist’s depiction of your special snowflake build process.

I’ve often seen a GitHub Actions workflow defined with a trigger like:

on: [push, pull_request]

This means “run on any push or when a pull request is opened or updated”. While that may seem correct, it leads to double execution of the workflow when you push a commit and then create a pull request with it. You can see this in the status check details for the pull request:

GitHub checks showing two runs of “CI / test”, labelled “CI / test (pull_request)” and “CI / test (push)”.

Double execution means double the cost, extra waiting time, and increased chance of a spurious/flaky failure.

A better on clause looks like:

on:
  push:
    branches:
    - main
  pull_request:

This means “run for any push to the main branch or when a pull request is opened or updated”. If your workflow should run for tagged pushes as well, use:

on:
  push:
    branches:
    - main
    tags:
    - '**'
  pull_request:

This might be useful when your workflow contains a release job, as my open source projects do (example).

You’ll then see just a single run for your workflow in pull requests, like:

GitHub checks showing a single run of “CI / test”, labelled “CI / test (pull_request)”.

Fin

May your workflows run when you need, but no more,

—Adam


Learn more about using GitHub effectively in my book Boost Your GitHub DX.


Subscribe via RSS, Twitter, Mastodon, or email:

One summary email a week, no spam, I pinky promise.

Related posts:

Tags: