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]
9 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
6 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?