在 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 呼叫與外部互動。設定更簡單,但需要更高的信任度。 範例: 在我們的 Auto 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"

基於權限的限制

使用 permission configurations 在 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 金鑰。