关联到 GitHub Enterprise

本页介绍了如何创建与 GitHub Enterprise 的连接,以及如何创建指向 GitHub Enterprise 代码库的链接。 您可以使用 Google Cloud 控制台或 Google Cloud CLI 完成这些任务。

这些说明适用于想要将 GitHub Enterprise 源代码库与 Google 搭配使用的应用开发者、平台管理员和安全经理。具体来说,您可以将 GitHub Enterprise 代码库与 Gemini Code Assist 搭配使用。

如需详细了解 Developer Connect,请参阅 Developer Connect 概览

准备工作

  1. 登录您的 Google 账号。

    如果您还没有 Google 账号,请注册新账号

  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Developer Connect API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Developer Connect API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  8. 启用 Developer Connect 也会启用 Secret Manager API

  9. 确保您有权访问 GitHub Enterprise 上的账号。

    为确保团队工作的安全性,我们建议您使用机器人账号或团队共享的账号(而非个人账号)完成本指南中的任务。

  10. 确保您拥有 GitHub Enterprise 代码库,或者对共享代码库具有管理员级别的权限。
  11. 使用 GitHub Enterprise 组织中的代码库时,请确保您拥有 GitHub 应用管理器权限。

  12. 可选:创建客户管理的加密密钥 (CMEK),用于加密 Developer Connect 创建的身份验证 Secret。
  13. 可选:如需使用本指南中的命令行说明,请完成以下步骤:
    1. 安装 Google Cloud CLI。 如果您之前已经安装了 gcloud CLI,请运行 gcloud components update 以确保您拥有最新版本。
    2. 运行以下命令,创建Developer Connect 服务账号,其中 PROJECT_ID 是您的Google Cloud 项目 ID
              gcloud beta services identity create \
                  --service=developerconnect.googleapis.com \
                  --project=PROJECT_ID
              

授予所需的 IAM 权限

如需获得创建连接和关联所需的权限,请让您的管理员为您授予以下 IAM 角色:

  • 如果您不是项目所有者,则需要为您的用户账号授予 Developer Connect Admin (roles/developerconnect.admin) 角色。
  • 如果您计划使用 CMEK 加密 Developer Connect 创建的 Secret,请在 Secret Manager 服务账号上授予 Cloud KMS CryptoKey Encrypter/Decrypter (roles/cloudkms.cryptoKeyEncrypterDecrypter) 角色。
  • 如果您计划使用 gcloud CLI 完成本指南中的步骤: Developer Connect 服务账号的 Secret Manager Admin 角色 (roles/secretmanager.admin)。
  • 如果您计划使用 Google Cloud 控制台完成本指南中的步骤,则需要: 您的用户账号的 Project IAM Admin (roles/resourcemanager.projectIamAdmin) 角色。

如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

您也可以通过自定义角色或其他预定义角色来获取所需的权限。

创建连接

本部分介绍了如何在 Developer Connect 与 GitHub Enterprise 之间建立连接。如果您使用的是 Google Cloud 控制台,则还可以在完成连接设置后开始添加指向代码库的链接。

如需创建新的 GitHub Enterprise 连接,请选择以下选项之一:

控制台

如需发起连接,请完成以下步骤:

  1. 在 Google Cloud 控制台中,打开 Developer Connect

    前往 Developer Connect

    Developer Connect 会显示 Git 代码库页面。

    • 如果您看到源代码管理提供方列表:请选择一个源代码管理提供方,开始配置您的第一个关联。点击 GitHub Enterprise 卡片上的关联
    • 如果您看到一个列出现有连接的表格:请依次点击创建连接 > GitHub Enterprise,设置源代码管理提供商。

      随即会打开创建连接页面。

  2. 区域字段中,为连接资源选择一个区域

    1. 名称部分,输入新连接的名称。
  3. 选中启用符合数据驻留规定的连接复选框,使相应连接符合数据驻留要求

    如果您在创建 GitHub 连接时选中此复选框,则使用 Google Cloud 控制台创建的 GitHub 连接符合数据驻留要求。执行此操作时,Developer Connect 会创建 GitHub 应用

    使用 Google Cloud CLI 创建 GitHub 连接时,这些连接不符合数据驻留要求。所有其他连接类型均符合数据驻留规定。

    1. 主机网址字段中,输入要连接到的主机的网址。
  4. 点击显示更多以查看可选配置设置。

    1. 可选:默认情况下,Developer Connect 可以充当代理,以便向 GitHub Enterprise 发出 Git 调用。如需停用此选项,请清除启用 Developer Connect 代理复选框。

    2. 对于网络类型,选择公共互联网

      如果您想使用专用网络,请参阅连接到专用网络中托管的 GitHub Enterprise 代码库

    3. 可选:在加密部分中,选择一个 CMEK 密钥来加密 Developer Connect 创建的 Secret Manager Secret。

  5. 点击继续

    系统会显示一个 GitHub 对话框,邀请您创建 GitHub 应用。

  6. 按照 GitHub 中的提示完成以下任务:

    1. 输入应用的名称。

    2. 选择要安装应用的组织。

    3. 选择要授予访问权限的代码库。

    对话框随即关闭。

    Developer Connect 会从 GitHub 获取您的私钥,并将其作为 Secret Manager 密钥存储在您的 Google Cloud 项目中。如需查看 Secret,请列出 Secret Manager 中的 Secret

