注:Codespaces 目前是有限公测版,可能会有变动。 在公测期间,GitHub 不对 Codespaces 的可用性做任何保证。 有关加入公测的更多信息,请参阅“关于 Codespaces”。
关于默认代码空间配置
您可以为仓库创建默认代码空间配置,以确定任何人员为该仓库创建的每个新代码空间的环境。 配置定义一个可以包含框架、工具、扩展和端口转发的开发容器。
如果您没有在仓库中定义配置,GitHub 将创建一个具有基本 Linux 映像的代码空间。 基本 Linux 映像包括适用于 Node.js、JavaScript、TypeScript、Python、C++、Java、C#、.NET Core、PHP 和 PowerShell 的工具。 有关基本 Linux 映像的更多信息,请参阅 microsoft/vscode-dev-containers 仓库。
也可为您的帐户创建的任何代码空间个性化代码空间环境的各个方面。 个性化可以包括 shell 首选项和其他工具。 dotfiles 个性化的应用先于仓库的默认码空间配置。 更多信息请参阅“个性化您帐户的 Codespaces”。
您可以使用项目类型的预建容器配置创建默认代码空间配置,也可以根据项目需要创建自定义配置。
Codespaces uses settings contained in a configuration file named devcontainer.json. This file can be located in the root of the repository or in a folder called .devcontainer. If the file is located in the root of the repository, the filename must begin with a period: .devcontainer.json.
You can use your devcontainer.json to set default settings for the entire codespace environment, including the Visual Studio Code editor, but you can also set editor-specific settings in a file named .vscode/settings.json.
对仓库代码空间配置的更改只会应用到每个新的代码空间,而不影响任何现有的代码空间。
使用预建容器配置
您可以对 vscode-dev-containers 仓库中的 Visual Studio Code 使用任何预建容器配置。 预建容器定义包括特定项目类型的共同配置,可帮助您利用现有的配置快速开始使用,配置中已经有适当的容器选项、Visual Studio Code 设置和应该安装的 Visual Studio Code 扩展。
- 克隆或下载
vscode-dev-containers仓库。 - 在
vscode-dev-containers仓库中,导航到containers文件夹,然后选择符合项目需求的容器配置。 我们将使用 Node.js & JavaScript 容器配置作为示例。 - 将
.devcontainer文件夹从Node.js & JavaScript文件夹复制到项目仓库的根目录。 - 提交并推送新配置到 GitHub 上的项目仓库。
从包含 .devcontainer 文件夹的分支创建的每个新代码空间将根据文件夹内容进行配置。 更多信息请参阅“创建代码空间”。
创建自定义代码空间配置
If none of the pre-built configurations meet your needs, you can create a custom configuration by adding a devcontainer.json file. This file can be located in the root of the repository or in a folder called .devcontainer. If the file is located in the root of the repository, the filename must begin with a period: .devcontainer.json.
在该文件中,您可以使用支持的配置键来指定代码库环境的各个方面,例如要安装哪些 Visual Studio Code 扩展。
配置 Visual Studio Code 的编辑器设置时,有三个可用的作用域:工作区、远程 [Codespaces]和用户。 如果在多个作用域中定义了设置,工作区设置优先级最高,远程 [Codespaces] 次之,最后是用户。
您可以在两个地方定义 Visual Studio Code 的默认编辑器设置。
.vscode/settings.json中定义的编辑器设置在代码空间中用作 Workspace 范围的设置。devcontainer.json的settings键中定义的编辑器设置在代码空间中用作 Remote [Codespaces] 范围的设置。
支持的代码空间配置键
您可以在 devcontainer.json 中使用 Codespaces 支持的配置键。
常规设置
namesettingsextensionsforwardPortsdevPortpostCreateCommand
Docker、Dockerfile 或映像设置
imagedockerFilecontextcontainerEnvremoteEnvcontainerUserremoteUserupdateRemoteUserUIDmountsworkspaceMountworkspaceFolderrunArgsoverrideCommandshutdownActiondockerComposeFile
有关可用于 devcontainer.json 的设置的更多信息,请参阅 Visual Studio Code 文档中的 devcontainer.json 参考。