在 GitHub Actions 和其他 CI/CD 系统中使用 Cursor CLI 来自动化开发任务。

GitHub Actions 集成

基础配置:
- name: 安装 Cursor CLI
  run: |
    curl https://cursor.com/install -fsS | bash
    echo "$HOME/.cursor/bin" >> $GITHUB_PATH

- name: 运行 Cursor Agent
  env:
    CURSOR_API_KEY: ${{ secrets.CURSOR_API_KEY }}
  run: |
    cursor-agent -p "Your prompt here" --model gpt-5

实用示例

查看我们的示例以了解实际工作流:自动更新文档修复 CI 问题

其他 CI 系统

在任何 CI/CD 系统中使用 Cursor CLI,需要:
  • Shell 脚本执行(bash、zsh 等)
  • 环境变量用于 API 密钥配置
  • 互联网连接以访问 Cursor 的 API

自主等级

选择你的代理自主等级:

完全自主方案

授予代理对 git 操作、API 调用和外部交互的完全控制。配置更简单,但需要更高信任。 示例: 在我们的 Auto Update Documentation cookbook 中,第一个工作流让代理:
  • 分析 PR 变更
  • 创建并管理 git 分支
  • 提交并推送变更
  • 在拉取请求中发布评论
  • 处理所有错误场景
- name: Update docs (full autonomy)
  run: |
    cursor-agent -p "You have full access to git, GitHub CLI, and PR operations. 
    Handle the entire docs update workflow including commits, pushes, and PR comments."

受限自主方案

我们建议在生产 CI 工作流中配合使用基于权限的限制。这样能两全其美:代理可以智能处理复杂分析和文件修改,而关键操作保持确定且可审计。
限制代理的操作,同时将关键步骤放在独立的工作流步骤中处理。可控性和可预测性更好。 示例: 同一 cookbook 中的第二个工作流将代理限制为仅进行文件修改:
- name: Generate docs updates (restricted)
  run: |
    cursor-agent -p "IMPORTANT: Do NOT create branches, commit, push, or post PR comments. 
    Only modify files in the working directory. A later workflow step handles publishing."

- name: Publish docs branch (deterministic)
  run: |
    # 由 CI 处理的确定性 git 操作
    git checkout -B "docs/${{ github.head_ref }}"
    git add -A
    git commit -m "docs: update for PR"
    git push origin "docs/${{ github.head_ref }}"

- name: Post PR comment (deterministic)  
  run: |
    # 由 CI 处理的确定性 PR 评论
    gh pr comment ${{ github.event.pull_request.number }} --body "Docs updated"

基于权限的限制

使用 权限配置 在 CLI 层面强制实施限制:
{
  "permissions": {
    "allow": [
      "Read(**/*.md)",
      "Write(docs/**/*)",
      "Shell(grep)",
      "Shell(find)"
    ],
    "deny": [
      "Shell(git)",
      "Shell(gh)", 
      "Write(.env*)",
      "Write(package.json)"
    ]
  }
}

身份验证

设置你的 CURSOR_API_KEY 环境变量:
env:
  CURSOR_API_KEY: ${{ secrets.CURSOR_API_KEY }}
你可以在 Cursor 设置 中获取你的 API Key。