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 프로젝트에 결제가 사용 설정되어 있는지 확인합니다. 프로젝트에 결제가 사용 설정되어 있는지 확인하는 방법을 알아보세요.

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

Agent Runtime 라이브러리를 지정하는 requirements.txt 파일을 test 디렉터리에 만듭니다.

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 권한 설정

서비스 ID를 생성하여 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 인스턴스를 만들었습니다.

다음 단계

참조 문서