Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 3
Message 1 of 6

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

Solved! Go to Solution.

I'm using the classical configuration:

```yml

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-... which doesn't have a satisfiying answer.

 

Thanks,

- Markus

5 Replies
Highlighted
Commander Lvl 1
Message 2 of 6

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

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? 



*Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!*
Highlighted
GitHub Partner
Message 3 of 6

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

 


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.   

on push pull_request.png

>>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.    

 

 

Highlighted
Copilot Lvl 3
Message 4 of 6

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

> 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 😅 I'm not able to find this in the documentation or I'm looking in the wong place.

 

Places I read:

- https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions

  That seems to be the first place to start

- https://help.github.com/en/actions/reference/events-that-trigger-workflows#pull-request-event-pull_r...

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

 

thanks

Highlighted
Solution
GitHub Partner
Message 5 of 6

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

 


- `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

PR Ref.pngPush 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. 

Highlighted
Copilot Lvl 3
Message 6 of 6

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

Thank you so much, this totally clears things up 🙇‍♀️