A release job runs `gemini -p "update CHANGELOG.md from the commits since the last tag"` with Gemini CLI 0.62.0 and changes nothing - why, and what do you change?
answer
- nobody there to answer a prompt
- ask becomes deny when headless
- the checkout is not trusted either
- auto_edit plus a narrow policy rule
- json output, exit codes 42 and 53
basics
~10 sHeadless runs cannot ask, so write_file, replace and run_shell_command are denied in the default mode, and the untrusted checkout forces that mode anyway. Trust it with --skip-trust and pick --approval-mode=auto_edit or a policy rule.
solid answer
~40 s`-p` puts Gemini CLI in non-interactive mode, where a call that would ask is denied: the built-in rules deny `write_file`, `replace`, `run_shell_command` and `web_fetch` outright when headless, so in `default` the agent can read and report but not edit. A headless run also cannot show the trust dialog, so the checkout is untrusted unless you pass `--skip-trust` or set `GEMINI_CLI_TRUST_WORKSPACE=true`, and an untrusted folder forces the approval mode back to `default`. For your own repository's pipeline: trust the workspace, use `--approval-mode=auto_edit` for edits, allow the one `git log` command it needs with a policy rule passed via `--policy`, authenticate with `GEMINI_API_KEY` or Vertex AI variables, and read `--output-format json` plus the exit code (0 success, 1 error, 42 input error, 53 turn limit).
code
bash · 9 lines# GEMINI_API_KEY comes from the CI secret store for this step only
gemini -p "Update CHANGELOG.md from the commits since the last tag, then stop" \
--skip-trust \
--approval-mode=auto_edit \
--policy ci/gemini-changelog.toml \
--output-format json > gemini-result.json
status=$?
jq -r '.response' gemini-result.json
exit "$status"go deeper
Know that gemini -p runs one prompt without the interactive UI and that it cannot stop to ask for approval.
Explain ask-becomes-deny in headless mode, what --output-format json and stream-json return, and the four exit codes.
Diagnose the empty-diff run: headless deny rules plus an untrusted checkout forcing default; fix it with --skip-trust, auto_edit and a narrow --policy rule.
Decide which pipelines may trust a checkout at all, and standardise on narrow policy files and sandboxed runners instead of yolo across teams.
## What `-p` changes `gemini -p "..."` (or `--prompt`) forces Gemini CLI into **non-interactive (headless) mode**; so does running it without a TTY. The prompt is appended to anything piped on stdin, so `git diff | gemini -p "review this"` works. In a real terminal a bare positional prompt (`gemini "update the changelog"`) now starts an *interactive* session with that first message, and `-i` does the same explicitly; only `-p` guarantees a one-shot run when you test the command by hand. There is nobody to click Allow, so the **policy engine** treats every `ask_user` decision as `deny`, and the built-in rules go further: a headless rule denies `replace`, `run_shell_command`, `write_file`, `activate_skill` and `web_fetch` whenever the run is non-interactive. Tools denied that way are dropped from what the model is offered at all. In the `default` approval mode that leaves the agent with read and search tools only. It reads the repository, describes the changelog entry it would write and finishes - exit code 0, nothing edited. ## The second cause: trust A headless run cannot display the folder-trust dialog either. Unless you pass `--skip-trust` or set `GEMINI_CLI_TRUST_WORKSPACE=true`, the workspace is not trusted, and an untrusted workspace: - ignores the repository's `.gemini/settings.json` and `.env`; - connects no MCP servers; - forces the approval mode back to `default`, so even `--approval-mode=auto_edit` has no effect. The trusted-folders page documents a headless run in an untrusted folder exiting with `FatalUntrustedWorkspaceError`. Either way, the fix is an explicit, deliberate trust decision - appropriate for your own repository on your own branch, not for a job that runs on pull requests from outside the team. ## What to change | Need | Setting | |---|---| | Trust this checkout for the run | `--skip-trust` or `GEMINI_CLI_TRUST_WORKSPACE=true` | | Let it edit files | `--approval-mode=auto_edit` (its allow rules beat the headless deny) | | Let it run exactly one command | A policy rule with `decision = "allow"` and a `commandPrefix`, passed with `--policy` | | Let it run anything | `--approval-mode=yolo`, only inside a sandbox (`-s` or `GEMINI_SANDBOX=docker`) | | Authenticate without a browser | `GEMINI_API_KEY`, or Vertex AI via `GOOGLE_CLOUD_PROJECT`, `GOOGLE_CLOUD_LOCATION` and `GOOGLE_APPLICATION_CREDENTIALS` | A narrow policy file keeps shell access to the command you meant: ```toml [[rule]] toolName = "run_shell_command" commandPrefix = "git log" decision = "allow" priority = 100 ``` Files given with `--policy` load at the **user** tier. A policy in the repository's own `.gemini/policies` is the workspace tier, which the policy-engine page currently documents as non-functional, so do not rely on it. The CLI reference marks the older `--allowed-tools` flag deprecated in favour of the policy engine. ## Reading the result `--output-format` (`-o`) takes three values: 1. `text` - the default, the final answer as plain text. 2. `json` - one object with `response` (the final answer), `stats` (token usage and latency) and, on failure, `error`. 3. `stream-json` - newline-delimited events: `init`, `message`, `tool_use`, `tool_result`, `error`, `result`. Use it when the job must log each tool call as it happens. The headless page lists the exit codes a script should branch on: `0` success, `1` general or API error, `42` input error (an invalid prompt or arguments), `53` turn limit exceeded. The turn limit is `model.maxSessionTurns`, unlimited (`-1`) by default, so setting it gives a runaway run a distinct, checkable failure. ## A checklist for the pipeline - Store the API key in the CI secret store and expose it only to this step. - Trust the workspace explicitly and only on trusted branches. - Grant edits with `auto_edit`; grant commands one prefix at a time. - Fail the step on a non-zero exit code, and treat "exit 0, empty diff" as a signal to check permissions before blaming the model. - Check the result in the repository, not in the reply: a `git diff --exit-code` after the step tells you whether anything changed, whatever the `response` text claims. - Keep the prompt narrow and say when to stop; a headless run has no one to redirect it halfway through.
- Why not just add --approval-mode=yolo to the CI step?yolo approves tool calls wholesale, including any shell command the model decides on after reading untrusted text such as an issue or a dependency's README. If you need it, run it inside a sandbox with `-s` or `GEMINI_SANDBOX=docker` on a throwaway runner. Usually `auto_edit` plus a one-command policy rule does the job with far less reach.
- The same command edits files on a developer's laptop but not in CI. What differs?The laptop has a cached Google sign-in and a trust decision recorded in `~/.gemini/trustedFolders.json` from an earlier dialog. The runner has neither: it needs `GEMINI_API_KEY` or Vertex AI variables to authenticate, and `--skip-trust` or `GEMINI_CLI_TRUST_WORKSPACE=true`, or the approval mode falls back to `default`.
- A headless gemini -p run waits on stdin for each approval.
- Headless runs approve edits automatically because nobody can object.
- The repository's .gemini/settings.json always applies in CI.
- Exit code 0 proves the agent changed the files it was asked to.
- --allowed-tools is the current way to pre-approve a shell command.
- A policy in the repository's .gemini/policies reliably grants CI permissions.
Assumes Gemini CLI 0.62.0: the headless deny rules, the forced default mode in untrusted folders, the --policy user tier and the exit codes are as that release defines them.