您可以在 GitHub Actions 工作流中运行 GitHub Copilot CLI,以在 CI/CD 流程中自动化 AI 驱动的任务。例如,您可以概述最近的仓库活动、生成报告或搭建项目内容。GitHub Copilot CLI 像其他 CLI 工具一样在 Actions Runner 上运行,因此您可以在作业期间安装它,并在工作流步骤中调用它。
在 Actions 工作流中使用 Copilot CLI
您可以在 GitHub Actions 工作流中定义一个作业,该作业:在 Runner 上安装 Copilot CLI、对其进行身份验证、以编程模式运行它,然后处理结果。编程模式专为脚本和自动化设计,允许您以非交互方式传递提示。
工作流可以遵循以下模式
- 触发:在计划时间、响应仓库事件或手动启动工作流。
- 设置:检出代码,配置环境。
- 安装:在 Runner 上安装 GitHub Copilot CLI。
- 认证:确保 CLI 拥有访问仓库并进行更改所需的权限。
- 运行 Copilot CLI:使用描述您想要自动化任务的提示调用 Copilot CLI。
示例工作流
下面的工作流会生成今天在默认分支上所做更改的详细信息,并将这些详细信息作为工作流运行的摘要进行展示。
name: Daily summary
on:
workflow_dispatch:
# Run this workflow daily at 5:30pm UTC
schedule:
- cron: '30 17 * * *'
permissions:
contents: read
jobs:
daily-summary:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v5
with:
fetch-depth: 0
- name: Set up Node.js environment
uses: actions/setup-node@v4
- name: Install Copilot CLI
run: npm install -g @github/copilot
- name: Run Copilot CLI
env:
COPILOT_GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
run: |
copilot -p "Review the git log for this repository and write a bullet point summary of all code changes that were made today, with links to the relevant commit on GitHub. Above the bullet list give a description (max 100 words) summarizing the changes made. Write the details to summary.md" --allow-tool='shell(git:*)' --allow-tool=write --no-ask-user
cat summary.md >> "$GITHUB_STEP_SUMMARY"
name: Daily summary
on:
workflow_dispatch:
# Run this workflow daily at 5:30pm UTC
schedule:
- cron: '30 17 * * *'
permissions:
contents: read
jobs:
daily-summary:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v5
with:
fetch-depth: 0
- name: Set up Node.js environment
uses: actions/setup-node@v4
- name: Install Copilot CLI
run: npm install -g @github/copilot
- name: Run Copilot CLI
env:
COPILOT_GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
run: |
copilot -p "Review the git log for this repository and write a bullet point summary of all code changes that were made today, with links to the relevant commit on GitHub. Above the bullet list give a description (max 100 words) summarizing the changes made. Write the details to summary.md" --allow-tool='shell(git:*)' --allow-tool=write --no-ask-user
cat summary.md >> "$GITHUB_STEP_SUMMARY"
以下章节解释此工作流的每个部分。
触发
在本示例中,工作流每日按计划运行,也可以手动触发。
使用 workflow_dispatch 触发器,您可以在 GitHub 上仓库的 Actions 选项卡中手动运行工作流,这在测试提示或工作流配置的更改时非常有用。
使用 schedule 触发器,可通过 cron 语法在指定时间自动运行工作流。
on:
# Allows manual triggering of this workflow
workflow_dispatch:
# Run this workflow daily at 11:55pm UTC
schedule:
- cron: '55 23 * * *'
on:
# Allows manual triggering of this workflow
workflow_dispatch:
# Run this workflow daily at 11:55pm UTC
schedule:
- cron: '55 23 * * *'
设置
设置作业,以便 Copilot CLI 能访问您的仓库并在 Actions Runner 上运行。这使得 Copilot CLI 在生成每日摘要时能够分析仓库上下文。
permissions 块定义了授予内置 GITHUB_TOKEN 的范围。由于此工作流读取仓库数据并将摘要打印到日志中,需要 contents: read 权限。
permissions:
contents: read
jobs:
daily-summary:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v5
with:
fetch-depth: 0
permissions:
contents: read
jobs:
daily-summary:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v5
with:
fetch-depth: 0
安装
在 Runner 上安装 Copilot CLI,以便工作流可以将其作为命令调用。您可以使用任何受支持的安装方式来安装 GitHub Copilot CLI。有关完整的安装选项列表,请参阅 安装 GitHub Copilot CLI。
在本示例中,工作流使用 npm 将 GitHub Copilot CLI 全局安装。
- name: Set up Node.js environment uses: actions/setup-node@v4 - name: Install Copilot CLI run: npm install -g @github/copilot
- name: Set up Node.js environment
uses: actions/setup-node@v4
- name: Install Copilot CLI
run: npm install -g @github/copilot
认证
要让 Copilot CLI 在 Actions Runner 上运行,您需要使用拥有有效 Copilot 许可证的 GitHub 用户账户进行身份验证。
步骤 1:创建具有 “Copilot Requests” 权限的个人访问令牌(PAT)
- 转到您的个人设置页面以创建细粒度个人访问令牌:github.com/settings/personal-access-tokens/new。
- 创建一个带有 “Copilot Requests” 权限的新 PAT。
- 复制令牌的值。
步骤 2:将 PAT 存储为 Actions 仓库机密
- 在您的仓库中,依次进入 Settings > Secrets and variables > Actions,然后点击 New repository secret。
- 为机密取一个将在工作流中使用的名称。本示例使用
PERSONAL_ACCESS_TOKEN作为机密名称。 - 将令牌值粘贴到 “Secret” 字段并点击 Add secret。
工作流会使用仓库机密的值设置一个特殊的环境变量。Copilot CLI 支持多个用于身份验证的特殊环境变量。本示例使用的是 COPILOT_GITHUB_TOKEN,它是 Copilot CLI 专用的,可让您为 Copilot 设置不同于内置 GITHUB_TOKEN 环境变量的权限。
- name: Run Copilot CLI
env:
COPILOT_GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
- name: Run Copilot CLI
env:
COPILOT_GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
运行 Copilot CLI
使用 copilot -p PROMPT [OPTIONS] 以编程方式运行 CLI,并在命令完成后退出。
CLI 会将响应打印到标准输出,日志中会记录该输出。然而,为了更方便地访问更改详情,此示例将信息添加到工作流运行的摘要中。
run: |
copilot -p "Review the git log for this repository and write a bullet point summary of all code changes that were made today, with links to the relevant commit on GitHub. Above the bullet list give a description (max 100 words) summarizing the changes made. Write the details to summary.md" --allow-tool='shell(git:*)' --allow-tool=write --no-ask-user
cat summary.md >> "$GITHUB_STEP_SUMMARY"
run: |
copilot -p "Review the git log for this repository and write a bullet point summary of all code changes that were made today, with links to the relevant commit on GitHub. Above the bullet list give a description (max 100 words) summarizing the changes made. Write the details to summary.md" --allow-tool='shell(git:*)' --allow-tool=write --no-ask-user
cat summary.md >> "$GITHUB_STEP_SUMMARY"
此示例在 CLI 提示后使用了多个选项
--allow-tool='shell(git:*)'允许 Copilot 运行 Git 命令来分析仓库历史,这是生成最近更改摘要所必需的。--allow-tool='write'允许 Copilot 将生成的摘要写入 Runner 上的文件。--no-ask-user阻止 CLI 提示用户输入,这在没有用户响应的自动化工作流中尤为重要。
后续步骤
在确认工作流生成更改摘要后,您可以将相同模式应用于其他自动化任务。只需更改传递给 copilot -p PROMPT 的提示,然后决定如何处理输出。例如,您可以
- 创建一个 Pull Request,将当天的更改写入仓库的变更日志文件。
- 将摘要通过电子邮件发送给仓库维护者。