连接到 Bitbucket Server 代码库

本页面介绍了如何将 Bitbucket Server 代码库连接到 Cloud Build。

准备工作

  • 启用 Cloud Build, Secret Manager, and Compute Engine API。

    启用 API

关联到 Bitbucket Server 代码库

控制台

如需使用 Google Cloud 控制台将 Bitbucket Server 代码库连接到 Cloud Build,请执行以下操作:

  1. 打开 Google Cloud 控制台中的制品库页面。

    打开“代码库”页面

  2. 在页面顶部,选择第 1 代标签页。

  3. 点击关联代码库

  4. 选择 Bitbucket Server

    您将看到关联代码库面板。完成以下步骤以连接您的 Bitbucket Server 代码库:

    1. 区域:选择您的连接所在的区域。

    2. 选择源代码管理提供方下,选择 Bitbucket Server

    3. 主机连接:从下拉菜单中选择主机连接的名称。

    4. 点击继续

    5. 选择代码库下,选择要关联到 Cloud Build 的 Bitbucket Server 代码库。

    6. 选择 Bitbucket Server 帐号和代码库后,请阅读意见征求免责声明,并选中旁边的复选框,以表示您接受所显示的条款。

    7. 点击关联以关联您的代码库。

    连接代码库后,Cloud Build 会在 Bitbucket Server 实例的代码库中配置网络钩子。然后,当您更改代码库时,代码库会发送网络钩子以调用相应的 Bitbucket Server 触发器。一个代码库也可以通过多个主机连接多次连接。如需了解如何管理网络钩子,请参阅管理网络钩子

    如需找到网络钩子,您需要获取网络钩子 ID。您可以在 BitbucketServerConfig 中找到每个已连接代码库的网络钩子 ID。

    输入以下命令以检索 webhook ID:

      gcloud alpha builds enterprise-config bitbucketserver describe CONFIG_NAME  --region=REGION
    

    其中:

    • CONFIG_NAME 是 Bitbucket Server 配置的名称
    • REGION 是 Bitbucket Server 主机连接的区域
  5. 点击完成。(可选)点击创建触发器以创建触发器。

API

如需使用 API 将 Bitbucket Server 代码库连接到 Cloud Build,请完成以下步骤:

  1. 使用以下 JSON 模板连接您的代码库:

    {
      "parent": "projects/PROJECT_NUMBER/locations/REGION/bitbucketServerConfigs/BITBUCKET_SERVER_CONFIG_NAME",
      "requests": {
        "parent": "projects/PROJECT_NUMBER/locations/REGION/bitbucketServerConfigs/BITBUCKET_SERVER_CONFIG_NAME",
       "bitbucketServerConnectedRepository": {
          "parent": "projects/PROJECT_NUMBER/locations/REGION/bitbucketServerConfigs/BITBUCKET_SERVER_CONFIG_NAME",
          "repo": {
            "projectKey": "PROJECT_KEY",
            "repoSlug": "REPO_SLUG"
           }
         }
      }
    }
    

    其中:

    • PROJECT_NUMBER 是您的 Cloud 项目的项目编号。
    • REGION 是与您的 Bitbucket Server 配置关联的区域
    • BITBUCKET_SERVER_CONFIG_NAME 是您的 Bitbucket Server 配置的名称。
    • PROJECT_KEY 是 Bitbucket Server 项目的密钥。 如果要连接个人代码库,您的项目密钥应在用户名前面包含波浪号 (~) 符号。例如 ~${USERNAME}。主机代码库的完整网址可能类似于 https://${HOST_URI}/projects/~${USER_NAME}/repos/${REPO_SLUG}
    • REPO_SLUG 是 Bitbucket Server 代码库的 slug
  2. 在终端中输入以下 curl 命令:

    curl -X POST -H "Authorization: Bearer "$(gcloud auth print-access-token) -H "Content-Type: application/json; charset=utf-8" -H "x-goog-user-project: PROJECT_NUMBER" https://cloudbuild.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/bitbucketServerConfigs/BITBUCKET_SERVER_CONFIG_NAME/connectedRepositories:batchCreate -d @requests.json
    

    其中:

    • PROJECT_NUMBER 是您的 Cloud 项目编号。
    • PROJECT_ID 是您的 Cloud 项目 ID。
    • REGION 是与您的 Bitbucket Server 配置关联的区域
    • BITBUCKET_SERVER_CONFIG_NAME 是您的 Bitbucket Server 配置的名称。

    如果成功,响应正文将包含一个新创建的 Operation 实例。

  3. 在终端中输入以下 curl 命令:

    curl -X GET -H "Authorization: Bearer "$(gcloud auth print-access-token) -H "Content-Type: application/json; charset=utf-8"  -H "x-goog-user-project: PROJECT_NUMBER" https://cloudbuild.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/operations/OPERATION_ID
    

    其中:

    • PROJECT_NUMBER 是您的 Cloud 项目编号。
    • PROJECT_ID 是您的 Cloud 项目 ID。
    • REGION 是与您的 Bitbucket Server 配置关联的区域
    • OPERATION_ID 是您的 Bitbucket Server 配置创建操作的 ID。您可以在响应的 name 字段中找到操作 ID。响应中 name 字段的格式如下所示:projects/project-id/locations/region/operations/operation-id

    您可能需要继续运行 GetOperation API 命令,直到响应包含 done: true(表示操作已完成)。如果 Bitbucket Server 代码库已成功连接,您可以在 response.bitbucketServerConnectedRepositories 字段中看到已连接的代码库。否则,请查看 error 字段以获取详细的错误报告。

后续步骤