跳至主要内容

以编程方式运行 GitHub Copilot CLI

在终端、脚本或 Actions 工作流中使用 Copilot CLI。

简介

您可以在单条命令中直接向 Copilot CLI 传递提示,而无需进入交互式会话。这样既可以在终端直接使用 Copilot,也可以在脚本、CI/CD 流水线以及自动化工作流中以编程方式使用 CLI。

要以编程方式使用 Copilot CLI,您可以执行以下任一操作。

  • 使用 copilot 命令并加上 -p--prompt 参数,然后写入您的提示

    Shell
    copilot -p "Explain this file: ./complex.ts"
    
  • 将提示通过管道传递给 copilot 命令

    Shell
    echo "Explain this file: ./complex.ts" | copilot
    

    注意

    如果同时使用了 -p--prompt 参数,则管道输入会被忽略。

以编程方式使用 Copilot CLI 的技巧

  • 提供精准的提示 — 清晰、无歧义的指令比模糊的请求能产生更好的结果。提供的上下文越多(文件名、函数名、具体的改动),Copilot 需要猜测的内容就越少。
  • 谨慎引用提示 — 若想避免 Shell 对特殊字符的解释,请使用单引号将提示括起来。
  • 始终授予最小权限 — 使用 --allow-tool=[TOOLS...]--allow-url=[URLs...] 参数,仅允许 Copilot 使用完成任务所必需的工具和访问权限。除非在沙箱环境中,否则避免使用过于宽松的选项(如 --allow-all)。
  • 捕获输出时使用 -s(静默),这会抑制会话元数据,让您得到纯净的文本。
  • 使用 --no-ask-user 可阻止代理提出澄清性问题。
  • 显式指定模型,使用 --model 以确保在不同环境下行为一致。

请参阅 GitHub Copilot CLI 编程参考,了解在以编程方式运行 Copilot CLI 时特别有用的选项。

CI/CD 集成

以编程方式运行 Copilot CLI 的常见用例是将 CLI 命令写入 CI/CD 工作流的某一步。

下面这段来自 GitHub Actions 工作流的代码展示了如何简单地运行 Copilot CLI 命令。

# Workflow step using Copilot CLI
- name: Generate test coverage report
  env:
    COPILOT_GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
  run: |
    copilot -p "Run the test suite and produce a coverage summary" \
      -s --allow-tool='shell(npm:*), write' --no-ask-user

更多信息请参阅 使用 Copilot CLI 与 GitHub Actions 自动化任务

编程使用示例

生成提交信息

Bash
copilot -p 'Write a commit message in plain text for the staged changes' -s \
  --allow-tool='shell(git:*)'

概括文件内容

Bash
copilot -p 'Summarize what src/auth/login.ts does in no more than 100 words' -s

为模块编写测试

Bash
copilot -p 'Write unit tests for src/utils/validators.ts' \
  --allow-tool='write, shell(npm:*), shell(npx:*)'

修复 lint 错误

Bash
copilot -p 'Fix all ESLint errors in this project' \
  --allow-tool='write, shell(npm:*), shell(npx:*), shell(git:*)'

解释差异(diff)

Bash
copilot -p 'Explain the changes in the latest commit on this branch and flag any potential issues' -s

对分支进行代码审查

使用 /review 斜杠命令,调用内置的 code-review 代理对当前分支的代码更改进行审查。

Bash
copilot -p '/review the changes on this branch compared to main. Focus on bugs and security issues.' \
  -s --allow-tool='shell(git:*)'

生成文档

Bash
copilot -p 'Generate JSDoc comments for all exported functions in src/api/' \
  --allow-tool=write

导出会话

将完整的会话记录保存为本地文件系统中的 Markdown 文件。

Bash
copilot -p "Audit this project's dependencies for vulnerabilities" \
  --allow-tool='shell(npm:*), shell(npx:*)' \
  --share='./audit-report.md'

将会话记录保存为 GitHub.com 上的 Gist,便于共享。

Bash
copilot -p 'Summarize the architecture of this project' --share-gist

注意

企业托管用户或使用数据驻留(*.ghe.com)的 GitHub Enterprise Cloud 均无法使用 Gist。

Shell 脚本模式

将 Copilot 输出捕获到变量中

Bash
result=$(copilot -p 'What version of Node.js does this project require? \
  Give the number only. No other text.' -s)
echo "Required Node version: $result"

在条件语句中使用

Bash
if copilot -p 'Does this project have any TypeScript errors? Reply only YES or NO.' -s \
  | grep -qi "no"; then
  echo "No type errors found."
else
  echo "Type errors detected."
fi

处理多个文件

Bash
for file in src/api/*.ts; do
  echo "--- Reviewing $file ---" | tee -a review-results.md
  copilot -p "Review $file for error handling issues" -s --allow-all-tools | tee -a review-results.md
done

延伸阅读

© . This site is unofficial and not affiliated with GitHub, Inc.