跳过主要内容

GitHub 集成示例

使用 AI 自动分析 GitHub issue。在任何 issue 评论中提及 @cline,即可触发自主调查,该调查会自动读取文件、分析代码并提供可操作的见解——所有这些都在 GitHub Actions 中自动运行。
Cline CLI 新手? 本示例假设您了解 Cline CLI 的基础知识并已完成安装指南。如果您是 Cline CLI 新手,我们建议先从GitHub RCA 示例开始,因为它更简单,有助于您在设置 GitHub Actions 之前了解基础知识。

工作流程

通过在任何 issue 评论中提及 @cline 来触发 Cline
Issue comment with @cline mention
Cline 的自动分析作为新评论出现,其中包含从您的实际代码库中提取的见解
Automated analysis response from Cline
整个调查在 GitHub Actions 中自主运行——从文件探索到发布结果。 让我们配置您的存储库。

先决条件

开始之前,您需要
  • Cline CLI 知识 - 完成了安装指南并了解基本用法
  • GitHub 存储库 - 具有配置 Actions 和 secrets 的管理员访问权限
  • GitHub Actions 熟悉度 - 对工作流程和 CI/CD 有基本了解
  • API 提供商帐户 - OpenRouter、Anthropic 或具有 API 密钥的类似提供商

设置

1. 复制工作流程文件

将此示例中的工作流程文件复制到您的存储库中。工作流程文件必须放置在存储库根目录的 .github/workflows/ 目录中,GitHub Actions 才能检测并运行它。在本例中,我们将其命名为 cline-responder.yml
# In your repository root
mkdir -p .github/workflows
curl -o .github/workflows/cline-responder.yml https://raw.githubusercontent.com/cline/cline/main/src/samples/cli/github-integration/cline-responder.yml
或者,您可以将完整的工作流程文件直接复制到 .github/workflows/cline-responder.yml
name: Cline Issue Assistant

on:
  issue_comment:
    types: [created, edited]

permissions:
  issues: write

jobs:
  respond:
    runs-on: ubuntu-latest
    environment: cline-actions
    steps:
      - name: Check for @cline mention
        id: detect
        uses: actions/github-script@v7
        with:
          script: |
            const body = context.payload.comment?.body || "";
            const isPR = !!context.payload.issue?.pull_request;
            const hit = body.toLowerCase().includes("@cline");
            core.setOutput("hit", (!isPR && hit) ? "true" : "false");
            core.setOutput("issue_number", String(context.payload.issue?.number || ""));
            core.setOutput("issue_url", context.payload.issue?.html_url || "");
            core.setOutput("comment_body", body);

      - name: Checkout repository
        if: steps.detect.outputs.hit == 'true'
        uses: actions/checkout@v4

      # Node v20 is needed for Cline CLI on GitHub Actions Linux
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Setup Cline CLI
        if: steps.detect.outputs.hit == 'true'
        run: |
          # Install the Cline CLI
          sudo npm install -g cline

      - name: Create Cline Instance
        if: steps.detect.outputs.hit == 'true'
        env:
          OPENROUTER_API_KEY: ${{ secrets.OPENROUTER_API_KEY }}
          CLINE_DIR: ${{ runner.temp }}/cline
        run: |
          # Create instance and capture output
          INSTANCE_OUTPUT=$(cline instance new 2>&1)
          
          # Parse address from output (format: "  Address: 127.0.0.1:36733")
          CLINE_ADDRESS=$(echo "$INSTANCE_OUTPUT" | grep "Address:" | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]+')
          echo "CLINE_ADDRESS=$CLINE_ADDRESS" >> $GITHUB_ENV
          
          # Configure API key
          cline config set open-router-api-key=$OPENROUTER_API_KEY --address $CLINE_ADDRESS -v

      - name: Download analyze script
        if: steps.detect.outputs.hit == 'true'
        run: |
          export GITORG="YOUR-GITHUB-ORG"
          export GITREPO="YOUR-GITHUB-REPO"

          curl -L https://raw.githubusercontent.com/${GITORG}/${GITREPO}/refs/heads/main/git-scripts/analyze-issue.sh -o analyze-issue.sh
          chmod +x analyze-issue.sh

      - name: Run analysis
        if: steps.detect.outputs.hit == 'true'
        id: analyze
        env:
          ISSUE_URL: ${{ steps.detect.outputs.issue_url }}
          COMMENT: ${{ steps.detect.outputs.comment_body }}
          CLINE_ADDRESS: ${{ env.CLINE_ADDRESS }}
        run: |
          set -euo pipefail
          
          RESULT=$(./analyze-issue.sh "${ISSUE_URL}" "Analyze this issue. The user asked: ${COMMENT}" "$CLINE_ADDRESS")
          
          {
            echo 'result<<EOF'
            printf "%s\n" "$RESULT"
            echo 'EOF'
          } >> "$GITHUB_OUTPUT"

      - name: Post response
        if: steps.detect.outputs.hit == 'true'
        uses: actions/github-script@v7
        env:
          ISSUE_NUMBER: ${{ steps.detect.outputs.issue_number }}
          RESULT: ${{ steps.analyze.outputs.result }}
        with:
          script: |
            await github.rest.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: Number(process.env.ISSUE_NUMBER),
              body: process.env.RESULT || "(no output)"
            });
