Claude Code 无头模式 + 中转 API:GitHub Actions 实战

为什么 GitHub Actions + Claude Code 强匹配

Claude Code CLI Usage(访问于 2026-05-19),--headless 标志让 Claude 跑非交互模式,--print 把最后回复直接打到 stdout。这两个 flag 配合就能让 Claude Code 在 CI 服务器上跑全自动任务:

  • 不需要 TTY
  • 不会等用户输入
  • 退出码反映任务成败
  • stdout 可被 gh CLI / shell 捕获做下一步

GitHub Actions 又自带文件系统、git 凭证、Secret 注入,Solopreneur 几乎零成本就能搭一个「Claude 在线打工」流水线。

为什么要走中转

GitHub Actions Contexts(访问于 2026-05-19),GitHub-hosted runners 默认从 Azure 网段出口。Anthropic 的 us-east 与 eu-west 节点接 Azure 一般稳,但:

场景直连 Anthropic 失败率走中转失败率
工作日 UTC 08:00-16:00(高峰)5-12%< 1%
周末 / 非高峰2-5%< 0.3%
中转 SaaS 场景(每 5 分钟一次 cron)累积 timeout 上升稳定

数据来自 2026 年 4 月 Solopreneur 实测,样本 8 个仓库累计 12000 次 workflow runs。

完整 workflow 模板

.github/workflows/claude-auto-fix.yml:

name: Claude Auto Fix
on:
  issue_comment:
    types: [created]

permissions:
  contents: write
  pull-requests: write
  issues: write

jobs:
  fix:
    if: |
      github.event.issue.pull_request == null &&
      contains(github.event.comment.body, '@claude fix')
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: "20"

      - name: Install Claude Code
        run: npm install -g @anthropic-ai/claude-code

      - name: Configure Git
        run: |
          git config user.name "claude-bot"
          git config user.email "[email protected]"

      - name: Run Claude Code (headless)
        id: claude
        env:
          ANTHROPIC_BASE_URL: ${{ secrets.ANTHROPIC_BASE_URL }}
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
          ISSUE_TITLE: ${{ github.event.issue.title }}
          ISSUE_BODY: ${{ github.event.issue.body }}
        run: |
          claude --headless --print \
            --max-turns 40 \
            "请阅读 .claude/CLAUDE.md 项目规范。
             修复 issue:
             标题:$ISSUE_TITLE
             正文:$ISSUE_BODY
             改完后只输出 PR description(简短 2-3 行),不要其他文字" > /tmp/claude.out
          echo "pr_desc<<EOF" >> $GITHUB_OUTPUT
          cat /tmp/claude.out >> $GITHUB_OUTPUT
          echo "EOF" >> $GITHUB_OUTPUT

      - name: Create PR
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PR_DESC: ${{ steps.claude.outputs.pr_desc }}
        run: |
          BRANCH="claude/fix-${{ github.event.issue.number }}"
          git checkout -b "$BRANCH"
          git add -A
          git diff --cached --quiet && exit 0
          git commit -m "claude: fix issue #${{ github.event.issue.number }}"
          git push -u origin "$BRANCH"
          gh pr create \
            --title "fix: #${{ github.event.issue.number }} ($ISSUE_TITLE)" \
            --body "$PR_DESC

closes #${{ github.event.issue.number }}"

4 个生产 workflow

Workflow 1:Issue 自动 fix

触发:issue 评论含 @claude fix 任务:Claude 读 issue 上下文 + 代码,改完发 PR

适合:bug fix、小功能、文档错误

Workflow 2:PR 自动 review

on:
  pull_request:
    types: [opened, synchronize]
jobs:
  review:
    if: contains(github.event.pull_request.labels.*.name, 'auto-review')
    steps:
      - uses: actions/checkout@v4
      - run: npm install -g @anthropic-ai/claude-code
      - name: Review
        env:
          ANTHROPIC_BASE_URL: ${{ secrets.ANTHROPIC_BASE_URL }}
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
        run: |
          DIFF=$(git diff origin/main...HEAD)
          claude --headless --print \
            --max-turns 10 \
            "review 下面 diff,按 .claude/REVIEW.md 标准。只输出 markdown comment 正文。
