Skip to main content

自定义 Copilot 编码代理的开发环境

了解如何使用其他工具自定义 GitHub Copilot 的开发环境。

注意

Copilot 编码智能体 is in 公共预览版 and subject to change.

有关 Copilot 编码智能体 的详细信息,请参阅“关于将任务分配到 Copilot”。

关于自定义 Copilot 编码智能体 的开发环境

处理任务时,Copilot 可以访问其自己的临时开发环境(由 GitHub Actions 提供支持),可在其中浏览代码、进行更改、执行自动测试和 Linter 等。

可以自定义 Copilot 的环境,例如:

在 Copilot 的环境中预安装工具或依赖项

在其临时开发环境中,Copilot 可以生成或编译项目并运行自动化测试、Linter 和其他工具。 为此,其需要安装项目的依赖项。

Copilot 可以通过试错过程自行发现并安装这些依赖项,但鉴于大型语言模型 (LLM) 的非确定性,这一过程可能既缓慢又不可靠。在某些情况下,可能完全无法下载这些依赖项,例如当这些依赖项属于专用依赖项时。

可以改为在代理启动之前通过创建特殊的 GitHub Actions 工作流文件(位于存储库中的 .github/workflows/copilot-setup-steps.yml)来预先配置 Copilot 的环境。

copilot-setup-steps.yml 文件看起来就像普通的 GitHub Actions 工作流文件,但必须包含单个 copilot-setup-steps 作业。 该作业将在 Copilot 开始工作之前在 GitHub Actions 中执行。 有关 GitHub Actions 工作流文件的详细信息,请参阅“GitHub Actions 的工作流语法”。

下面是 TypeScript 项目的 copilot-setup-steps.yml 文件的简单示例,它可以克隆项目、安装 Node.js 并下载和缓存项目的依赖项。 应对此进行自定义,以适应自己的项目语言和依赖项:

YAML
name: "Copilot Setup Steps"

# Automatically run the setup steps when they are changed to allow for easy validation, and
# allow manual testing through the repository's "Actions" tab
on:
  workflow_dispatch:
  push:
    paths:
      - .github/workflows/copilot-setup-steps.yml
  pull_request:
    paths:
      - .github/workflows/copilot-setup-steps.yml

jobs:
  # The job MUST be called `copilot-setup-steps` or it will not be picked up by Copilot.
  copilot-setup-steps:
    runs-on: ubuntu-latest

    # Set the permissions to the lowest permissions possible needed for your steps.
    # Copilot will be given its own token for its operations.
    permissions:
      # If you want to clone the repository as part of your setup steps, for example to install dependencies, you'll need the `contents: read` permission. If you don't clone the repository in your setup steps, Copilot will do this for you automatically after the steps complete.
      contents: read

    # You can define any steps you want, and they will run before the agent starts.
    # If you do not check out your code, Copilot will do this for you.
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: "npm"

      - name: Install JavaScript dependencies
        run: npm ci

copilot-setup-steps.yml 文件中,只能自定义 copilot-setup-steps 作业的以下设置。 如果尝试自定义其他设置,所做的更改将被忽略。

  • steps(请参阅上文)
  • permissions(请参阅上文)
  • runs-on(请参阅下文)
  • container
  • services
  • snapshot
  • timeout-minutes(最大值:59

有关这些选项的详细信息,请参阅“GitHub Actions 的工作流语法”。

copilot-setup-steps.yml 文件在发生更改时会自动作为普通的 GitHub Actions 工作流运行,以便你查看是否运行成功。 在拉取请求中创建或修改该文件时,这项检查会与其他检查一起显示。

将 YAML 文件合并到默认分支后,你可以随时在仓库的“Actions”选项卡中手动运行该工作流,以确认一切运行正常****。 有关详细信息,请参阅“手动运行工作流程”。

升级为更大型的 GitHub 托管的 GitHub Actions 运行器

默认情况下,Copilot 在资源有限的标准 GitHub Actions 运行器中工作。

可以选择改用具有更多高级功能的大型运行器,例如更多的 RAM、CPU 和磁盘空间,以及高级网络控制。 如果发现性能不佳(例如在下载依赖项或运行测试时),则可能需要升级为大型运行器。 有关详细信息,请参阅“关于较大的运行器”。

必须先添加一个或多个大型运行器,并配置存储库以使用这些运行器,然后 Copilot 才可以使用更多大型运行器。 请参阅“管理较大的运行器”。 完成此操作后,可以使用 copilot-setup-steps.yml 文件告知 Copilot 使用大型运行器。

若要使用大型运行器,请将 copilot-setup-steps 作业的 runs-on 步骤设置为希望 Copilot 使用的大型运行器的标签和/或组。 有关使用 runs-on 指定大型运行器的详细信息,请参阅“在较大的运行器上运行作业”。

# ...

jobs:
  copilot-setup-steps:
    runs-on: ubuntu-4-core
    # ...

注意

  • Copilot 编码智能体 仅与 Ubuntu x64 Linux 运行器兼容。 不支持使用 Windows、macOS 或其他操作系统的运行器。
  • 不支持自托管的 GitHub Actions 运行器。

启用 Git Large File Storage (LFS)

如果使用 Git Large File Storage (LFS) 来存储仓库中的大型文件,则需要自定义 Copilot 的环境,以安装 Git LFS 并获取 LFS 对象。

要启用 Git LFS,请将 actions/checkout 步骤添加到 copilot-setup-steps 作业中,并将 lfs 选项设置为 true

YAML
# ...

jobs:
  copilot-setup-steps:
    runs-on: ubuntu-latest
    permissions:
      contents: read # for actions/checkout
    steps:
      - uses: actions/checkout@v4
        with:
          lfs: true

其他阅读材料