# 连接版本控制系统

> Use Unity Build Automation with a version control system.

要在项目中使用 Build Automation，首先必须将项目托管在一个源代码控制系统（也称为版本控制系统）中。Unity Build Automation 支持以下系统：

* [Unity Version Control](#connect-build-automation-to-unity-version-control)
* [Git](#connect-build-automation-to-git)
* [Azure](#connect-build-automation-to-azure)
* [SVN](#connect-build-automation-to-svn)
* [Perforce](#connect-build-automation-to-perforce)

以下说明如何为 Build Automation 设置源代码控制。

## 将 Build Automation 连接到 Unity Version Control##connect-build-automation-to-unity-version-control

Unity Build Automation 支持存储在 UVCS 代码仓库中的项目。

> **Note:**
>
> **注意**：此产品需要订阅 Unity DevOps。

1. 从 Build settings（构建设置）中，选择 **Source control（源代码控制）** 选项卡。
2. 从下拉选单中选择 **UVCS**。
3. 在 **source control provider URL（源代码控制提供者 URL）** 字段中，添加 UVCS 云服务器的 URL，或者从可用组织列表中选择。
4. 选择 **Authenticate with Unity ID（使用 Unity ID 进行身份验证）** 或 **Authenticate with username or password（使用用户名或密码进行身份验证）**。
5. 如果选择 **Authenticate with username or password（使用用户名或密码进行身份验证）**，请输入 UVCS 服务器的用户名和密码。
6. 从下拉选单中选择一种**身份验证类型**。
7. 如果代码仓库已加密，请选中 **My repository is encrypted（我的代码仓库已加密）**复选框。a.添加**加密密钥**。b.添加**加密方法**。
8. 选择 **Save（保存）**。

## 将 Build Automation 连接到 Git##connect-build-automation-to-git

Unity Build Automation 支持存储在 Git 代码仓库中的项目。代码仓库可以托管在 [GitHub](https://github.com/)、[GitLab](https://about.gitlab.com/)、[Bitbucket](https://bitbucket.org/) 或私有服务器上。

您可以使用以下授权协议来配置对代码仓库的访问：

* [个人访问令牌](#personal-access-tokens)（默认）
* [SSH 密钥](#ssh-keys-and-private-repositories)

如果使用的是 Windows 操作系统，还可以选择构建将使用的 [Git 可执行文件](../optimize-build-speed/choose-git-executable)。

### 个人访问令牌##personal-access-tokens

要将 Build Automation 配置为使用个人访问令牌来访问代码仓库，请执行以下操作：

1. 从 Build settings（构建设置）中，选择 **Source control（源代码控制）** 选项卡。
2. 从下拉选单中选择 **GitHub**、**Bitbucket** 或 **GitLab**。
3. 从 Git 提供者生成个人访问令牌。
4. 输入您的令牌并选择 **Authorize（授权）**，以便 Unity 可以访问您的提供者帐户。
5. 从下拉选单中选择您的首选代码仓库。
6. 从 Build settings（构建设置）页面中，选择 **Save（保存）**。

个人访问令牌因提供者而异。下面列出了每个提供者的相关详细信息，但有关如何生成和使用令牌的最新详细信息，请参阅提供者的文档。

#### GitHub##github

Build Automation 要求您对要构建的代码仓库具有读取权限。

如果使用的是个人访问令牌（经典），则所需的特定权限为：如果要构建的代码仓库是私有代码仓库，则需要 `repo` 权限；如果要构建的代码仓库是公开代码仓库并且您希望允许最低访问级别，则需要 `public_repo` 权限。

如果您计划在 Build Automation 中启用自动构建，则还需要 `write:repo_hook` 权限。

即使您在创建令牌时没有提供到期日期，GitHub 也会自动移除一年内未使用的经典个人访问令牌。

可以为用户或组织创建细粒度的个人访问令牌。可以授予对用户或组织拥有的所有代码仓库的访问权限，也可以仅授予对特定代码仓库的访问权限。如果选择使用细粒度令牌，并且配置的令牌不只是具有公开代码仓库访问权限，则必须为访问令牌添加特定权限。

以下是使用细粒度令牌的最低代码仓库权限要求：

* `Contents` `Read-only`
* `Webhooks` `Read and write`（如果使用自动构建）

Build Automation 不需要任何帐户权限。

请注意，细粒度个人访问令牌始终具有过期日期，一旦令牌过期，Build Automation 将无法再访问您的代码仓库。

> **Note:**
>
> 细粒度个人访问令牌目前处于测试阶段，所需权限（或令牌的其他方面）可能会发生变化，这可能要求您采取措施，以继续在 Build Automation 中使用这些令牌。

#### GitLab##gitlab

为了让 Build Automation 构建代码仓库，必须向个人访问令牌授予 `read_repository` 范围的权限。如果打算在 Build Automation 中启用自动构建，则还需要 `write_repository` 范围的权限。

GitLab 令牌始终会在创建时设置过期日期，一旦令牌过期，Build Automation 将无法再访问您的代码仓库。

#### Bitbucket##bitbucket

代码仓库访问令牌授予对单个代码仓库的访问权限，因此您必须为要构建的每个代码仓库创建一个令牌。

为了让 Build Automation 构建代码仓库，必须向令牌授予 `Repositories` 范围的 `Read` 权限。如果打算在 Build Automation 中启用自动构建，还需要授予 `Webhooks` 范围的 `Read and write` 权限。

对于所有提供者，如果您撤销访问令牌、令牌过期，或者与令牌关联的用户对代码仓库的访问权限被降低或撤销，则 Build Automation 将无法再访问代码仓库。

有关下一步操作，请参阅“设置目标构建平台”。

### SSH 密钥和私有代码仓库##ssh-keys-and-private-repositories

Build Automation 在连接到托管站点时，会自动检测代码仓库是公开还是私有代码仓库。如果代码仓库是公开代码仓库，则 Build Automation 会自动连接到该代码仓库，并可跳至 [Build Configuration（构建配置）](../basic-build-configuration/overview)。

如果代码仓库是私有代码仓库，则会生成 SSH 密钥并显示在代码仓库 URL 下方的设置屏幕中。将此 SSH 密钥添加到源代码控制提供者的设置中，以授予 Build Automation 访问权限。

要将 Build Automation 配置为使用 SSH 密钥来访问代码仓库，请执行以下操作：

1. 在 **Source control（源代码控制）** 选项卡中，从下拉选单中选择 **Manual setup（手动设置）**，然后选择 **Git**。
2. 在 **Source control provider URL（源代码控制提供者 URL）** 字段中，添加 Git 服务器的 URL。以下是 [GitHub](https://github.com/)、[Bitbucket](https://bitbucket.org/) 和 [GitLab](https://about.gitlab.com/) 的 URL 示例：
   * `git@github.com:youraccount/yourrepo.git`
   * `git@bitbucket.org:youraccount/yourrepo.git`
   * `git@gitlab.com:youracccount/yourrepo.git`
3. 如果代码仓库是私有代码仓库，请将 SSH 密钥添加到源代码控制提供者设置中。
4. 设置完成后，在 Source control（源代码控制）选项卡上，选择 **Save（保存**）。

## 将 Build Automation 连接到 Azure##connect-build-automation-to-azure

Unity Build Automation 支持存储在 Azure 代码仓库中的项目。

### 在 Dashboard 上配置 Azure##configuring-azure-on-the-dashboard

1. 从 Build settings（构建设置）页面中，选择 **Source control（源代码控制）** 选项卡。
2. 从下拉选单中选择 **Azure**。
3. 添加您的 **Azure 组织**。
4. 添加代码仓库所属的 **Azure 项目**。
5. 创建并提供 [Azure 个人访问令牌](https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops\&tabs=Windows#create-a-pat)。
6. 选择 **Authorize（授权）** 以允许 Unity 检索代码仓库。
7. 选择 **Save（保存）**。

## 将 Build Automation 连接到 SVN##connect-build-automation-to-svn

Unity Build Automation 支持托管在 [Apache Subversion](https://subversion.apache.org/) (SVN) 代码仓库中的项目。

1. 从 Build settings（构建设置）页面中，选择 **Source control（源代码控制）** 选项卡。
2. 从下拉选单中选择 **Manual setup（手动设置）**，然后选择 **SVN**。
3. 在 SVN 服务器中，仅为 Unity Build Automation 创建一个只读用户帐户以及安全密码。
4. 在 Build settings（构建设置）页面的相应字段中，输入新的用户名和密码。
5. 选择 **Save（保存）**。

### 公有 SSH 密钥##public-ssh-keys

Build Automation 不支持使用公有 SSH 密钥连接到 SVN 代码仓库。请改用用户名和密码。

### SSL 证书##ssl-certificates

不支持自签名的 SSL 证书。Build Automation 不支持与具有自签名证书的服务器进行 SSL 握手。证书中的主机名必须与 Build Automation 访问的主机名匹配。

## 将 Build Automation 连接到 Perforce##connect-build-automation-to-perforce

Unity Build Automation 支持存储在 [Perforce](https://www.perforce.com/) 代码仓库中的项目。

### 在 Dashboard 上配置 Perforce##configuring-perforce-on-the-dashboard

1. 从 Build settings（构建设置）页面中，选择 **Source control（源代码控制）** 选项卡。
2. 从下拉选单中选择 **Manual setup（手动设置）**，然后选择 **Perforce**。
3. 在 source control provider URL（源代码控制提供者 URL）字段中，输入 Perforce 服务器的 URL。您可以指定使用 `https://` 或 SSL 协议的 URL。以下是有效的 URL 示例：
   * `https://127.0.0.1:1667`
   * `ssl:127.0.0.1:167`
4. 在 P4 中，创建一个对代码仓库具有只读访问权限的用户。
5. 在 Build settings（构建设置）页面的相应字段中，输入用户名和密码。
6. 选择 **Save（保存）**。

## 后续步骤##next-steps

* [创建第一个构建配置](../basic-build-configuration/overview)。
* [运行第一个构建](../run-builds/run-builds-manually)。