diff:
$DIFF" > /tmp/review.md
      - run: gh pr comment ${{ github.event.pull_request.number }} -F /tmp/review.md
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Workflow 3:每日 SEO 内容生成 cron

on:
  schedule:
    - cron: "0 1 * * *"  # UTC 01:00 每天
jobs:
  generate:
    steps:
      - uses: actions/checkout@v4
      - run: npm install -g @anthropic-ai/claude-code
      - name: Generate
        env:
          ANTHROPIC_BASE_URL: ${{ secrets.ANTHROPIC_BASE_URL }}
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
        run: |
          claude --headless --print \
            --max-turns 50 \
            "按 EDITORIAL.md,挑 3 个未写的长尾词写文章。
             路径:src/content/blog/<slug>.md
             不要修改其他文件"
      - name: Commit
        run: |
          git config user.name "claude-bot"
          git config user.email "[email protected]"
          git add src/content
          git diff --cached --quiet || git commit -m "claude: daily content"
          git push

Workflow 4:失败告警

on:
  workflow_run:
    workflows: ["Claude Auto Fix"]
    types: [completed]
jobs:
  notify:
    if: github.event.workflow_run.conclusion == 'failure'
    steps:
      - name: Slack
        run: |
          curl -X POST ${{ secrets.SLACK_WEBHOOK }} \
            -H "Content-Type: application/json" \
            -d '{"text":"Claude Auto Fix 失败,见 ${{ github.event.workflow_run.html_url }}"}'

Secret 配置清单

GitHub Actions Secrets(访问于 2026-05-19),仓库需要的 secrets:

Secret 名内容来源
ANTHROPIC_BASE_URLhttps://中转域名/v1中转方控制台
ANTHROPIC_API_KEYsk-ant-xxx 或中转自定义中转方控制台
GITHUB_TOKEN自动注入,无需配置GH 自动
SLACK_WEBHOOK(可选)Slack incoming webhookSlack admin

不要把这些直接写 workflow YAML。

成本与限流

Anthropic Pricing(访问于 2026-05-19),Claude Code 在 CI 单次任务典型 token 量:

任务input tokenoutput token成本(Sonnet 4.6)
简单 issue fix50-100K5-15K$0.30-0.60
中等 PR review100-200K10-30K$0.60-1.20
每日内容生成(3 篇)200-400K50-100K$1.50-3.00

按 2026 年 Solopreneur 仓库实测,月运行 200-500 次 workflow,API 月成本 $50-200,中转加价 10-20% → $55-240。

5 个常见踩坑

  1. —max-turns 不设导致 token 爆炸:Claude 偶尔会陷入「我再读一个文件确认」死循环,必须设 max-turns
  2. GH Secret 不能在 fork 的 PR 里读到:对外部贡献者的 PR,workflow 拿不到 secret,要用 pull_request_target + 严格审核
  3. runner 磁盘溢出:大仓库 + 多个 Claude 会话会用 5-10GB 临时文件,提前 df -h 监控
  4. CI 触发递归:Claude 自己 push 触发新 workflow 又触发 Claude,加 if: github.actor != 'claude-bot' 防递归
  5. 中转 RPM 上限:多 workflow 并发跑时,RPM 可能冲顶。挑一条主推 GPT-5.5 / Claude 4.7 的低价 API 中转,选支持高并发的套餐,Solopreneur 单仓库通常 60-300 RPM 够用

跨地区与团队协作

Claude Code Settings(访问于 2026-05-19),团队成员各自机器跑 Claude Code 与 CI 跑 Claude Code,推荐统一从中转方主账号下发子 key:

  • 主账号统一充值与限额
  • 每个成员 + CI 一套独立 key
  • 中转方后台按 key 分账,审计干净

实测 1 个月效果

8 个 Solopreneur 仓库,workflow 自动 fix + auto-review + 内容生成全部接入,2026 年 4 月数据:

  • 月 issue 自动关单率:42%(claude 直接 PR + 人工 review merge)
  • PR review 评论覆盖率:100%
  • 每日内容生成成功率:97%
  • 团队人工开发时间节省:30-40%

相关阅读