Open1

Claude Code Action で作成した PR が CI を pass しない時の対処

kobapikobapi

結論

以下の2点を実行すると CI を通すことができた ✔️

  • CI で実行するコマンドを実行できるように workflow を整備する
    • (例)pnpm test を実行したければ pnpm を動かせるように actions/setup-nodepnpm/action-setup を claude-code を動かす前に実行する
  • claude code に allowed_tools として実行したいコマンドの文字列を渡す
    • (例)pnpm test などを実行したければ以下のような形にしてあげる
    with:
        anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
        allowed_tools: "Bash(pnpm install),Bash(pnpm test),Bash(pnpm lint),Bash(pnpm type:check),Bash(pnpm add:*)"
    

前提

  • ターミナルからセットアップすると以下のような workflow が作成される
name: Claude Code

on:
  issue_comment:
    types: [created]
  pull_request_review_comment:
    types: [created]
  issues:
    types: [opened, assigned]
  pull_request_review:
    types: [submitted]

jobs:
  claude:
    if: |
      (github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) ||
      (github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
      (github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) ||
      (github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')))
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: read
      issues: read
      id-token: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 1

      - name: Run Claude Code
        id: claude
        uses: anthropics/claude-code-action@beta
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
  • これをそのまま実行すると linter, type:check を実行せず、CI で落ちる PR が生成される

修正版

  • allowed_toolspnpm test を渡すだけでは結局 pnpm を実行できずに linter を無視して job を完了にしてくるので、pnpm を実行できるようにする。
    • (linter を実行していない旨をちゃんと伝えてほしさある... 🙃)

修正版は以下のようになった

name: Claude Code

on:
  issue_comment:
    types: [created]
  pull_request_review_comment:
    types: [created]
  issues:
    types: [opened, assigned]
  pull_request_review:
    types: [submitted]

jobs:
  claude:
    if: |
      (github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) ||
      (github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
      (github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) ||
      (github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')))
    runs-on: ubuntu-latest
    timeout-minutes: 10
    permissions:
      contents: read
      pull-requests: read
      issues: read
      id-token: write
      packages: read
    steps:
    # @see: https://github.com/actions/checkout
      - name: Checkout repository
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      # @see: https://github.com/pnpm/action-setup
      - name: Set up pnpm
        uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0
        with:
          version: 10.11.0

      # @see: https://github.com/actions/setup-node
      - name: Set up Node.js
        uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
        with:
          node-version-file: .node-version
          cache: 'pnpm'
          cache-dependency-path: frontend/pnpm-lock.yaml

      - name: Install dependencies
        run: pnpm -C ./frontend install --frozen-lockfile
        env:
          NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Run Claude Code
        id: claude
        uses: anthropics/claude-code-action@beta
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          allowed_tools: "Bash(pnpm install),Bash(pnpm test),Bash(pnpm lint),Bash(pnpm type:check),Bash(pnpm add:*)"