How to run a workflow only once, even when triggered mutliple times #26741
-
I have a CI workflow that I want to run on PRs, but also when I’m just pushing to my repo. That way, I can notice issues sooner during development when I have not yet decided to turn it into a PR. The workflow also needs to run when a PR gets merged into master. But when I simply enter
it will run twice every time I update a PR that is on a branch that’s already part of the repo. Either a config that fits my usecase or a way to tell a workflow to run only once given a commit hash would be nice. |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments
-
The way you have it setup it will always run on a push and when a PR is opened, yes. That would be expected when you configure it like that. If you only want to run it whenever you push a new branch (which is how I interpret your use case), you should listen just to the create event instead, so you’ll end up with:
And it will only run when you first push a new branch and when you eventually open up a PR. |
Beta Was this translation helpful? Give feedback.
-
That way it won’t be run when it gets merged into master, though. In other words, I want to run the workflow on every push for every branch within my repo, and also for PRs that come from forked repos. But reading the docs you linked for the pull request event, it seems that there is no such configuration and i just might need to change the way I think about these triggers. |
Beta Was this translation helpful? Give feedback.
-
In order to trigger things when they are merged into master, you can use the following workflow structure:
Note the “if” condition on the step. You will need to add that to every step to make sure it only run whenever the PR was actually merged (and not closed/discarded without merge). There is currently no way to enforce such a condition on all steps in a job yet, but this setup works just fine for my CD workflow (which I also only want to trigger when something is merged into master). |
Beta Was this translation helpful? Give feedback.
-
We recently shipped support for if at the job level so this workflow should be easier now. https://help.github.com/en/articles/workflow-syntax-for-github-actions#jobsjob_idif |
Beta Was this translation helpful? Give feedback.
-
This has some more comments on running checks twice: Duplicate checks on "push" and "pull_request" simultaneous event - #4 by yanjingzhu Since most of the comments are older, I am not sure what is helpful, but it might be a good idea to link / merge these threads. |
Beta Was this translation helpful? Give feedback.
-
Personally, as a newbie I find the explanations confusing. The accepted solution above only runs the CI on merge (which would make it not run at all when pushing to a feature branch in a PR, right?). The newer comment gives a helpful link, but there is no example. |
Beta Was this translation helpful? Give feedback.
In order to trigger things when they are merged into master, you can use the following workflow structure:
Note the “if” condition on the step. You will need to add that to every step to make sure it only run whenever the PR was actually merged (and not closed/discarded without merge). There is currently no way to enforce such a condition on all steps in a job yet, but this setup works just fine for my CD workflow (which I also only want to trigger when someth…