How to prevent running actions on `push` when `pull_request` is also specified?

I’m using the classical configuration:


name: …

on: [push, pull\_request]

# …

When a PR is made, this always run both strategies (obvious), but I don’t want that.

What I want is:

  • as long as there’s not PR, I want to run the build on every push or force-push (for every branch)

  • once there’s a PR (pull_request) I don’t need pull anymore.

Maybe… do I even need pull_request in this scenario?

Basically on every push or force-push, no matter where to (a branch with or without PR) I want the build to run.

This matters for cases where no dedicated PR is needed in branch but I still want to “see” that the tests for this branches are green before merging manually. Or I just want to trigger a test suit run with an idea of a modification.

Maybe I’m overthinking things, not sure. Or I don’t get the proper distinction between them. I looked at https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#on but can’t derive a clear answer.

It sounds similar to https://github.community/t5/GitHub-Actions/How-to-trigger-an-action-on-push-or-pull-request-but-not-both/m-p/35805 which doesn’t have a satisfiying answer.

Thanks,

  • Markus
5 Likes

Hey! I am trying to understand exactly what you are looking for here. Is it fair to say that your desired workflow is: “Basically on every push or force-push, no matter where to (a branch with or without PR) I want the build to run.” or am I misunderstanding something? 

1 Like

What I want is:

  • as long as there’s not PR, I want to run the build on every push or force-push (for every branch)
  • once there’s a PR (pull_request) I don’t need pull anymore.

Let me sort out your requirement, for example there is a pull request from dev to master, when changes push to dev.

  1. You don’t want the workflow to be triggered on: push with dev branch. Just on: pull_request is enough.   

>> What I have to say is that your requirement could not be reached.  

If you want to run a build when push to every branch , you need to specify on:push in the workflow yml file on all branches . This will lead to that on push will work no matter there is a pull request or not. 

The workflow on pull request is building a pre merged code from dev to master (refs/pull/id/merge) , the workflow on push is building just code on dev branch. These two workflow runs are running with different codes. You could not prevent the workflow run on push event.    

1 Like

> Hey! I am trying to understand exactly what you are looking for here. Is it fair to say that your desired workflow is: “Basically on every push or force-push, no matter where to (a branch with or without PR) I want the build to run.” or am I misunderstanding something? 

Yes, that’s th gist and correct!

> These two workflow runs are running with different codes. You could not prevent the workflow run on push event.

Oh, I think this is the news for me!

So:

  • pull means: check out the “tree on the given commit”, then run action

  • pull_request` means: check out the “tree on given commit and merge with master (or whatever the target is)”, then run action because of what you said:

  > The workflow on pull request is building a pre merged code from dev to master (refs/pull/id/merge)

Did I got this right?

Drive-by question: If so, what happens on merge conflicts with master/target branch?

It totally makes sense but it’s also totally not obvious to me :sweat_smile: I’m not able to find this in the documentation or I’m looking in the wong place.

Places I read:

  That seems to be the first place to start

  Very detailed on it’s parameters, but doens’t tell you about the git/tree state?

thanks

  • pull means: check out the “tree on the given commit”, then run action
  • pull_request` means: check out the “tree on given commit and merge with master (or whatever the target is)”, then run action because of what you said:

  > The workflow on pull request is building a pre merged code from dev to master (refs/pull/id/merge)

 

Did I got this right?

Yes, you got it right. The git/tree you mentioned is the GITHUB_REF. You could see the environment variable description here

Github_ref.png

Note:  I think the ‘pull’ you mentioned above is push. 

Drive-by question: If so, what happens on merge conflicts with master/target branch?

When PR reports merge conflicts, the workflow on: pull_request will not be triggered. 

8 Likes

Thank you so much, this totally clears things up :bowing_woman:

Have you figure out the answer? It’s happing to me as well. I don’t want multiple builds(for push and PR) when PR is already created

1 Like