连接创建完毕后,系统会显示关联代码库页面。

请完成以下步骤,将代码库与您的连接相关联:

  1. 在可用代码库列表中,选择要使用的代码库。

  2. 点击确定

  3. 点击关联

您的连接已添加到 Google Cloud 控制台的连接页面,您的代码库链接已添加到代码库页面。您可以随时向现有连接添加更多链接

如果您正在设置 Gemini Code Assist,请按照配置和使用 Gemini Code Assist 代码自定义功能中的步骤继续完成该流程。

gcloud

以下任务需要在网络浏览器中执行一些操作。

如需发起连接,请完成以下步骤:

  1. 运行 gcloud developer-connect connections create 命令以创建与 GitHub Enterprise 的连接:

    gcloud developer-connect connections create CONNECTION_NAME \
        --location=REGION \
        --github-enterprise-config-host-uri=HOST_URI
        --git-proxy-config-enabled
    

    替换以下内容:

    • CONNECTION_NAME:连接的名称。
    • REGION:连接的区域
    • HOST_URI:您要连接的主机的 URI。
    • --git-proxy-config-enabled:一个可选标志,用于使 Developer Connect 能够充当 GitHub Enterprise 的 Git 调用代理。此功能目前为预览版
  2. 您必须创建 GitHub 应用才能管理与 GitHub 的连接。运行以下命令以获取应用设置的 URI:

    gcloud developer-connect connections describe CONNECTION_NAME \
        --location=REGION
    

    替换以下内容:

    • CONNECTION_NAME:连接的名称。
    • REGION:连接的区域

    Developer Connect 会返回一个链接,您可以在浏览器窗口中完成后续步骤。复制此链接。

  3. 在网络浏览器中打开该链接。

  4. 如果系统提示您登录 GitHub 账号,请登录。

  5. 按照 GitHub 中的提示完成以下任务:

    1. 输入应用的名称。

    2. 选择要安装应用的组织。

    3. 选择要授予访问权限的代码库。

    对话框随即关闭。

    Developer Connect 会从 GitHub 获取您的私钥,并将其作为 Secret Manager 密钥存储在您的 Google Cloud 项目中。如需查看 Secret,请列出 Secret Manager 中的 Secret

验证连接

运行以下命令,验证 GitHub 连接是否已创建:

gcloud developer-connect connections describe CONNECTION_NAME \
    --location=REGION

其中:

  • CONNECTION_NAME:连接的名称。
  • REGION:连接所在的区域。

Developer Connect 会在 installationState 字段中返回状态。如果此字段的值为 COMPLETE,则表示应用安装已完成。否则,Developer Connect 会返回一个 URI,以便您返回浏览器中的配置。

连接完成后,请按照关联到代码库中的说明选择要关联的代码库。

与 GitHub Enterprise 建立连接后,您就可以关联代码库了。您可以稍后重复执行这些步骤,根据需要关联其他代码库。

如需在现有 GitHub Enterprise 连接上创建代码库链接,请选择以下选项之一:

控制台

  1. 在 Google Cloud 控制台中打开代码库页面。

    打开代码库页面

  2. 点击关联代码库

    系统会打开关联 Git 代码库 窗格。

  3. 在连接列表中,选择一个连接。

  4. 点击继续

  5. 在代码库列表中,选择要关联的代码库。

  6. 点击创建

Developer Connect 会创建代码库链接,并在 Google Cloud 控制台中显示这些链接。

gcloud

运行以下命令:

gcloud developer-connect connections git-repository-links create REPO_NAME \
    --clone-uri=REPO_URI \
    --connection=CONNECTION_NAME \
    --location=REGION

替换以下内容:

  • REPO_NAME:代码库链接的名称。
  • REPO_URI:代码库的链接。 例如 https://github.com/cloud-build/test-repo.git
  • CONNECTION_NAME:连接的名称。
  • REGION:连接的区域

Developer Connect 会创建代码库链接。

如需列出关联的代码库,请运行 developer-connect connections git-repository-links list 命令。

如果您正在设置 Gemini Code Assist,请按照配置和使用 Gemini Code Assist 代码自定义功能中的步骤继续完成该流程。

数据共享

与此连接关联的 Google Cloud 资源的内容可能会转移到 GitHub。此语句适用于目前和未来使用此连接的所有资源。如需停止与 GitHub 共享信息,请在 Google Cloud 控制台或 gcloud CLI 中删除 Developer Connect 连接,并在 GitHub 中撤消访问权限

后续步骤