Gemini can review your pull requests automatically. It reads the diff, comments inline on risky lines and posts a summary, all triggered inside GitHub Actions or Cloud Build on every push. This turns your CI pipeline into a first-pass reviewer that never gets tired and never skips the boring files.
This lesson wires that up end to end. You will see the official action, a clean workflow YAML, the exact permissions it needs, and the levers that keep it cheap and safe.
Two ways to run Gemini against a PR
There are two distinct integration surfaces, and mixing them up wastes time.
Gemini Code Assist for GitHub is the managed app. You install it from the GitHub Marketplace onto a repo or org. It reviews PRs automatically and responds to /gemini commands in comments (for example /gemini review or /gemini summary). There is no YAML and no key management. Start here: Gemini Code Assist.
The Run Gemini CLI action is the DIY path. You control the workflow, the model, the prompt and the exact steps. Use it when you need custom checks, such as enforcing a changelog entry, blocking a dependency or verifying that a migration file exists. It is also the path when you want the review to run inside your existing Cloud Build pipeline. The action is google-github-actions/run-gemini-cli.
The rest of this lesson covers the DIY path, because that is where the advanced control lives.
What "the CLI in CI" actually means
You already know the Gemini CLI as an interactive terminal agent. In CI it runs headless: you pass it a prompt, it can read files and run shell tools (like the gh CLI), and then it exits. The GitHub Action is a thin wrapper. It authenticates, feeds the CLI a prompt and gives it the tools it needs to post comments.
The minimal PR review workflow
Here is a complete workflow. It triggers on pull requests, hands the diff context to Gemini, and lets Gemini post a review via the GitHub CLI.
name: Gemini PR Review
on:
pull_request:
types: [opened, synchronize]
permissions:
contents: read
pull-requests: write
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Gemini review
uses: google-github-actions/run-gemini-cli@v0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
with:
gemini_api_key: ${{ secrets.GEMINI_API_KEY }}
gemini_model: gemini-3-flash
prompt: |
Review the diff for PR #${PR_NUMBER}.
Run: gh pr diff ${PR_NUMBER}
Focus on bugs, security issues, and missing error handling.
Skip style nits. If the diff is clean, say so briefly.
Post your review with:
gh pr comment ${PR_NUMBER} --body "<your review>"Notice what is not here. There is no giant custom script and there are no manual API calls. The action authenticates the CLI, the prompt tells Gemini what to do, and Gemini uses gh (preinstalled on GitHub runners) to read the diff and post back.
The action is still on a v0 tag, so input names can shift between releases. Check the action README for the current inputs before you copy this into production, and pin a specific version once it works.
Why fetch-depth: 0
A shallow checkout (the default) only grabs the tip commit. gh pr diff needs enough history to compute the diff against the base branch. Fetching the full history avoids a class of "empty diff" failures that look like the model ignored the code.
Permissions: give the least, name it explicitly
The permissions block is the most important safety control. GitHub grants the GITHUB_TOKEN only what you list.
permissions:
contents: read # read the code
pull-requests: write # post review commentsThat is the whole footprint for a review job. These permissions are deliberately absent:
contents: write, because the job must never push codeactions: write, because it must never modify other workflowsid-token: write, which is only needed for Workload Identity (see below)
Guard against forked PRs. The pull_request trigger from a fork gets a read-only token and no access to your secrets. That is correct and safe. If you switch to pull_request_target to get secret access, your workflow runs with write permissions against untrusted code from the fork. That is a well-known attack vector. Prefer pull_request. If a fork genuinely needs review, require a maintainer to approve the run first using GitHub's "require approval for outside collaborators" setting.
Check your reading
What is the fundamental distinction between 'Gemini Code Assist for GitHub' and the 'Run Gemini CLI action'?
Authenticating: API key vs vertex AI
You have two ways to give the action credentials, and the choice affects both billing and governance.
Gemini API key (shown above). This is the simplest option. Create a key in Google AI Studio and store it as the GEMINI_API_KEY repo secret. It suits individuals and small teams. Billing runs through the Gemini API.
Vertex AI with Workload Identity Federation. This is the option for companies. Instead of a long-lived key, the workflow exchanges its GitHub OIDC token for short-lived Google Cloud credentials, so there is no secret to leak.
To set it up, add id-token: write to permissions and configure the google-github-actions/auth step. The review then runs against Vertex AI. It lands under your org's Cloud project, IAM, quotas and audit logging. This is the answer when security asks "where does our code go and who can see it."
In practice, keys are faster to start with. Vertex AI is what you standardize on once more than a couple of people depend on the review.
Keeping it cheap
Automated reviews run on every push. Without limits, a chatty contributor force-pushing ten times can trigger ten full reviews. Three levers control cost.
Pick the right model tier
Use Gemini 3 Flash for routine PR review. It is the fast, low-cost tier, and it reliably spots missing null checks, obvious injection risks and absent error handling. Reserve Gemini 3 Pro for changes that need deeper multi-file reasoning, like an architectural refactor or a tricky concurrency fix.
A common pattern is to default to Flash and add a second workflow that re-runs the review with Pro when a maintainer applies a deep-review label.
Older workflows often still pin gemini-2.5-flash. Google retires older model versions on a published schedule, so check the Gemini models page and update the model string before a pinned model stops responding and your check silently fails.
Cancel superseded runs
If someone pushes three times in a minute, only review the latest. Add a concurrency group:
concurrency:
group: gemini-review-${{ github.event.pull_request.number }}
cancel-in-progress: trueOn active PRs, this alone can cut spend dramatically.
Scope what the model reads
Gemini handles long context well, but being able to read a whole repo does not mean every review should. Feeding an entire monorepo into every review is slow and expensive. Constrain the prompt to the diff (gh pr diff). For large diffs, tell the model to focus on the highest-risk files (auth, migrations, payment logic) instead of commenting on every changed line. You can also tell it in the prompt to skip generated files and lockfiles.
Video
Automate PR Reviews with the Gemini CLI GitHub Action
walkthrough of installing the run-gemini-cli action and configuring automated reviews
Watch on YouTubeCheck your reading
In which scenario should you prefer the 'Run Gemini CLI action' over the managed Code Assist app?
Beyond review: labeling, triage, and gated checks
Once the action is in place, PR review is just one prompt. The same setup automates the tedious edges of maintaining a repo.
Auto-triage issues. A separate workflow triggered on issues: [opened] can have Gemini read a new issue and apply labels (bug, docs, feature). It can also post a "here is what we need to reproduce this" comment. Give that workflow issues: write and nothing else.
Gated custom checks. Because the CLI can run shell tools, you can ask it to enforce project rules that a linter cannot easily express. Here is an example prompt line: "If this PR changes any file under db/migrations/, verify a matching rollback file exists. If not, post a comment and exit with code 1." A nonzero exit fails the check. That blocks the merge if you make it a required status check in branch protection.
Scheduled maintenance. A schedule trigger can run Gemini nightly to summarize stale PRs or flag dependencies that have drifted. This is where the CLI's agentic design pays off. It reads state, reasons and acts in one step.
Keeping the review useful, not noisy
The fastest way to get a bot ignored is to let it comment on everything. Two rules keep the signal high.
First, tell it what to skip. Style and formatting belong to your linter and formatter, not to Gemini. Put "do not comment on formatting, import ordering, or naming style" directly in the prompt.
Second, label the output. A first-pass review is a first pass, not the final word. A one-line header like "Automated review by Gemini 3 Flash. A human still owns approval." sets expectations and keeps the review from being mistaken for a sign-off.
A prompt that ages well
Store the review instructions in a file in the repo (for example .gemini/review-prompt.md) and have the workflow read it, rather than inlining a long prompt in YAML. This has two benefits:
- The prompt is version-controlled and reviewable like any other code.
- Different repos can tune their own emphasis. A security-critical service can ask for stricter checks than an internal dashboard.
The action also accepts a settings input for CLI configuration, which keeps your workflow YAML short.
Putting it in cloud build
If your org runs CI in Google Cloud rather than on GitHub runners, the same Gemini CLI runs as a Cloud Build step. You trigger the build from a GitHub PR event, run gemini in a step with the prompt, and use gh to post back.
The advantage is that authentication to Vertex AI is native. The build's service account already has a Google Cloud identity, so there is no OIDC federation to configure and no key to store. For regulated environments where code cannot leave your Cloud perimeter, this is the cleanest setup.
Key Takeaways
- Use the managed Gemini Code Assist GitHub app for zero-config review. Use the
run-gemini-cliaction when you need custom checks, custom prompts or Cloud Build integration. - A reviewer needs only
contents: readandpull-requests: write. Never grant write access to code, and avoidpull_request_targeton untrusted forks. - Default to Gemini 3 Flash and escalate to Gemini 3 Pro on demand. Replace any pinned 2.5 model strings before they are retired.
- Add a
concurrencygroup withcancel-in-progress, and constrain the prompt to the diff, to keep cost predictable on busy PRs. - Start with an AI Studio API key. Move to Vertex AI with Workload Identity Federation once audit and data-residency questions come up.
- Keep the review prompt in the repo and tell the model what to skip. Mark every comment as automated so a human still owns the merge.
Quiz
Did the essentials stick?
3 questions, one at a time. Each one right on the first try brings the probe closer to the planet.
Question 1 of 3
What does it mean that the Gemini CLI runs 'headless' in CI, as opposed to its interactive terminal use?
Frequently asked questions
What's the difference between the Gemini Code Assist GitHub app and the run-gemini-cli action?
Gemini Code Assist for GitHub is the managed app: you install it from the Marketplace, it reviews PRs automatically and answers `/gemini review` or `/gemini summary` comments, with no YAML and no key to manage. The `google-github-actions/run-gemini-cli` action is the DIY path where you own the workflow, the model and the prompt. Choose the action when you need custom checks, like requiring a changelog entry or a rollback file, or when the review has to run inside Cloud Build.
What GitHub permissions does an automated Gemini PR review actually need?
Two: `contents: read` to read the code and `pull-requests: write` to post comments. Nothing else. Leave out `contents: write` so the job can never push code, `actions: write` so it can never edit other workflows, and `id-token: write` unless you are using Workload Identity Federation with Vertex AI.
Is it safe to run a Gemini review on pull requests from forks?
With the standard `pull_request` trigger, yes: a fork gets a read-only token and no access to your secrets, so the review simply cannot do damage. The danger is switching to `pull_request_target` to gain secret access, because the workflow then runs with write permissions against untrusted fork code, a well-known attack vector. Stay on `pull_request` and use GitHub's "require approval for outside collaborators" setting when a fork genuinely needs reviewing.
Should I use a Gemini API key or Vertex AI for CI code review?
Start with a Gemini API key from Google AI Studio stored as a repo secret: it is the fastest path for an individual or a small team. Move to Vertex AI with Workload Identity Federation once audit or data-residency questions come up, since the workflow exchanges its GitHub OIDC token for short-lived credentials and there is no long-lived secret to leak. Vertex AI also puts the review under your org's Cloud project, IAM, quotas and audit logs.
How do I stop automated Gemini PR reviews from getting expensive?
Three levers. Default to Gemini 3 Flash for routine review and escalate to Gemini 3 Pro only for multi-file refactors or tricky concurrency changes, often via a `deep-review` label. Add a concurrency group keyed on the PR number with `cancel-in-progress: true` so ten force-pushes trigger one review, not ten. Then constrain the prompt to `gh pr diff` rather than the whole repo, and tell the model to skip generated files and lockfiles.
Why does my Gemini PR review workflow return an empty diff?
Usually because of a shallow checkout. `actions/checkout` fetches only the tip commit by default, and `gh pr diff` needs enough history to compute the difference against the base branch. Set `fetch-depth: 0` on the checkout step; without it the failure looks like the model ignored your code.