您在提交之前必须编辑工作流程文件!打开 .github/workflows/cline-responder.yml 并更新工作流程中的“Download analyze script”步骤,以指定存储分析脚本的 GitHub 组织和存储库:
export GITORG="YOUR-GITHUB-ORG"      # Change this!
export GITREPO="YOUR-GITHUB-REPO"    # Change this!
示例: 如果您的存储库是 github.com/acme/myproject,请设置
export GITORG="acme"
export GITREPO="myproject"
这会告诉工作流程在您在步骤 3 中提交分析脚本后从您的存储库中下载它。
工作流程将查找新的或更新的 issue,检查 @cline 提及,然后启动 Cline CLI 实例以深入研究 issue,并作为对 issue 的回复提供反馈。

2. 配置 API 密钥

将您的 AI 提供商 API 密钥添加为存储库机密
  1. 转到您的 GitHub 存储库
  2. 导航到 SettingsEnvironment 并添加一个新环境。
    Navigate to Actions secrets
    确保将其命名为“cline-actions”,使其与 cline-responder.yml 文件顶部的 environment 值匹配。
  3. 点击 New repository secret
  4. OPENROUTER_API_KEY 添加一个机密,其值为来自 openrouter.com 的 API 密钥。
    Add API key secret
  5. 验证您的机密已配置
    API key configured
现在您已准备好在 GitHub Action 中为 Cline 提供所需的凭据。

3. 添加分析脚本

github-issue-rca 示例中的分析脚本添加到您的存储库中。首先,您需要在存储库根目录中创建一个 git-scripts 目录,脚本将位于此处。 选择以下选项之一: 选项 A:直接下载(推荐)
# In your repository root, create the directory and download the script
mkdir -p git-scripts
curl -o git-scripts/analyze-issue.sh https://raw.githubusercontent.com/cline/cline/main/src/samples/cli/github-issue-rca/analyze-issue.sh
chmod +x git-scripts/analyze-issue.sh
选项 B:手动复制粘贴 手动创建目录和文件,然后粘贴脚本内容:
# In your repository root
mkdir -p git-scripts
# Create and edit the file with your preferred editor
nano git-scripts/analyze-issue.sh  # or use vim, code, etc.
#!/bin/bash
# Analyze a GitHub issue using Cline CLI

if [ -z "$1" ]; then
    echo "Usage: $0 <github-issue-url> [prompt] [address]"
    echo "Example: $0 https://github.com/owner/repo/issues/123"
    echo "Example: $0 https://github.com/owner/repo/issues/123 'What is the root cause of this issue?'"
    echo "Example: $0 https://github.com/owner/repo/issues/123 'What is the root cause of this issue?' 127.0.0.1:46529"
    exit 1
fi

# Gather the args
ISSUE_URL="$1"
PROMPT="${2:-What is the root cause of this issue?}"
if [ -n "$3" ]; then
    ADDRESS="--address $3"
fi

# Ask Cline for its analysis, showing only the summary
cline -y "$PROMPT: $ISSUE_URL" --mode act $ADDRESS -F json | \
    sed -n '/^{/,$p' | \
    jq -r 'select(.say == "completion_result") | .text' | \
    sed 's/\\n/\n/g'
粘贴脚本内容后,使其可执行
chmod +x git-scripts/analyze-issue.sh
此分析脚本调用 Cline 来执行对 GitHub issue 的提示,总结输出以填充对 issue 的回复。

4. 提交并推送

git add .github/workflows/cline-responder.yml
git add git-scripts/analyze-issue.sh
git commit -m "Add Cline issue assistant workflow"
git push

用法

设置完成后,只需在任何 issue 评论中提及 @cline
@cline what's causing this error?

@cline analyze the root cause

@cline what are the security implications?
GitHub Actions 将
  1. 检测到 @cline 提及
  2. 启动 Cline CLI 实例
  3. 下载分析脚本
  4. 使用 act 模式和 yolo(完全自主)分析 issue
  5. 将 Cline 的分析作为新评论发布
注意:工作流程仅在 issue 评论上触发,不在拉取请求评论上触发。

工作原理

工作流程 (cline-responder.yml)
  1. 在 issue 评论(创建或编辑)上触发
  2. 检测 @cline 提及(不区分大小写)
  3. 使用 npm 全局安装 Cline CLI
  4. 使用 cline instance new 创建 Cline 实例
  5. 使用 cline config set open-router-api-key=... --address ... 配置身份验证
  6. github-issue-rca 示例下载可重用的 analyze-issue.sh 脚本
  7. 使用实例地址运行分析
  8. 将分析结果作为评论发布