在 GitHub Actions 與其他 CI/CD 系統中使用 Cursor CLI,自動化開發作業。

與 GitHub Actions 整合

基本設定:
- name: Install Cursor CLI
  run: |
    curl https://cursor.com/install -fsS | bash
    echo "$HOME/.cursor/bin" >> $GITHUB_PATH

- name: Run 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 呼叫與外部互動。設定較簡單,但需要更高的信任。 範例: 在我們的 Update Documentation 實作範例中,第一個工作流程讓代理可以:
  • 分析 PR 變更
  • 建立並管理 git 分支
  • 提交並推送變更
  • 在 pull request 上發佈留言
  • 處理所有錯誤情境
- 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 工作流程中,搭配「基於權限的限制」使用此方案。這能兼顧兩者優點:代理能智慧處理複雜分析與檔案修改,而關鍵操作仍保持可決定性與可稽核。
限制代理的操作,並將關鍵步驟放在獨立的工作流程步驟中。控管更好、可預測性更高。 範例: 同一個實作範例中的第二個工作流程,將代理限制為只能進行檔案修改:
- 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)"
    ]
  }
}

驗證

產生你的 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 }}