31

on push and on pull_request difference in github actions?

On every pull request we are pushing our code then why do we need on push and on pull_request isn't just on push enough?

3 Answers 3

17

You can trigger only on pushes to master or pull requests to master. This will prevent builds from happening twice when somebody opens a pull request against master and then pushes updates to their branch.

For example:

on:
  push:
    branches:
    - master
  pull_request:
    branches:
    - master
9

on push and on pull_request difference in github actions?

In general, push will trigger when you push code where pull_request will trigger when there is a pull request.

They overlap when you create PRs from the same repo, but you need pull_request if you want to run an action when you receive a PR from a fork for example. You need push when you want to run an action when something is push. You can fine tune them depending on the behaviour you expect to avoid duplication of jobs.

1
  • 3
    The only duplication I can think of is when somebody (cough dependabot cough) pushes a single commit and opens a PR with it against master. Then the same build is run twice. But I can't figure out how to "fine tune" my triggers to avoid this without also turning off builds I'd like to run.
    – kthy
    Commented Jan 16, 2023 at 23:09
3

Example that highlights the difference and shows that both can be important and needed:

Imagine you want to run CI tests every time code is pushed, but only when in a PR (eg to save money in GHA)

If you only use on: push then when the PR is first raised, you won't have any tests run (because raising a PR alone does not trigger the on: push) You also need on: pull_request

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.