How do you run Claude Code in a GitLab CI/CD job, and why doesn't an @claude comment start that job by itself?
answer
- no packaged action, a job you write
- masked CI/CD variable for the key
- claude -p with tools pre-approved
- mentions need your own listener
- AI_FLOW_* variables through the trigger API
basics
~20 sIn GitLab you add a job to .gitlab-ci.yml that installs Claude Code and runs claude -p with pre-approved tools, using a masked ANTHROPIC_API_KEY variable. A comment starts nothing until your webhook listener calls the pipeline trigger API.
solid answer
~40 sClaude Code for GitLab CI/CD is in beta and maintained by GitLab. There is no packaged action: you add a job to `.gitlab-ci.yml` that installs Claude Code with the install script and runs `claude -p` with the task, and you store `ANTHROPIC_API_KEY` as a masked CI/CD variable. The documented job pre-approves tools with `--permission-mode acceptEdits --allowedTools "Bash Read Edit Write mcp__gitlab"`, because nobody can answer a prompt in a pipeline. For GitLab API work it uses `CI_JOB_TOKEN` by default, or a Project Access Token with `api` scope stored as `GITLAB_ACCESS_TOKEN`. Mention-driven runs are not built in: a project webhook for comments (notes) must reach a listener you run, which calls the pipeline trigger API with variables such as `AI_FLOW_INPUT` and `AI_FLOW_CONTEXT`. Bound each run with `--max-turns` and the job's `timeout`.
code
yaml · 18 linesclaude:
stage: ai
image: node:24-alpine3.21
timeout: 30m
rules:
- if: '$CI_PIPELINE_SOURCE == "web"'
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
before_script:
- apk add --no-cache git curl bash
- curl -fsSL https://claude.ai/install.sh | bash
- export PATH="$HOME/.local/bin:$PATH"
script:
- >
claude
-p "${AI_FLOW_INPUT:-'Review this MR and implement the requested changes'}"
--permission-mode acceptEdits
--allowedTools "Bash Read Edit Write mcp__gitlab"
--max-turns 15go deeper
Recall that GitLab uses a job you add to .gitlab-ci.yml running claude -p, with the API key in a masked CI/CD variable.
Explain the job's parts: rules, installing the CLI, pre-approved tools, CI_JOB_TOKEN or a Project Access Token, and why mentions need a webhook listener and the trigger API.
Bound and secure it: --max-turns and the job timeout, OIDC through id_tokens instead of static cloud keys, and merge requests as the review gate.
Decide whether a beta integration maintained by GitLab is acceptable to own, including running the comment listener as a service of your own.
## GitLab is a job, not an app On GitHub, Claude Code arrives as a packaged action plus an app that watches for `@claude`. GitLab has neither. **Claude Code for GitLab CI/CD** is a documented pattern, in **beta** and **maintained by GitLab**, in which an ordinary pipeline job installs the Claude Code CLI and runs it non-interactively. Everything an action would hide, from the trigger and the credentials to the tool grants and the limits, is yours to write. | Concern | GitHub action | GitLab CI/CD job | | - | - | - | | Packaging | `anthropics/claude-code-action@v1` | a job in `.gitlab-ci.yml` you write | | Credential | repository secret | masked CI/CD variable `ANTHROPIC_API_KEY` | | Mention trigger | built in, `@claude` | your webhook listener plus the trigger API | | Write-back | the Claude GitHub App | `CI_JOB_TOKEN` or a Project Access Token | ## The job The documented quick-setup job does four things: 1. **Selects when it runs** with `rules`, for example `$CI_PIPELINE_SOURCE == "web"` for a manual run from the Pipelines page or `"merge_request_event"` for merge request pipelines. 2. **Installs Claude Code** in `before_script` with the install script, then adds `~/.local/bin` to `PATH`, since the installer puts `claude` there. 3. **Runs Claude non-interactively** with `claude -p`, taking the task from `AI_FLOW_INPUT` when a trigger passed one and falling back to a default prompt. 4. **Pre-approves its tools**: `--permission-mode acceptEdits` lets it edit files, and `--allowedTools "Bash Read Edit Write mcp__gitlab"` lists the tools it may use without asking, including a GitLab MCP server's tools if your setup starts one. The pre-approval is not optional decoration. A pipeline has no person to answer a permission prompt, so in a `-p` run any call that would need one is denied, and an edit or command Claude was never cleared for simply does not happen. What each permission mode allows, and how `-p` runs behave in general, belong to the permission-mode and headless material. ## Credentials - **Claude API**: `ANTHROPIC_API_KEY` as a **masked** CI/CD variable, protected where needed, never committed. - **Amazon Bedrock / Google Cloud's Agent Platform**: no static key. An `id_tokens:` block makes GitLab mint `GITLAB_OIDC_TOKEN`, which the job exchanges for short-lived cloud credentials; the job sets `CLAUDE_CODE_USE_BEDROCK` or `CLAUDE_CODE_USE_VERTEX` to `1`, and `ANTHROPIC_API_KEY` is not used. - **GitLab API operations**: `CI_JOB_TOKEN` by default, or a Project Access Token with `api` scope stored as the masked variable `GITLAB_ACCESS_TOKEN`. ## Mention triggers are yours to build The usual surprise: someone comments `@claude implement this` on an issue and nothing happens. GitLab does not start pipelines on comments by itself. The documented optional setup is: - add a **project webhook for "Comments (notes)"** pointing at an event listener you run; - have the listener, when a comment contains `@claude`, call the **pipeline trigger API** with variables such as `AI_FLOW_INPUT` (the request) and `AI_FLOW_CONTEXT` (where it came from); - let the job read those variables and build its prompt from them. Without that listener, the job runs only on the events its `rules` match: manual web runs, merge request events, or API triggers. ## Bounding a run The documented job carries no equivalent of the GitHub action's checks on who triggered a run, so who can hand it a task is decided by your `rules`, your listener, the trigger token and who may run pipelines; treat all of them as access control. Each job consumes runner minutes and API tokens. The docs' levers: - `--max-turns` on the `claude` command line to cap iterations; - GitLab's job-level `timeout` keyword, for example `timeout: 30m`, to cap wall-clock time; - limited concurrency so parallel runs stay bounded; - a concise `CLAUDE.md`, which Claude reads on every run, and specific requests that need fewer turns. Every change still flows through a merge request, so branch protection and approvals apply to what Claude writes. ## Troubleshooting - **Job never starts on a comment**: check the webhook, the listener and the trigger call. - **Job cannot comment or open an MR**: check the token's permissions, or use a Project Access Token with `api` scope; confirm `mcp__gitlab` is in `--allowedTools`; confirm the job has merge request context or `AI_FLOW_*` variables. - **Authentication errors**: confirm the key is valid, or the OIDC role, region and model access for a cloud provider.
- The Claude job runs in GitLab but cannot comment or open a merge request. What do you check?First the GitLab credential: `CI_JOB_TOKEN` may lack the needed permissions, so use a Project Access Token with `api` scope stored as `GITLAB_ACCESS_TOKEN`. Then confirm `mcp__gitlab` is listed in `--allowedTools`, since a call that would need approval is denied in a `-p` run, and that the job runs with merge request context or receives `AI_FLOW_*` variables.
- How do you run the GitLab job on Amazon Bedrock without storing a cloud key?Add an `id_tokens:` block so GitLab mints `GITLAB_OIDC_TOKEN` with the audience your AWS identity provider expects, exchange it with `aws sts assume-role-with-web-identity` for temporary credentials, and set `CLAUDE_CODE_USE_BEDROCK: "1"` and `AWS_REGION`. The IAM role's trust policy should be restricted to your project and protected refs; `ANTHROPIC_API_KEY` is not used.
- Commenting @claude on a GitLab issue starts the job with no extra wiring
- /install-github-app also sets up GitLab repositories
- Tool pre-approval is pointless because a pipeline never shows a prompt
- The GitLab integration is generally available and maintained by Anthropic
- A private project can keep ANTHROPIC_API_KEY inside .gitlab-ci.yml