在 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

Cookbook 示例

查看我们的 Cookbook 示例,了解实用的工作流:更新文档修复 CI 问题

其他 CI 系统

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

自主级别

选择你的 agent 的自主级别:

完全自主方案

赋予 agent 对 git 操作、API 调用和外部交互的完全控制。设置更简单,但需要更高的信任度。 示例: 在我们的 Update Documentation 菜谱中,第一个工作流让 agent 可以:
  • 分析 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 工作流中使用带有基于权限的限制的这种方案。这样能兼顾两者优势:agent 可智能处理复杂分析和文件修改,而关键操作保持确定性且可审计。
限制 agent 的操作,并将关键步骤放到单独的工作流步骤中处理。更易控制且更可预测。 示例: 同一菜谱中的第二个工作流将 agent 限制为仅进行文件修改:
- 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: |
    # Deterministic git operations handled by CI
    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: |
    # Deterministic PR commenting handled by CI
    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)"
    ]
  }
}

身份验证

生成你的 API 密钥

首先,在 Cursor 控制台生成一个 API 密钥

配置仓库机密

将你的 Cursor API 密钥安全地存储在仓库中:
  1. 前往你的 GitHub 仓库
  2. 点击 SettingsSecrets and variablesActions
  3. 点击 New repository secret
  4. 命名为 CURSOR_API_KEY
  5. 将你的 API 密钥粘贴到值中
  6. 点击 Add secret

在工作流中使用

设置你的 CURSOR_API_KEY 环境变量:
env:
  CURSOR_API_KEY: ${{ secrets.CURSOR_API_KEY }}