tool-containers
Menu
GitHub (opens in new tab)

Run Claude Code with GitLab CI

gitlab-ci.yml defines a job that reviews each merge request with Claude Code on a GitLab Runner with the Docker executor (opens in new tab). It pipes the diff into claude -p (print mode) with --max-turns 10.

Images
  • GHCRghcr.io/hambn/claude-code
  • Docker Hubdocker.io/hambn/claude-code
Use case
Code review in CI on GitLab merge requests
Upstream
github.com/anthropics/claude-code (opens in new tab)
Source
tools/ai/claude-code/docs/gitlab-ci (opens in new tab)
On this page

Prerequisites

  • A GitLab project with a runner that uses the Docker executor, such as GitLab.com hosted Linux runners.
  • A masked ANTHROPIC_API_KEY CI/CD variable (opens in new tab).

Set up

Add the claude-code-review job to your project's .gitlab-ci.yml. It runs only in merge request pipelines.

The job runs ghcr.io/hambn/claude-code:ubuntu. Replace the tag with @sha256:<digest> to pin one build.

Variables

Variable Required Purpose
ANTHROPIC_API_KEY yes Masked CI/CD variable: Anthropic 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 claude entrypoint because the runner passes a shell as the command. The review covers $CI_MERGE_REQUEST_DIFF_BASE_SHA to HEAD.

Files

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.

File contents

gitlab-ci.yml17 lines
# Add this job to your project's .gitlab-ci.yml and set ANTHROPIC_API_KEY as a masked CI/CD variable.
claude-code-review:
  image:
    name: ghcr.io/hambn/claude-code: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"
    - ': "${ANTHROPIC_API_KEY:?Set ANTHROPIC_API_KEY as a masked CI/CD variable}"'
    - git diff "$CI_MERGE_REQUEST_DIFF_BASE_SHA" HEAD | claude -p --max-turns 10 "Review this merge request diff for bugs, security issues, and missing tests."

Topics