'Configuration of GitHub Actions: Avoid running CI twice when merging PR into main
I am using GitHub a ctions to manage my CI and the following events trigger my workflow:
on:
pull_request:
branches: main
push:
branches: main
I observed the following "problem": When I create a PR, the CI is run. If the test passes and I merge it into main, the tests are run again (which I don' t want in specific cases). How can I setup my workflow such that the CI is not triggered when merging a PR, where the CI already passed for the PR?
Thanks in advance!
Solution 1:[1]
You might consider an if conditional in your case:
jobs.<job_id>.ifYou can use the
jobs.<job_id>.ifconditional to prevent a job from running unless a condition is met. You can use any supported context and expression to create a conditional.When you use expressions in an
ifconditional, you may omit the expression syntax(${{ }})because GitHub automatically evaluates theifconditional as an expression. For more information, see "Expressions".
jobs:
build:
if: github.event.pull_request.merged == false
. . .
That would still trigger the workflow, but it can skip the jobs in the workflow.
Alternatives exist in this thread, which deals with the opposite requirements ("Trigger workflow only on pull request merge"): it can be adapted to do what you need (not trigger a workflow, or at least do the same job twice, on PR merge)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
