เชื่อมต่อ GitHub กับ Google Cloud ด้วย Developer Connect

1. บทนำ

ใน Codelab นี้ คุณจะได้สร้างการเชื่อมต่อที่ปลอดภัยกับที่เก็บ GitHub โดยใช้ Developer Connect และใช้การเชื่อมต่อดังกล่าวเพื่อติดตั้งใช้งาน Agent โดยตรงโดยใช้ Agent Runtime บนแพลตฟอร์ม Gemini Enterprise Agent

Developer Connect จะสร้างการเชื่อมต่อโดยแนะนำการกำหนดค่าสิทธิ์ การให้สิทธิ์ การตรวจสอบสิทธิ์ และเครือข่ายไปยังเครื่องมือสำหรับนักพัฒนาแอปที่ไม่ใช่ของ Google ซึ่งเป็นวิธีดึงโค้ดของแอปพลิเคชันไปยังบริการของ Google Cloud โดยตรง

สำหรับ Codelab นี้ เราจะใช้การเชื่อมต่อที่เก็บ Git ของ Developer Connect เพื่อติดตั้งใช้งาน Agent โดยตรงโดยใช้ Agent Runtime บนแพลตฟอร์ม Gemini Enterprise Agent Developer Connect รองรับ GitHub, GitHub Enterprise, Bitbucket Cloud, Bitbucket Data Center, Gitlab และ GitLab Enterprise ใน Codelab นี้ เราจะแนะนำการเชื่อมต่อกับ GitHub

สิ่งที่คุณต้องทำ

  • สร้างเอเจนต์พื้นฐานใน Agent Runtime และพุชไปยัง GitHub
  • ใช้ Developer Connect เพื่อลิงก์ที่เก็บ GitHub กับ Google Cloud
  • ติดตั้งใช้งาน Agent ใน Agent Runtime โดยใช้ที่เก็บที่เชื่อมต่อโดยตรง
  • เรียกใช้และทดสอบ Agent ระยะไกลที่ติดตั้งใช้งาน

สิ่งที่คุณต้องมี

  • เว็บเบราว์เซอร์ เช่น Chrome
  • โปรเจ็กต์ Google Cloud ที่เปิดใช้การเรียกเก็บเงิน
  • บัญชี GitHub และโทเค็นเพื่อการเข้าถึงส่วนบุคคล (คลาสสิก) ที่มีสิทธิ์เข้าถึงที่เก็บ

Codelab นี้มีไว้สำหรับนักพัฒนาซอฟต์แวร์ทุกระดับ รวมถึงผู้เริ่มต้น ทรัพยากรที่จัดการใน Codelab นี้ส่วนใหญ่เป็น API แบบ Serverless และควรมีค่าใช้จ่ายไม่เกิน $1

2. ก่อนเริ่มต้น

สร้างโปรเจ็กต์ Google Cloud

  1. ในคอนโซล Google Cloud ให้เลือกหรือสร้างโปรเจ็กต์ Google Cloud ในหน้าตัวเลือกโปรเจ็กต์
  2. ตรวจสอบว่าได้เปิดใช้การเรียกเก็บเงินสำหรับโปรเจ็กต์ Cloud แล้ว ดูวิธีตรวจสอบว่าได้เปิดใช้การเรียกเก็บเงินในโปรเจ็กต์แล้วหรือไม่

เริ่มต้น Cloud Shell

  1. คลิกเปิดใช้งาน Cloud Shell ที่ด้านบนของคอนโซล Google Cloud
  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

เรียกใช้คำสั่งนี้เพื่อเปิดใช้ API ที่จำเป็นทั้งหมดสำหรับ Developer Connect และ Vertex AI

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

3. เตรียมซอร์สโค้ดของ Agent

ก่อนอื่นคุณจะต้องสร้างที่เก็บ GitHub ใหม่เพื่อเก็บซอร์สโค้ดของเอเจนต์ แล้วเพิ่มเอเจนต์การให้เหตุผล Python แบบง่ายลงในที่เก็บนั้น

  1. เข้าสู่ระบบบัญชี GitHub
  2. สร้างที่เก็บใหม่แบบส่วนตัวชื่อ devconnect-agent
  3. อย่าเริ่มต้นด้วย README หรือ .gitignore

สร้างไฟล์ Agent ในเครื่อง

