本页介绍了如何将 GitHub Enterprise 代码库连接到 Cloud Build。如需详细了解 Cloud Build 代码库,请参阅 Cloud Build 代码库。
准备工作
启用 Cloud Build 和 Secret Manager API。
启用 API 所需的角色
如需启用 API,您需要拥有 Service Usage Admin IAM 角色 (
roles/serviceusage.serviceUsageAdmin),该角色包含serviceusage.services.enable权限。了解如何授予 角色。
- 按照说明连接 GitHub Enterprise 主机。
- 确保您对要为其启用 GitHub Enterprise 触发器的代码库具有管理员访问权限。
连接到 GitHub Enterprise 代码库
控制台
如需将 GitHub Enterprise 代码库连接到主机连接,请完成以下步骤:
在 Google Cloud 控制台中打开代码库页面。
您会看到代码库 页面。
选择页面顶部的第 2 代 标签页。
点击关联代码库 以关联连接中的代码库。
您会看到关联代码库 面板。
在关联代码库 面板中,输入以下信息:
- 连接:从下拉菜单中选择一个连接。
代码库:选择要关联到连接的代码库。
代码库名称:输入代码库的名称。
- 已生成:选择此选项,以便 Cloud Build 代表您为所选代码库自动 生成代码库名称。
手动:选择此选项可为所选代码库手动指定名称。
如果您选择手动,则可以在代码库名称部分修改所选代码库的名称。
点击关联 以将代码库关联到连接。
您现在已成功将 GitHub Enterprise 代码库关联到连接。
gcloud
如需将 GitHub Enterprise 代码库添加到连接,请输入以下命令:
gcloud builds repositories create REPO_NAME \
--remote-uri=REPO_URI \
--connection=CONNECTION_NAME --region=REGION
其中:
- REPO_NAME 是代码库的名称。
- REPO_URI 是 GitHub 代码库的链接。
- CONNECTION_NAME 是在 Cloud Build 中通过 连接到 GitHub Enterprise 主机 创建的 GitHub Enterprise 主机连接的名称。
- REGION 是连接的 区域。
Terraform
如需将 GitHub Enterprise 代码库添加到连接,请将以下代码段添加到 Terraform 配置:
resource "google_cloudbuildv2_repository" "my-repository" {
project = "PROJECT_ID"
name = "REPO_NAME"
location = :REGION"
parent_connection = google_cloudbuildv2_connection.my-connection.name
remote_uri = "REMOTE_URI'
}
其中:
- PROJECT_ID 是 您的 Google Cloud 项目 ID。
- REPO_NAME 是 GitHub 代码库的名称。
- REGION 是连接的 区域。
- URI 是代码库的远程 URI。例如
https://github.com/myuser/myrepo.git。
后续步骤
- 了解如何从 GitHub Enterprise 构建代码库。
- 了解如何在专用网络中从 GitHub Enterprise 构建代码库。
- 了解如何在 Compute Engine 上执行蓝绿部署。
- 了解如何将其他代码库指定为构建的依赖项。