Codex
Runs the Codex CLI headless in CI jobs, in its sandbox and without approval prompts.
Usage
include:
- project: hosstio/gitlab-pipelines
file: helpers/codex.yamlVariables
| Variable | Default | Description |
|---|---|---|
AGENT_CODEX_IMAGE | "" | An image with the Codex CLI, glab, Git and jq; see Base |
AGENT_CODEX_MODEL | "" | Model to use; empty uses the CLI's default |
AGENT_CODEX_EFFORT | "" | Reasoning effort, such as low, medium, high or xhigh; the levels depend on the model, and empty uses the model's default |
AGENT_CODEX_SANDBOX | danger-full-access | Where the commands Codex runs may write: read-only, workspace-write or danger-full-access |
AGENT_CODEX_OUTPUT_FILE | /tmp/codex-output.jsonl | Where the JSON events of the last run are written |
AGENT_CODEX_RUNS_FILE | /tmp/codex-runs.jsonl | One line of usage per run, which the report reads |
AGENT_CODEX_TRANSCRIPT_FILE | /tmp/codex-transcript.jsonl | Every event of every run, which the report copies to the artifacts |
CODEX_API_KEY | OpenAI API key; set as a masked CI/CD variable | |
CODEX_ACCESS_TOKEN | Access token of a ChatGPT Business or Enterprise workspace, for runs on the workspace's plan instead of the API |
Base (.codex)
Base job that runs $AGENT_CODEX_IMAGE and prints the Codex CLI version. The variable is empty, because no image these templates point at ships the Codex CLI yet, so a job that extends .codex needs an image of your own. Pin the Codex CLI in it, so a job doesn't change behavior when a new release comes out.
The image's entrypoint is off (entrypoint: [""]), the same as in the Claude helper, so a job runs docker-entrypoint.sh itself once it has exported GITLAB_TOKEN.
Variables
| Variable | Default | Description |
|---|---|---|
AGENT_ASSISTED_BY | Codex | Tool name in the Assisted-by trailer that the agent jobs add to every commit |
AGENT_CODEX_SLACK_USER | "" | Sender name of the job's Slack posts; empty keeps SLACK_MESSAGE_USER |
AGENT_CODEX_SLACK_ICON_URL | "" | Public URL of the sender's icon, such as a GitLab user avatar (https://gitlab.com/uploads/-/system/user/avatar/<user id>/<file>); empty keeps SLACK_MESSAGE_USER_ICON_URL |
Its before_script exports these two as SLACK_MESSAGE_USER and SLACK_MESSAGE_USER_ICON_URL, which the Slack helper sends, so a job that extends .codex and posts with .slack_message shows the agent's name and icon. Both need the chat:write.customize scope on SLACK_AUTHENTICATION_TOKEN.
Run (.codex_run)
Defines codex_run "<prompt>" [extra args…]. It skips with a warning when neither CODEX_API_KEY nor CODEX_ACCESS_TOKEN is set, then runs codex exec --json with AGENT_CODEX_MODEL, AGENT_CODEX_EFFORT and AGENT_CODEX_SANDBOX, and with approval_policy set to never, since nobody answers an approval request in CI. Any extra arguments go to codex exec before the prompt, such as --add-dir /repositories.
While Codex works, the job log shows one line per shell command, file change, MCP tool call and web search, such as Codex > Shell: glab mr view 70 --comments or Codex > File update: charts/yopass/Chart.yaml, without the output. The events go to AGENT_CODEX_OUTPUT_FILE and AGENT_CODEX_TRANSCRIPT_FILE.
After each run it appends one line to AGENT_CODEX_RUNS_FILE with the model setting, the effort, the turns, the duration and the input, cached input, output and reasoning tokens. It then calls codex_result, which fails on output that isn't JSON, a failed turn, or a run without an answer, and otherwise exports Codex's last message as AGENT_CODEX_RESULT.
Codex has no budget or turn cap, so only the job's timeout: bounds a run.
The sandbox applies to the commands Codex runs. Codex runs them with bwrap, which can't create a namespace in a default Docker container, so in read-only and workspace-write every command fails with bwrap: No permissions to create a new namespace. The default is danger-full-access, where only the job's container limits what Codex can do: it acts with the GitLab token's rights and can use every CI/CD variable of its job. On a runner whose containers allow user namespaces, read-only lets the commands write nothing and workspace-write only the checkout, and both cut them off from the network, which glab, git push and Docker need.
Examples
summarize:
extends: .codex
script:
- !reference [.log_helpers, script]
- !reference [.codex_run, script]
- codex_run "Summarize the last 10 commits"Report (.codex_report)
.codex runs codex_report in its after_script, so it also runs when the job fails. It turns AGENT_CODEX_RUNS_FILE into a table with one row per run and a total row when there were several, in the job log and in codex-report.md. A merge request links that file as "Codex report", next to codex-transcript.jsonl, the events of every run, for members with at least the Developer role (access: developer), for 30 days. The Codex CLI reports tokens, not cost, so the table has no cost column.