กลับไปที่เทอร์มินัล Cloud Shell แล้วสร้างไดเรกทอรีสำหรับ Agent และกำหนดการอ้างอิงของ Agent ดังนี้

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

สร้างไฟล์ requirements.txt ในไดเรกทอรี test โดยระบุไลบรารีรันไทม์ของ Agent ดังนี้

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

สร้างไฟล์ my_agent.py ในไดเรกทอรี test สคริปต์นี้กำหนด Agent อย่างง่ายที่ตอบคำค้นหาแบบรายการ

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 กับที่เก็บอย่างปลอดภัย

ตั้งค่าสิทธิ์ IAM

ให้สิทธิ์ Developer Connect เข้าถึงโปรเจ็กต์ที่อยู่ในระบบคลาวด์ของ Google โดยสร้างข้อมูลประจำตัวของบริการ

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

คุณสร้างการเชื่อมต่อและลิงก์ได้โดยใช้คอนโซล Google Cloud หรือ gcloudCLI

ตัวเลือกที่ 1: ใช้คอนโซล Google Cloud

  1. ในคอนโซล Google Cloud ให้ไปที่ Developer Connect
  2. คลิกเชื่อมต่อในส่วน GitHub
  3. ตั้งชื่อการเชื่อมต่อ my-github-connection แล้วเลือกการเชื่อมต่อใน us-central1
  4. ทำตามข้อความแจ้งเพื่อให้สิทธิ์แอป GitHub ของ Developer Connect
  5. เลือกdevconnect-agentที่เก็บเพื่อลิงก์กับโปรเจ็กต์

ตัวเลือกที่ 2: การใช้ gcloud CLI

เรียกใช้คำสั่งต่อไปนี้ใน Cloud Shell เพื่อลิงก์ที่เก็บ GitHub

ก่อนอื่น คุณจะต้องให้สิทธิ์เข้าถึง Secret Manager แก่บัญชีบริการของ Developer Connect

# 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. ติดตั้งใช้งาน Agent จาก Developer Connect

เมื่อเชื่อมต่อที่เก็บอย่างปลอดภัยแล้ว คุณจะทำให้ Agent Runtime ใช้งานได้โดยตรงโดยใช้ประโยชน์จากลิงก์ Developer Connect โดยตรง

ติดตั้งใช้งาน Agent Runtime

สร้างและเรียกใช้สคริปต์ Python ในเครื่องใน Cloud Shell เพื่อติดตั้งใช้งานเอเจนต์โดยใช้ 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

ทดสอบ Agent

เมื่อการติดตั้งใช้งานเสร็จสิ้น ให้เรียกใช้สคริปต์เพื่อค้นหาข้อมูลปลายทางของเอเจนต์

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. ล้างข้อมูล

โปรดลบทรัพยากรที่สร้างขึ้นระหว่างการทำ Codelab นี้เพื่อหลีกเลี่ยงการเรียกเก็บเงินอย่างต่อเนื่องกับบัญชี Google Cloud

ล้างข้อมูลทรัพยากร 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. ขอแสดงความยินดี

ยินดีด้วย คุณได้สร้างการผสานรวมที่เก็บ GitHub อย่างปลอดภัยโดยใช้ Developer Connect และทำให้ AI Agent ทำงานได้โดยตรงจากโครงสร้างแหล่งที่มา

สิ่งที่คุณได้เรียนรู้

  • กำหนดค่าโปรเจ็กต์ Google Cloud ด้วย Developer Connect และ Vertex AI
  • จัดเก็บโทเค็นเพื่อการเข้าถึงส่วนบุคคลไว้ใน Secret Manager อย่างปลอดภัย
  • สร้างการเชื่อมต่อ Developer Connect อย่างชัดเจนผ่าน gcloud CLI
  • สร้างอินสแตนซ์รันไทม์ของ Vertex AI Agent โดยใช้การแมปออบเจ็กต์ developer_connect_source โดยอัตโนมัติ

ขั้นตอนถัดไป

  • สำรวจเพิ่มเติมด้วย Gemini Code Assist ที่ปรับแต่งโดยใช้การผสานรวมฐานของโค้ดของ Developer Connect
  • ทดลองสร้างAgent Runtime Agent ที่มีประสิทธิภาพ

เอกสารอ้างอิง