Pick the right command
Most jobs want the second or the fourth row. Reach for streaming input only when the pipeline really does need to send more than one message and react in between.
Authenticate the job
Credentials resolve in a fixed order:CORTEX_AUTH_TOKEN first, then CORTEX_API_KEY, and only then the credential cortex login stored. A CI runner has no credential store and no browser, so cortex login cannot complete its interactive flow there. An environment variable is the only path that works, and because it is checked first it also wins on a machine that does have a stored sign-in.
Set the secret on the job or step, from your provider’s secret store, and never bake a token into an image:
Install the CLI in the job
Install from the release host as a job step, then check the version so a broken install fails early rather than mid-run:GitHub Actions
fetch-depth: 0. The default checkout is shallow, and an agent cannot diff against a base branch it does not have. This is the single most common cause of “the agent cannot see my changes”.env:at the step level, so the secret is exposed to the one step that needs it.- A JSON output format, because the default text shape is not a contract and will break your parser one day.
--timeoutand--max-turnson every invocation. Defaults exist, 600 seconds and 100 turns, but a pipeline should state its own.
cortex github install scaffolds workflows for pull-request review and issue automation if you would rather not maintain the YAML yourself. See GitHub.
GitLab CI
A shell script
The same shape works in any runner, and it is worth keeping the early exit: a job that had nothing to review should pass, not spend a turn.Decide the autonomy before the run
In CI there is nobody to answer an approval prompt, so what the run may do has to be settled by policy in advance. Three controls do that, and they compose:--auto, the level for this run,read-onlyby default..cortex/permissions.toml, committed to the repository, pinning individual rules. First matching rule wins, and deny beats allow.- The
[permission]table in configuration, which can set a capability toallow,askordeny. A project.cortex/config.tomlcarries these and beats the user’s own configuration, which makes it the right place to pin what CI may do for a given repository.
[permission] table above. See Hooks and Permission policy.
Restrict network egress
Network egress is blocked by default in a headless run. When you want a specific, reviewable restriction for a repository, commit a.cortex/sandbox.toml to it: it restricts egress for runs in that repository, and because it is in the repository, every CI run inherits the same rules without anyone remembering to pass a flag.
Fail the job on the exit code
Branch on the exit status, not on prose in the output:0 means the task completed. Any non-zero exit means the run failed, was interrupted, was truncated, or the coding service was unreachable. That is the contract, so do not wrap a failing run in || true. The JSON result document tells the cases apart if the job needs to know which one it was.
Two small things while you are here. --dry-run belongs to cortex run rather than cortex exec, and it previews what would be sent, with estimated token counts, without executing: the cheap way to test a pipeline change. And -n/--notification, also on cortex run, raises a desktop notification, which is pointless on a runner.
Driving a multi-turn run
For a pipeline that sends more than one message,cortex exec accepts a streaming input format: one JSON object per line on standard input, plus two control messages.
stream-jsonl: every non-empty line is the next turn, the stream needs no envelope or ids, and the connection stays open after a turn completes. --input-format stream-jsonrpc is the JSON-RPC variant instead, for a pipeline that wants request ids and matching responses.
Troubleshooting
If the run reports that the coding service is temporarily unavailable, that message is the whole story: retry the job. See CLI troubleshooting.
Related
- Headless and one-shot runs, the two commands and their flags
- Permission policy, rules that decide an unattended run’s authority
- Sign in to the CLI, credentials for a runner
- Cloud, This PC, and SSH, where a CI run executes its tools
- Cortex Security, a review that runs on pull requests without a pipeline of your own