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]
10 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
8 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.

1 Like