跳至主要内容

故障排除预构建

您可以使用预构建来加快创建 Codespaces 的速度。本文提供处理预构建常见问题的排查步骤。

欲了解有关 GitHub Codespaces 预构建的更多信息,请参阅 Prebuilding your codespaces

检查是否从预构建创建了 Codespace?

创建 Codespace 时,您可以选择要使用的虚拟机类型。如果该虚拟机类型有可用的预构建,""预构建就绪" 将显示在其旁边。

Screenshot of a list of available machine types: 2, 4, 8, 16, and 32 core, all labeled "Prebuild ready."

如果您将 GitHub Codespaces 编辑器首选项设置为 “Visual Studio Code for Web”,则在 “设置您的 Codespace” 页面上会显示消息 “已找到预构建的 Codespace”,前提是使用了预构建。

Screenshot of the "Setting up your codespace" page, with the text: "Prebuilt codespace found. Downloading image."

同样地,如果您的编辑器首选项是 “VS Code”,则在集成终端中会出现消息 “您正使用由仓库的预构建配置定义的预构建 Codespace”,当您创建新 Codespace 时。更多信息,请参阅 Setting your default editor for GitHub Codespaces

创建 Codespace 后,您可以在终端中运行以下 GitHub CLI 命令来检查它是否由预构建创建

Shell
gh api /user/codespaces/$CODESPACE_NAME --jq .prebuild

如果 Codespace 是使用预构建创建的,则此命令返回 true

如果未安装 GitHub CLI(gh),也可以使用以下命令;如果 Codespace 是由预构建创建的,则返回 createFromPrebuild

Shell
cat /workspaces/.codespaces/shared/environment-variables.json | jq '.ACTION_NAME'

检查预构建使用情况

您可以在仓库设置的 “Codespaces” 页面中查看该仓库是否正在使用预构建。

通过查看个人或组织账户的计费数据,您可以检查当前计费周期内预构建已消耗多少存储空间。您也可以生成使用报告,以了解哪些仓库正在使用预构建。更多信息,请参阅 Viewing your usage of metered products and licenses

“预构建就绪”标签有时会缺失

您可能会注意到,有时在从启用了预构建的分支创建新 Codespace 时,选择机器类型的对话框中不会显示 “预构建就绪” 标签。这意味着当前没有可用的预构建。

默认情况下,每次向启用了预构建的分支推送代码时,预构建都会更新。如果推送涉及对 dev container 配置的更改,那么在更新进行期间,"预构建就绪” 标签会从机器类型列表中移除。在此期间,您仍然可以创建没有预构建的 Codespace。如有必要,您可以通过将预构建设置为仅在更改 dev container 配置文件时更新,或仅在自定义计划下更新,以减少预构建不可用的次数。更多信息,请参阅 Configuring prebuilds

如果您的分支没有专门启用预构建,但它是从已启用预构建的分支分叉出来的,仍可能受益于预构建。不过,一旦您在该分支上更改了 dev container 配置,使其与基线分支的配置不一致,预构建将不再可用。

如果特定分支未显示 “预构建就绪” 标签,请检查以下事项

  • 确认该分支存在预构建配置。如果您不是仓库管理员,需要联系管理员确认。
  • 确认预构建配置包含您所在的区域。
  • 检查最近是否向启用了预构建的分支推送了 dev container 配置的更改。如果是,通常需要等待此推送对应的预构建工作流运行完成后,预构建才会再次可用。
  • If no configuration changes were recently made, go to the Actions tab of your repository, click Codespaces Prebuilds in the workflows list, and check that prebuild workflow runs for the branch are succeeding. If latest runs of a workflow failed, and one or more of these failed runs contained changes to the dev container configuration, then there will be no available prebuilds for the associated branch.

使用预构建创建的 Codespaces 中某些资源无法访问

如果预构建配置的 devcontainer.json 文件声明需要访问其他仓库的权限,则在创建或更新预构建配置时会提示仓库管理员授权这些权限。如果管理员未授予全部请求的权限,则预构建及基于该预构建创建的 Codespaces 可能会出现问题。即使随后创建 Codespace 的用户在提示时授予了所有权限,也可能会受到影响。

排查预构建工作流运行失败的问题

提升 GitHub Actions 费用上限

预构建是通过 GitHub Actions 创建和更新的。如果您已用完所有 GitHub Actions 分钟并达到了费用上限,预构建工作流将会失败。此时可以提升 GitHub Actions 的费用上限,以允许工作流继续运行。更多信息,请参阅 Setting up budgets to control spending on metered products

授权访问权限

如果预构建配置的 devcontainer.json 文件已更新为需要访问其他仓库的权限,而仓库管理员尚未收到授权提示,则预构建工作流可能会失败。尝试更新预构建配置,但不做任何更改。如果点击 Update 时出现授权页面,请检查请求的权限是否合适,如合适请授权。更多信息,请参阅 Managing prebuildsManaging access to other repositories within your codespace

如果预构建配置的工作流运行失败,您可以在调查期间暂时禁用该预构建配置。更多信息,请参阅 Managing prebuilds

防止使用过时的预构建

默认情况下,如果最新的预构建工作流失败,则会使用同一仓库、分支和 devcontainer.json 配置文件组合的上一次预构建来创建新 Codespace。此行为称为预构建优化。

我们建议保持预构建优化开启,因为它可确保在最新预构建不可用时仍能快速创建 Codespace。不过,作为仓库管理员,如果发现预构建的 Codespace 落后于分支的当前状态,您可以关闭预构建优化。如果关闭预构建优化,当最新的预构建工作流失败或正在运行时,相关仓库、分支和 devcontainer.json 文件组合的 Codespace 将在没有预构建的情况下创建。

  1. 在 GitHub 上,导航至仓库的主页面。

  2. 在仓库名称下方,点击 设置。如果看不到 “Settings” 选项卡,请选择 下拉菜单,然后点击 Settings

    Screenshot of a repository header showing the tabs. The "Settings" tab is highlighted by a dark orange outline.

  3. 在侧栏的 “Code & automation” 部分,点击 Codespaces

  4. 在受影响的预构建配置右侧,点击省略号 (...),然后点击 Edit

    Screenshot of the prebuild options dropdown with "Edit" highlighted.

  5. 滚动至 “Edit configuration” 页面底部,点击 Show advanced options

    Screenshot of the bottom of the prebuilds configuration page. The link "Show advanced options" is highlighted with a dark orange outline.

  6. 如果确定要禁用默认设置,请选择 Disable prebuild optimization

    Screenshot of the "Advanced options" settings. The checkbox labeled "Disable prebuild optimization" is selected. Under this is the "Update" button.

  7. 保存更改,请点击 Update

延伸阅读

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