使用 Developer Connect 将 GitHub 连接到 Google Cloud

1. 简介

在此 Codelab 中,您将使用 Developer Connect 创建与 GitHub 代码库的安全连接,并使用该连接通过 Gemini Enterprise Agent Platform 上的 Agent Runtime 直接部署智能体。

Developer Connect 会引导您完成对非 Google 开发者工具的权限、授权、身份验证和网络配置,从而建立连接。这提供了一种直接的原生方式,可将您的应用代码拉取到 Google Cloud 服务中。

在此 Codelab 中,我们将利用 Developer Connect Git 代码库连接,通过 Gemini Enterprise Agent Platform 上的 Agent Runtime 直接部署智能体。Developer Connect 支持 GitHub、GitHub Enterprise、Bitbucket Cloud、Bitbucket Data Center、Gitlab 和 GitLab Enterprise。在此 Codelab 中,我们将演练与 GitHub 的连接。

您将执行的操作

  • 在 Agent Runtime 上创建基本智能体并将其推送到 GitHub
  • 使用 Developer Connect 将 GitHub 代码库关联到 Google Cloud
  • 使用关联的代码库在 Agent Runtime 上以原生方式部署智能体
  • 调用并测试已部署的远程代理

所需条件

  • 网络浏览器,例如 Chrome
  • 启用了结算功能的 Google Cloud 项目
  • GitHub 账号和具有代码库访问权限的个人访问令牌(传统)

此 Codelab 适用于各种水平的开发者,包括新手。在此 Codelab 中管理的大部分资源都是无服务器 API,费用应低于 1 美元。

2. 准备工作

创建 Google Cloud 项目

  1. Google Cloud 控制台 的项目选择器页面上,选择或创建一个 Google Cloud 项目
  2. 确保您的云项目已启用结算功能。了解如何检查项目是否已启用结算功能

启动 Cloud Shell

  1. 点击 Google Cloud 控制台顶部的激活 Cloud Shell
  2. 连接到 Cloud Shell 后,验证您的身份验证:
    gcloud auth list
    
  3. 确认您的项目已配置:
    export PROJECT_ID=$(gcloud config get-value project)
    
  4. 如果您的项目未按预期设置,请进行设置:
    export PROJECT_ID=<YOUR_PROJECT_ID>
    gcloud config set project $PROJECT_ID
    

启用 API

运行以下命令,为 Developer Connect 和 Vertex AI 启用所有必需的 API:

gcloud services enable \
  developerconnect.googleapis.com \
  aiplatform.googleapis.com

3. 准备智能体源代码

首先,您将创建一个新的 GitHub 代码库来存放智能体源代码,并向其中添加一个简单的 Python 推理智能体。

  1. 登录您的 GitHub 账号
  2. 创建一个名为 devconnect-agent 的新私有代码库。
  3. 请勿 使用 README 或 .gitignore 初始化该代码库。

在本地创建智能体文件

返回 Cloud Shell 终端,为您的智能体创建一个目录并定义其依赖项:

mkdir -p devconnect-agent/test
cd devconnect-agent

test 目录中创建一个 requirements.txt 文件,指定 Agent Runtime 库:

cat <<EOF > test/requirements.txt
google-cloud-aiplatform[agent_engines]
EOF

test 目录中创建一个 my_agent.py 文件。此脚本定义了一个简单的智能体,用于回答列表查询:

cat <<EOF > test/my_agent.py
class MyAgent:

  def query_none(self):
    return None

  def query_list(self):
    return [1, 2, 3]

  def register_operations(self):
    return {
        "": ["query_none", "query_list"],
    }

agent = MyAgent()
EOF

将代码推送到 GitHub

初始化 Git 代码库,并将代码推送到新创建的 GitHub 代码库。

将 `<YOUR_GITHUB_USERNAME>` 替换为您的 GitHub 用户名,并将 `<YOUR_GITHUB_TOKEN>` 替换为您的个人访问令牌。

git init
git branch -M main
git add .
git commit -m "Initial commit of agent source"
git remote add origin https://<YOUR_GITHUB_TOKEN>@github.com/<YOUR_GITHUB_USERNAME>/devconnect-agent.git
git push -u origin main

4. 配置 Developer Connect

现在,您的代码库已在 GitHub 上,Developer Connect 会安全地将您的 Google Cloud 项目关联到该代码库。

设置 IAM 权限

通过生成服务身份,授权 Developer Connect 访问您的 Google Cloud 项目。

gcloud beta services identity create \
    --service=developerconnect.googleapis.com \
    --project=$PROJECT_ID

您可以使用 Google Cloud 控制台或 gcloud CLI 创建连接和关联。

选项 1:使用 Google Cloud 控制台

  1. Google Cloud 控制台 中,前往 Developer Connect
  2. 点击 GitHub 下的 连接
  3. 将连接命名为 my-github-connection,并在 us-central1 中选择该连接
  4. 按照提示授权 Developer Connect GitHub 应用。
  5. 选择 devconnect-agent 代码库,将其关联到您的项目。

