Run Codex CLI with GitLab CI
gitlab-ci.yml defines a job that reviews each merge request with
Codex on a GitLab Runner with the Docker executor (opens in new tab). It
pipes the diff into codex exec, which treats piped input as context for the prompt.
- Images
- GHCR
ghcr.io/hambn/codex - Docker Hub
docker.io/hambn/codex
- GHCR
- Use case
- Code review in CI on GitLab merge requests
Prerequisites
- A GitLab project with a runner that uses the Docker executor, such as GitLab.com hosted Linux runners.
- A masked
CODEX_API_KEYCI/CD variable (opens in new tab).
Set up
Add the codex-review job to your project's .gitlab-ci.yml. It runs only in merge
request pipelines.
The job runs ghcr.io/hambn/codex:ubuntu. Replace the tag with @sha256:<digest> to pin one build.
Variables
| Variable | Required | Purpose |
|---|---|---|
CODEX_API_KEY |
yes | Masked CI/CD variable: OpenAI API key. codex exec reads this variable, not OPENAI_API_KEY. |
Workspace
The runner clones the full history (GIT_DEPTH: "0") into $CI_PROJECT_DIR and runs
the script as the image user, sysadmin (UID 1000). The clone belongs to a different
user, so the first script line marks it as a Git safe directory. The job clears the
image's codex entrypoint because the runner passes a shell as the command. The
review covers $CI_MERGE_REQUEST_DIFF_BASE_SHA to HEAD.
Files
gitlab-ci.yml: merge request review job.
Limitations
- Merge request pipelines that run in a fork do not get the parent project's CI/CD variables, so the job stops at the variable check.
- The review is advisory: the job does not comment on the merge request or fail on findings. Read it in the job log.
- The diff is untrusted model input, and the job environment, including
CI_JOB_TOKEN, is visible to any command the agent runs. - Codex runs with
--sandbox danger-full-accessbecause its own sandbox may not work inside a container, as the Codex security docs (opens in new tab) describe. The container is the only isolation: it can reach the network, and a crafted change can make Codex readCODEX_API_KEY.
File contents
gitlab-ci.yml18 lines
# Add this job to your project's .gitlab-ci.yml and set CODEX_API_KEY as a masked CI/CD variable.
codex-review:
image:
name: ghcr.io/hambn/codex:ubuntu
# The runner passes a shell as the command, so clear the image's CLI entrypoint.
entrypoint: [""]
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
variables:
# A full clone keeps the merge request's diff base commit available.
GIT_DEPTH: "0"
timeout: 30m
script:
# The runner clones as a different user than sysadmin, so Git needs the path marked safe.
- git config --global --add safe.directory "$CI_PROJECT_DIR"
- ': "${CODEX_API_KEY:?Set CODEX_API_KEY as a masked CI/CD variable}"'
# Codex's own sandbox may not work inside a container; the job container isolates it.
- git diff "$CI_MERGE_REQUEST_DIFF_BASE_SHA" HEAD | codex exec --sandbox danger-full-access "Review this merge request diff for bugs, security issues, and missing tests."