你可在 GitHub Actions 工作流中运行 GitHub Copilot CLI,将 AI 驱动任务自动化作为 CI/CD 流程的一部分。 例如,可以汇总最近的存储库活动、生成报表或基架项目内容。 GitHub Copilot CLI 像任何其他 CLI 工具一样在操作运行程序上运行,因此可以在作业期间安装它,并从工作流步骤调用它。
在操作工作流中使用 Copilot CLI
你可在 GitHub Actions 工作流中定义作业:在运行器上安装 Copilot CLI、进行身份验证、以编程模式运行,然后处理结果。 编程模式专为脚本和自动化而设计,可让你以非交互方式传递提示。
工作流可以遵循以下模式: 1. 触发器:按计划启动工作流,以响应存储库事件,或手动启动工作流。 1. 设置:签出代码,设置环境。 1. 安装:在运行器上安装 GitHub Copilot CLI。 1. 身份验证:确保 CLI 具有访问存储库和进行更改所需的权限。 1. 运行 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"
以下部分介绍此工作流的每个部分。
Trigger
在此示例中,工作流按每日计划运行,也可以手动触发。
触发器workflow_dispatch允许你从存储库**** 的GitHub”选项卡手动运行工作流,这在测试对提示或工作流配置的更改时很有用。
触发器 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 * * *'
Setup
设置作业,以便 Copilot CLI 可以访问存储库并在 Actions 运行程序上运行。 这允许 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
Install
在运行程序上安装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
Authenticate
若要允许Copilot CLI在 Actions 运行程序上运行,需要使用有效的GitHub许可证对用户帐户进行身份验证Copilot。
**步骤 1:创建包含“Copilot 请求”权限的 personal access token (PAT):**
-
转到创建 fine-grained personal access token 的个人设置:github.com/settings/personal-access-tokens/new。
-
使用“Copilot 请求”权限创建新的 PAT。
-
复制令牌值。
**步骤 2:将 PAT 存储为 Actions 存储库机密:** -
在存储库中,转到 “设置 > 机密和变量 > 操作 ”,然后单击“ 新建存储库机密”。
-
为机密指定一个你将在工作流中使用的名称。 在此示例中,我们使用
PERSONAL_ACCESS_TOKEN作为机密的名称。 -
将令牌值粘贴到“机密”字段中,然后单击“ 添加机密”。
工作流设置一个具有存储库机密值的特殊环境变量。
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 将生成的摘要写入运行程序上的文件。 -
`--no-ask-user` 阻止 CLI 提示用户输入,这在自动化工作流中运行时非常重要,因为自动化工作流中没有用户响应其他输入请求。
后续步骤
确认工作流生成更改摘要后,可以将相同的模式适应其他自动化任务。 首先更改您传递给 copilot -p PROMPT 的提示,然后决定如何处理输出内容。 例如,您可以:
- 创建拉取请求,将当天的更改更新到存储库中的变更日志文件。
- 向存储库维护人员发送电子邮件摘要。
延伸阅读
-
[AUTOTITLE](/copilot/reference/copilot-cli-reference/cli-command-reference) -
[AUTOTITLE](/actions) -
[AUTOTITLE](/copilot/how-tos/copilot-cli/automate-copilot-cli/run-cli-programmatically)