How to trigger an action on push or pull request but not both? #26276
-
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,
|
Beta Was this translation helpful? Give feedback.
Replies: 18 comments 9 replies
-
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:
|
Beta Was this translation helpful? Give feedback.
-
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:
|
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
The provided code does not work for me, I get
Here’s my yml
What am I doing wrong? How do I troubleshoot? Thx |
Beta Was this translation helpful? Give feedback.
-
I published https://github.com/marketplace/actions/skip-duplicate-actions to solve this problem. |
Beta Was this translation helpful? Give feedback.
-
Solution:
|
Beta Was this translation helpful? Give feedback.
-
It does prevent additional runs caused by |
Beta Was this translation helpful? Give feedback.
-
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… |
Beta Was this translation helpful? Give feedback.
-
This works for me! Thanks @piersy! |
Beta Was this translation helpful? Give feedback.
-
@marcosfelt Did it really? I thought it was working for us but it wasn’t actually letting the forked PRs run. In fact I came to the conclusion that the “user:branch-name” branch names for forked PRs are a result of the way the github frontend displays branch names and do not actually represent a different class of branch name. In the case of non forked PRs I think github hides the “user:” prefix, because it is redundant. The branch name that is matched against by github actions will be the segment that follows the colon. Please confirm if I’m incorrect otherwise I will delete the original post as I do not see any way to edit it. We have now settled for a different solution:
This runs github actions on all PRs and allows us to manually trigger workflows on arbitrary branches. This works out slightly better for us since we didn’t really want to run github actions on every push. |
Beta Was this translation helpful? Give feedback.
-
I deleted this post because it contained incorrect information, the solution I provided did not work and I receive confirmation from one other person that it was not working for them. See this post for the current solution we have adopted - How to trigger an action on push or pull request but not both? - #13 by piersy |
Beta Was this translation helpful? Give feedback.
-
I have found a work around to this: My workflow has this at the top:
I have the following steps: Get Branch Name:
Using a JS script (see below)
js script (GET request on Github API to see if the branch pushed is in a PR already)
Set output for the js script
Using this if statement, you can make it so on_push skips all of the steps and exists successfully, but if you push a branch before a PR, the workflow will run as normal. Let me know if you have questions.
|
Beta Was this translation helpful? Give feedback.
-
Yea, I also have that problem. But now while reading here, I reconsider why I actually have my CI triggered on both pushes and pull requests. The reason was that I definitely need CI on pushes to master. But I also wanted to have CI run on custom dev branches. And of course I want to have CI run on PRs. The CI on custom dev branches are causing the two runs of CI because they are often also PRs at the same time. But now I wonder if I really need the CI to run on any branch (except master). For any dev branch where I would want CI to run, I could just create a draft PR. Draft PRs basically solve this. So now I use:
|
Beta Was this translation helpful? Give feedback.
-
We’ve solved the same problem with this minimal configuration:
This way:
|
Beta Was this translation helpful? Give feedback.
-
I had the same issue and it was solved by using |
Beta Was this translation helpful? Give feedback.
-
You can manage concurrent workflows using GitHub Actions' concurrency feature. By setting up concurrency at the workflow level, you ensure that only one instance of a workflow runs for a given concurrency group. This is particularly useful to avoid overlapping runs for the same branch or pull request. For example, if you specify the following in your workflow file, it ensures that only one workflow associated with the same branch (or pull request) runs at a time: concurrency:
group: "your-group-name"
cancel-in-progress: true I got the inspiration for this solution in a helpful blog post by Maxime Heckel. |
Beta Was this translation helpful? Give feedback.
-
This action might provide the functionality you're looking for: https://github.com/fkirc/skip-duplicate-actions |
Beta Was this translation helpful? Give feedback.
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: