How to trigger an action on push or pull request but not both?

I would like my workflow to be triggered by either a push or a pull-request, but if it a push to a pull-request only trigger one rather than two workflows.

Something like,

on: [push | pull_request]
15 Likes

I’m afraid that you cannot do this directly.  However, 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
10 Likes

I found this while searching and thought some people might find my solution helpfull as well.  I have a workflow that has a ‘build’ job and a ‘deploy’ job.  I want to build pull request and deploy merges to master.  Here is my workflow setup:

name: 'foo'
on:
  push:
    branches:
      - master
  pull_request:
env:
  SOME_THING: sweet
jobs:
  build:
    runs-on: ubuntu-latest
    if: github.ref != 'refs/heads/master'
    steps:
      - name: 'Checkout'
        uses: actions/checkout@master
  deploy:
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/master'
    steps:
      - name: 'Checkout'
        uses: actions/checkout@master
9 Likes

Thanks for sharing that solution. I have a follow up question. Is it possible to run actions on branches which have not been pull requested, but don’t run the push on the PR than.

Do you now what I mean?

Will this still run builds when the branch for a pull request is updated? That is, I understand the action will run when a pull request is first created for a branch. But if the action is only run for pushes to master, then does that mean it will not run for pushes which update a branch used in a pull request?

The provided code does not work for me, I get

 Error

Invalid type for `on`

Here’s my yml

on:
  schedule:
    - cron: "0 10 * * *" # everyday at 10am
  tags:
    - "v*.*.*"
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

What am I doing wrong? How do I troubleshoot? Thx

I published https://github.com/marketplace/actions/skip-duplicate-actions to solve this problem.
You can give it a try if this is still an issue.

2 Likes

Solution:

on: 
  push:
  pull_request:
    types: [opened]

It does prevent additional runs caused by pull_request synchronize later as you push to the feature branch indeed. But doesn’t it still cause two workflow runs initially? (pull_request opened and push to feature branch)

I have found a way that seems to work for me.

If you are not concerned about PRs that come from forks the following should work.

on:
  push:

That sets up actions for every push regardless of whether the push is to a branch that is part of a PR or not.

If you also want to run actions on PRs from forked repositories, you need further config because pushes to forks do not count as pushes to your repo.

I’ve noticed that fork PR branches have the form “:”

So by limiting github actions runs to PRs only matching that format I’m able to run github actions workflows only for the PRs which come from forks.

The full config looks like this.

  push:
  pull_request:
    branches:
      # Branches from forks have the form 'user:branch-name' so we only run
      # this job on pull_request events for branches that look like fork
      # branches. Without this we would end up running this job twice for non
      # forked PRs, once for the push and then once for opening the PR.
    - '**:**'

Obviously this won’t work if you are regularly naming branches with colons in them, but in the whole history of our project we never did, so it’s working pretty well for us.

7 Likes

This is a very important issue as you may use some automatization for merging… if you have N checks, created PR as repo owner creates 2N check compare to forked PR creates only N checks…
With limiting push/PR to master you miss all development/testing in branches even as repo owner :confused:

This works for me! Thanks @piersy!