# CI Integration — ForgeDock PR Review in GitHub Actions

This guide explains how to integrate ForgeDock's `/review-pr` pipeline into GitHub Actions so every PR gets automatic multi-domain AI review without requiring a manual Claude Code session.

<!-- publish trigger: bot-token merges do not fire path-filtered workflows -->

---

## What This Does

The `forgedock-review.yml` workflow template triggers on every pull request (opened, synchronized, or reopened) and runs ForgeDock's full review pipeline:

- Security, auth, billing, database, concurrency, frontend, API, performance, and infrastructure agents all review the PR diff
- Review findings are posted as comments on the PR
- Each finding becomes a tracked GitHub issue in your repository
- No human needs to remember to run `/review-pr` — it happens automatically

---

## Prerequisites

1. **Anthropic API key** — Claude Code runs against the Anthropic API. Get one at [console.anthropic.com](https://console.anthropic.com).
2. **ForgeDock installed** — The workflow installs ForgeDock automatically via `npx forgedock install` (install is always global, so commands are available system-wide in the runner).
3. **GitHub Actions enabled** — Standard for all public repos; enabled by default for private repos.

---

## Setup

### 1. Add the API key as a repository secret

In your repository: **Settings → Secrets and variables → Actions → New repository secret**

| Name | Value |
|------|-------|
| `ANTHROPIC_API_KEY` | Your Anthropic API key (`sk-ant-...`) |

Never commit the API key to your repository. GitHub Actions masks secrets in logs, but only secrets stored via Settings are protected.

### 1a. Set the deploy-gate repository variable

If you use the `gate-marker-check` workflow (which enforces that every staging→main PR was reviewed by `/review-pr-staging` before merge), you must also set a **repository variable** that tells the gate which GitHub logins are authorized to post gate markers.

In your repository: **Settings → Secrets and variables → Actions → Variables → New repository variable**

| Name | Value |
|------|-------|
| `FORGE_TRUSTED_MARKER_AUTHORS` | Comma-separated list of GitHub logins authorized to run `/review-pr-staging` (e.g. `your-bot-login,your-personal-login`) |

**Why this is required**: The gate-marker-check workflow verifies that gate markers (`FORGE:GATE_PASS` / `FORGE:GATE_FAILURE`) were posted by a trusted author — not hand-typed by an external contributor. If this variable is unset or empty, the gate rejects every marker and blocks all staging→main PRs. The gate will fail immediately with setup instructions rather than waiting 30 minutes and producing a cryptic timeout error.

**What logins to include**: Add the GitHub login of the bot or user account that runs Claude Code sessions (the account that posts `/review-pr-staging` output). For automated CI setups this is typically the bot account; for manual runs it is your personal GitHub login. List a GitHub App bot both as `name` and `name[bot]`: comments report the first and the comment event the second.

### 1b. Require the `forge/deploy-gate` status on main

The workflow reports its verdict as the `forge/deploy-gate` commit status on the PR head:

| Newest counted trusted marker on the PR | `forge/deploy-gate` |
|---|---|
| `FORGE:GATE_PASS` (final staging-review verdict for the current head) | success |
| `FORGE:GATE_FAILURE` (for the current head, or naming no head) | failure |
| none | pending, at any age |

A pass counts only when it is the review's final verdict (`FORGE:GATE_PASS:TYPE=staging-review`) and its `**Head**:` line is the PR's current head. The review's pre-dispatch gate posts an earlier, plain `GATE_PASS` before any reviewer has run, and that one does not count. A push to staging after the review leaves the status pending until a delta review passes the new head.

A posted marker re-runs the check, so the status follows the review. The workflow's own job check is not the gate. It stays green while a review runs, so the staging review's CI read is not blocked by the gate it is about to satisfy.

The status blocks nothing until main requires it. In **Settings → Rules → Rulesets** (or branch protection for `main`), add `forge/deploy-gate` as a required status check. GitHub then refuses to merge a staging→main PR that has no passing review on record, including a merge by hand. The gate covers `staging`, `feature` and `release/<id>` (snapshot release) heads into `main`.

### 2. Copy the workflow template

Copy `templates/workflows/forgedock-review.yml` from your ForgeDock installation to your repository:

```bash
# From your repository root
mkdir -p .github/workflows
cp "$(npx forgedock which-dir)/templates/workflows/forgedock-review.yml" .github/workflows/forgedock-review.yml
```

Or copy it manually — the file is in `~/.claude/commands/../templates/workflows/forgedock-review.yml` after running `npx forgedock install`.

### 3. Commit and push

```bash
git add .github/workflows/forgedock-review.yml
git commit -m "feat(ci): add ForgeDock automated PR review workflow"
git push
```

The workflow activates immediately. Open or synchronize any PR to trigger the first review.

---

## Security Model

### API key handling

The `ANTHROPIC_API_KEY` is passed exclusively via the `env:` block in the workflow YAML. It is never:
- Echoed to logs with `echo` or `printf`
- Passed as a CLI argument (visible in process lists)
- Written to disk during the workflow run

GitHub Actions masks the secret value in all log output automatically.

### GITHUB_TOKEN scope

The workflow uses the built-in `GITHUB_TOKEN`, which is:
- Automatically provided by GitHub Actions for every workflow run
- Scoped to the repository
- Revoked after the workflow run completes
- Constrained to `contents: read` and `pull-requests: write` by the `permissions:` block

No additional tokens or personal access tokens are required.

### Runner isolation

Each workflow run uses a fresh ephemeral GitHub-hosted runner (`ubuntu-latest`). The `--dangerously-skip-permissions` flag passed to Claude Code is safe in this context because:
- The runner is discarded after the job completes
- No persistent state is left behind
- The workflow's `permissions:` block constrains what the `GITHUB_TOKEN` can do

---

## Fork PR Limitation

**Pull requests from forks do not have access to repository secrets.**

This is a GitHub security restriction: a forked repository's PR cannot read secrets from the upstream repository's Settings. The `pull_request` event that triggers this workflow will have `ANTHROPIC_API_KEY` as an empty string for fork PRs, and the Claude Code step will fail silently or error.

### Option 1: Accept the limitation (recommended for open-source repos)

Leave the workflow as-is. Team members' PRs get reviewed automatically. External contributor PRs are reviewed manually when a maintainer runs `/review-pr` in Claude Code.

### Option 2: Use `pull_request_target` (advanced — security implications)

`pull_request_target` runs in the context of the base repository (not the fork), so secrets are available. However, it also runs untrusted code from the fork in a privileged context, which is a security risk if the workflow checks out and runs code from the fork.

If you use `pull_request_target`, you MUST:
1. Never check out the fork's code and run it
2. Scope what Claude Code can access carefully
3. Review the [GitHub security hardening guide for `pull_request_target`](https://securitylab.github.com/research/github-actions-preventing-pwn-requests/)

This is not recommended without understanding the security implications.

---

## `forgedock run` Execution Backend

This guide's `forgedock-review.yml` workflow invokes `/review-pr` directly via the `claude --print` CLI, so it always needs `ANTHROPIC_API_KEY` as shown above — the CLI itself authenticates against your Anthropic account.

Separately, `npx forgedock run <command>` (the standalone, non-Claude-Code command runner in `bin/runner.mjs`) has its own **backend selection**, independent of this CI workflow:

| Backend | When used | Credential needed |
|---------|-----------|--------------------|
| `cli` (via `--backend cli` or `FORGEDOCK_BACKEND=cli`) | Explicit, or auto-selected when a working `claude` CLI is detected on PATH | None — reuses the CLI's own authentication (Pro/Max OAuth or a CLI-managed key) |
| `api` (via `--backend api` or `FORGEDOCK_BACKEND=api`) | Explicit only, or the `auto` fallback when the owner opted in (forge.yaml `execution.agent_backend: api` plus `FORGEDOCK_ALLOW_API_BACKEND=1`) | `ANTHROPIC_API_KEY` |
| `auto` (default) | No `--backend` / `FORGEDOCK_BACKEND` given | Uses `cli`; never falls back to `api` without the owner opt-in. With no CLI it fails with `CLI_BACKEND_UNAVAILABLE` |

**When CI still needs `ANTHROPIC_API_KEY` for `forgedock run`**: CI runners generally do not have an interactively-authenticated `claude` CLI (no browser/OAuth flow available in a headless job), so `auto` there fails with `CLI_BACKEND_UNAVAILABLE`: the API backend is never the default, because it bills an API key (#4257). If your CI runner does install and log in the `claude` CLI (e.g. via a headless API-key-based login), you can force `--backend cli` to skip the SDK/API path entirely. Otherwise, keep `ANTHROPIC_API_KEY` configured as a repository secret (see Setup above) and set `--backend api` / `FORGEDOCK_BACKEND=api` explicitly, or opt in to the `auto` fallback with forge.yaml `execution.agent_backend: api` plus `FORGEDOCK_ALLOW_API_BACKEND=1`.

`npx forgedock run <command> --dry-run` always reports which backend it resolved to, without making any network call or requiring a key either way.

---

## Customization

### Skip review for certain PRs

Add conditions to the `if:` field on the job:

```yaml
# Skip bot PRs and draft PRs
if: |
  github.actor != 'dependabot[bot]' &&
  github.actor != 'github-actions[bot]' &&
  github.event.pull_request.draft == false
```

### Target only certain branches

```yaml
on:
  pull_request:
    types: [opened, synchronize, reopened]
    branches:
      - main
      - staging
      - 'milestone/**'
```

### Pass additional flags to /review-pr

The `claude --print` invocation accepts `/review-pr` arguments. For example, to skip auto-merge (default) or pass a custom flag:

```yaml
run: |
  claude --print "/review-pr $PR_NUMBER --model opus" \
    --dangerously-skip-permissions
```

### Enable commit status badge

Uncomment the `Set Commit Status` step in the workflow template to add a pass/fail badge to PRs. This uses the GitHub API via `actions/github-script` to post a commit status after the review completes.

---

## Superseded runs

A push to a PR branch makes the runs of the previous head useless, but GitHub lets them finish. On ForgeDock's own
release PRs, 39% of CI job minutes over 7 days (2026-11-08 to 2026-11-15) went to jobs that were then cancelled. Two ways to stop paying for it:

**In the workflow (preferred, no API calls).** Add a `concurrency` block to every workflow that only verifies a head:

```yaml
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}
```

The group is per workflow and per PR, so a new push cancels only the older run of the same workflow on the same PR.
`cancel-in-progress` is limited to pull-request events: a push or dispatch run on a protected branch may be a deploy, and
a deploy must never be cancelled half way. Do not add the block to a deploy workflow; give that one a group with
`cancel-in-progress: false` so deploys queue instead.

**From ForgeDock (when the workflow cannot be edited).** `verify.cancel_superseded: own` in forge.yaml makes a lane cancel
the pull-request runs of its own branch for an older head right after it pushes (default `off`; see
[CONFIG.md](CONFIG.md#verify-optional)). For any branch or PR, by hand or from a hook:

```bash
npx forgedock ci cancel-superseded --pr 123 --dry-run   # list what would be cancelled
npx forgedock ci cancel-superseded --pr 123 --mode own  # cancel the runs this actor triggered
```

It cancels only queued or running `pull_request` / `pull_request_target` runs whose head sha is not the branch's current
head. The checks of the current head are untouched, so no gate gets weaker. Measure the effect with
`forgedock stats --release-ledger` (row "CI minutes in cancelled jobs").

## Troubleshooting

**Review job fails immediately with `ANTHROPIC_API_KEY not set`**
→ The secret is not configured. Go to Settings → Secrets and variables → Actions and add `ANTHROPIC_API_KEY`.

**Review passes but no comments appear on the PR**
→ Check that `permissions: pull-requests: write` is present in the workflow. Without it, the `gh` CLI calls from `/review-pr` will receive 403 errors.

**Fork PRs show empty `ANTHROPIC_API_KEY`**
→ Expected behavior. See the Fork PR Limitation section above.

**`claude: command not found`**
→ The `npm install -g @anthropic-ai/claude-code` step failed. Check the job logs. The `ubuntu-latest` runner includes Node.js but the global npm install may fail if the runner's Node version is too old. Ensure the `Set up Node.js` step runs first and targets Node.js 18+.

**`npx forgedock install` reports `command not found: forgedock`**
→ `npx forgedock install` runs the installer from npm. If it fails, verify the ForgeDock package name is correct: `npm show forgedock version` should return a version number.

**`claude: unknown flag --output-format` or similar unrecognized flag error**
→ Your local Claude Code installation is outdated. The `--output-format` flag is available in all recent versions of Claude Code. Run `npm update -g @anthropic-ai/claude-code` to update to the latest version. Note: the workflow template itself no longer passes `--output-format` (the flag was redundant — `--print` defaults to text output). If you copied the template before this fix, remove the `--output-format text` line from your workflow file.