选项 2:使用 gcloud CLI

在 Cloud Shell 中运行以下命令,以关联您的 GitHub 代码库。

首先,您需要向 Developer Connect 服务账号授予对 Secret Manager 的访问权限。

# Get the service account
SERVICE_ACCOUNT=$(gcloud beta services identity create \
    --service=developerconnect.googleapis.com \
    --project=$PROJECT_ID \
    --format="value(email)")

# Grant access to Secret Manager
gcloud projects add-iam-policy-binding $PROJECT_ID \
    --member="serviceAccount:$SERVICE_ACCOUNT" \
    --role="roles/secretmanager.admin"
# 1. Create the general Developer Connect connection to GitHub
gcloud developer-connect connections create my-github-connection \
    --location=us-central1 \
    --github-config-app=developer-connect
# 2. Link your specific agent repository to the connection
# Replace <YOUR_GITHUB_USERNAME> with your actual GitHub username
gcloud developer-connect connections git-repository-links create devconnect-agent \
    --connection=my-github-connection \
    --location=us-central1 \
    --clone-uri=https://github.com/<YOUR_GITHUB_USERNAME>/devconnect-agent.git

5. 通过 Developer Connect 部署智能体

在代码库安全连接后,您可以利用 Developer Connect 链接以原生方式直接部署 Agent Runtime 智能体。

部署 Agent Runtime

在 Cloud Shell 中本地创建并运行 Python 脚本,以使用 Vertex AI SDK 部署智能体。

cd ~
cat <<EOF > deploy.py
import vertexai

PROJECT_ID = "$PROJECT_ID"
LOCATION = "us-central1"

vertexai.init(project=PROJECT_ID, location=LOCATION)
client = vertexai.Client(project=PROJECT_ID, location=LOCATION)

# Define the full URI string for the Developer Connect repository link
repo_link = f"projects/{PROJECT_ID}/locations/{LOCATION}/connections/my-github-connection/gitRepositoryLinks/devconnect-agent"

print("Deploying to Agent Runtime from Developer Connect...")

remote_agent = client.agent_engines.create(
    config={
        "developer_connect_source": {
            "git_repository_link": repo_link,
            "revision": "main",
            "dir": "test",
        },
        "entrypoint_module": "my_agent",
        "entrypoint_object": "agent",
        "requirements_file": "requirements.txt",
        "class_methods": [
            {"name": "query_list", "api_mode": ""}
        ],
        "display_name": "DevConnect Agent",
    },
)

print(f"Agent Runtime deployed successfully: {remote_agent.api_resource.name}")
EOF

在 gcloud 中配置默认应用凭据。

gcloud auth application-default login

运行部署脚本。请注意,此架构允许 Vertex AI 完全绕过本地执行范围,并从源代码构建远程代理映像。

python3 deploy.py

测试智能体

部署完成后,运行脚本以查询智能体端点。

cat <<EOF > invoke.py
import vertexai

PROJECT_ID = "$PROJECT_ID"
LOCATION = "us-central1"

client = vertexai.Client(project=PROJECT_ID, location=LOCATION)

# Retrieve the latest reasoning engine
engines = list(client.agent_engines.list())
if engines:
    agent = client.agent_engines.get(name=engines[0].api_resource.name)
    print("Invoking remote agent via endpoint...")
    # NOTE: Invoking remote agent
    response = agent.query_list()
    print(f"Agent response: {response}")
else:
    print("No deployment found.")
EOF

python3 invoke.py

您看到的输出结果应该类似于以下内容:

Invoking remote agent via endpoint...
Agent response: [1, 2, 3]

6. 清理

为避免系统持续向您的 Google Cloud 账号收费,请删除在此 Codelab 期间创建的资源。

清理 Developer Connect 和 Agent Runtime 资源:

cat <<EOF > cleanup.py
import vertexai

PROJECT_ID = "$PROJECT_ID"
LOCATION = "us-central1"

client = vertexai.Client(project=PROJECT_ID, location=LOCATION)

for engine in client.agent_engines.list():
    print(f"Deleting {engine.api_resource.name}")
    engine.delete()
EOF

python3 cleanup.py

清理 Developer Connect 资源:

gcloud developer-connect connections git-repository-links delete devconnect-agent \
    --connection=my-github-connection \
    --location=us-central1 \
    --quiet

gcloud developer-connect connections delete my-github-connection \
    --location=us-central1 \
    --quiet

7. 恭喜

恭喜!您已使用 Developer Connect 安全地建立 GitHub 代码库集成,并直接从源代码树以原生方式部署了 AI 智能体。

您学到的内容

  • 使用 Developer Connect 和 Vertex AI 配置了 Google Cloud 项目
  • 将个人访问令牌安全地存储到 Secret Manager 中
  • 通过 gcloud CLI 显式生成了 Developer Connect 连接
  • 使用 developer_connect_source 对象映射以编程方式创建了 Vertex AI Agent Runtime 实例。

后续步骤

参考文档