如何用 Cline CLI 在 GitHub Actions 中搭建 PR 自动代码审查工作流? 如何用 Cline CLI 在 GitHub Actions 中搭建 PR 自动代码审查工作流【免费下载链接】clineAutonomous coding agent as an SDK, IDE extension, or CLI assistant.项目地址: https://gitcode.com/GitHub_Trending/cl/cline在团队协作中每个 Pull Request 都要等人工审查排队。本文的目标是用 Cline CLI 在 GitHub Actions 里搭一条自动化流水线当 PR 打开或标记为 ready for review 时Cline 自动运行在 CI 环境中读取 PR 的 diff、详情和 CI 状态完成后在 PR 上发布一条以 Reviewed by Cline 开头的综合审查评论并可通过 GitHub API 提交行内代码建议。适用前提以项目文档 GitHub PR Review 为准一个已启用 Actions 的 GitHub 仓库一个 AI 供应商的 API Key如 Anthropic、OpenRouter并已添加为仓库 Secret例如ANTHROPIC_API_KEYGitHub Token 由 Actions 自动提供GITHUB_TOKEN无需手动配置。准备工作确认环境与认证方式Cline CLI 要求 Node.js 20推荐 22见 Installing Cline。工作流示例中通过actions/setup-nodev4固定使用node-version: 22并通过npm install -g cline在 CI 中全局安装 CLI。CI 中没有交互式终端所以认证不能走cline auth的交互流程而是用非交互参数一次性完成该命令形式可在 CLI Reference 的auth命令中查到cline auth --provider anthropic \ --apikey ${{ secrets.ANTHROPIC_API_KEY }} \ --modelid claude-opus-4-5-20251101其中--provider指定供应商可换成openai、openrouter等同时把对应的 Secret 配到仓库设置里--apikey传入 API Key--modelid指定本次会话使用的模型。上面这行来自官方示例示例用的是 Anthropic 与claude-opus-4-5-20251101如果你换供应商请相应替换这三项。创建工作流文件在仓库中创建.github/workflows/cline-pr-review.yml内容如下完整照搬官方示例${{ ... }}是 GitHub Actions 表达式会在运行时由 Actions 求值不需要手动替换name: Cline PR Code Review on: pull_request: types: [opened, ready_for_review] workflow_dispatch: inputs: pr_number: description: PR number to review required: true type: string concurrency: group: pr-review-${{ github.event.pull_request.number || inputs.pr_number }} cancel-in-progress: true jobs: cline-pr-review: if: | (github.event_name pull_request github.event.pull_request.draft false) || github.event_name workflow_dispatch runs-on: ubuntu-latest timeout-minutes: 60 permissions: contents: read pull-requests: write issues: read steps: - name: Checkout repository uses: actions/checkoutv4 with: fetch-depth: 0 - name: Setup Node.js uses: actions/setup-nodev4 with: node-version: 22 cache: npm - name: Install Cline CLI run: npm install -g cline - name: Configure Cline Authentication # Replace anthropic with your provider of choice (openai, openrouter, etc.) # and ensure the corresponding secret is set in your repo settings. run: | cline auth --provider anthropic \ --apikey ${{ secrets.ANTHROPIC_API_KEY }} \ --modelid claude-opus-4-5-20251101 - name: Get PR number id: pr run: | if [ ${{ github.event_name }} workflow_dispatch ]; then echo number${{ inputs.pr_number }} $GITHUB_OUTPUT else echo number${{ github.event.pull_request.number }} $GITHUB_OUTPUT fi - name: Review PR with Cline env: PR_NUMBER: ${{ steps.pr.outputs.number }} GITHUB_REPO: ${{ github.repository }} GH_TOKEN: ${{ github.token }} # Restrict Cline to only safe, read-only GitHub CLI commands CLINE_COMMAND_PERMISSIONS: | { allow: [ gh pr diff *, gh pr view *, gh pr checks *, gh pr list *, gh issue list *, gh issue view *, git log *, gh pr comment ${{ steps.pr.outputs.number }} *, gh api repos/${{ github.repository }}/pulls/${{ steps.pr.outputs.number }}/comments *, gh api repos/${{ github.repository }}/pulls/${{ steps.pr.outputs.number }}/reviews * ] } run: | cline --auto-approve true You are a GitHub PR reviewer for this repository. Your goal is to give the PR author helpful feedback and give maintainers the context they need to review efficiently. PR: #${PR_NUMBER} ## Gather context Use gh commands to fetch the PR diff, details, and checks. bash # Get full PR details gh pr view ${PR_NUMBER} --json number,title,body,author,createdAt,updatedAt,isDraft,labels,commits,files,additions,deletions,changedFiles,baseRefName,headRefName,mergeable,reviewDecision # Get the diff gh pr diff ${PR_NUMBER} # Check CI status gh pr checks ${PR_NUMBER} ## Deep code review Analyze the code changes. Look for: - Logic errors and edge cases - Security vulnerabilities - Performance issues - adherence to patterns in the codebase ## Submit Review Post a single comprehensive comment summarizing your review. If you have specific code suggestions, use the GitHub API to post inline comments: bash gh api repos/${GITHUB_REPO}/pulls/${PR_NUMBER}/reviews \ -X POST \ -f eventCOMMENT \ -f body \ -F comments[{path: src/file.ts, line: 10, body: Suggestion: ...}] Start your main comment with Reviewed by Cline.触发与运行逻辑均来自该示例pull_request事件在 PRopened或ready_for_review时触发if:条件排除了 draft PR。workflow_dispatch提供手动触发入口输入pr_number可审查任意历史 PR——这也是对旧 PR 补审查的方式。concurrency按 PR 编号分组并cancel-in-progress: true同一个 PR 上同时只会跑一个审查任务。timeout-minutes: 60限制单次审查最长运行 60 分钟。配置仓库 Secret进入仓库 Settings →Secrets and variables→Actions点击New repository secret名称填ANTHROPIC_API_KEY或与你工作流中secrets.*引用的名称一致值填你的 API Key 实际内容。Secret 名称必须与工作流里--apikey ${{ secrets.ANTHROPIC_API_KEY }}引用的名称一致认证步骤才能拿到密钥。工作流内部发生了什么整个 job 是一条固定链路checkout 代码 → 安装 Node.js 22 和 Cline CLI →cline auth非交互配置供应商与模型 → 从事件信息或手动输入中取出 PR 编号steps.pr.outputs.number→ 运行cline --auto-approve true ...执行审查提示词。审查提示词是任务的核心输入它告诉 Cline先用gh pr view、gh pr diff、gh pr checks收集 PR 详情、diff 和 CI 状态然后分析逻辑错误、边界情况、安全漏洞、性能问题和是否符合代码库既有模式最后发布一条综合评论有行级建议时通过gh api ... /pulls/编号/reviews提交 inline comments并明确要求主评论以 Reviewed by Cline 开头。提示词全文可按项目需要改写见文末下一步。两个关键参数需要理解清楚--auto-approve true让 Cline 以自主模式运行执行已批准的工具而不再等待交互确认。Cline 的 prompt 运行默认进入 Act 模式配合 auto-approve 后 CI 流程可以立即执行任务见 CLI Reference 与 CLI Overview。注意文档中的提示自主执行可能修改文件并运行命令因此下面的命令权限策略不可省略。CLINE_COMMAND_PERMISSIONS一个 JSON 策略环境变量限制 Cline 可以执行的 shell 命令。allow是 glob 模式列表设置后只有匹配的命令被允许deny始终优先见 CLI Reference。上面示例把允许范围收敛到只读的gh pr view/diff/checks/list、gh issue list/view、git log以及限定到当前 PR 编号的gh pr comment和gh api .../comments、.../reviews写评论命令——Cline 无法执行与审查无关的系统命令。权限边界与安全说明工作流只授予最小权限permissions: contents: read pull-requests: write issues: readpull-requests: write让 Cline 能发布评论和行内审查contents: read保证它能读代码做分析但不能直接 push 变更。这就是文档中给出的安全边界Cline 的产出只落在 PR 评论里不会改动分支代码。验证工作流是否生效按文档描述的行为判断标准是提交并 push 一个非 draft 的 PR或把已有 PR 标记为 ready for review也可以在 Actions 页面通过Run workflowworkflow_dispatch输入 PR 编号手动触发。查看 Actions 运行记录cline auth步骤应输出供应商配置成功认证命令成功后会打印 Provider configured 一类确认信息具体以你的供应商配置为准Review PR with Cline步骤正常跑完且不超时。回到 PR 页面应出现一条 Cline 发布的审查评论按提示词要求以 Reviewed by Cline 开头包含对 diff 的综合分析如有行级建议会以 inline comment 形式挂在对应文件和行上。如果 PR 一直处于 draft 状态工作流不会运行这是if:条件设计如此同一 PR 重复触发时concurrency会取消进行中的旧任务再跑新的。限制与可调整项模型与供应商是示例值--provider anthropic和--modelid claude-opus-4-5-20251101来自官方示例换供应商时同时修改 provider 参数和仓库 Secret 名称命令形式不变。审查行为由提示词决定传给 Cline 的系统提示词完全可自定义例如强制某种代码风格规范、把重点放在安全或性能上、调整反馈语气等文档 Customizing the Reviewer 一节列出的三个方向。命令权限可以放宽或收紧默认allowRedirects为false不允许 shell 重定向如审查任务确实需要写临时文件等可按 CLI Reference 中CLINE_COMMAND_PERMISSIONS的字段说明调整allow/deny/allowRedirectsdeny 规则永远优先。本文只覆盖 PR 自动审查这一场景。仓库内还有其他 GitHub Actions 示例如 supply-chain-alerts和 GitHub Integration 的集成说明属于独立场景不在本文范围内。【免费下载链接】clineAutonomous coding agent as an SDK, IDE extension, or CLI assistant.项目地址: https://gitcode.com/GitHub_Trending/cl/cline创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考