Developer Connect を使用して GitHub を Google Cloud に接続する

1. はじめに

この Codelab では、Developer Connect を使用して GitHub リポジトリへの安全な接続を作成し、その接続を使用して Gemini Enterprise Agent Platform の Agent Runtime を使用してエージェントを直接デプロイします。

Developer Connect は、Google 以外のデベロッパー ツールに対する権限、承認、認証、ネットワーキング構成の手順を説明することで、接続を確立します。これにより、アプリケーション コードを Google Cloud サービスに直接ネイティブに pull できます。

この 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 に push する
  • 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

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 に push する

Git リポジトリを初期化し、コードを新しく作成した GitHub リポジトリに push します。

`<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 を生成して、Google Cloud プロジェクトにアクセスする権限を Developer Connect に付与します。

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 をデプロイする

Vertex AI SDK を使用してエージェントをデプロイするには、Cloud Shell で Python スクリプトを作成してローカルで実行します。

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 インスタンスをプログラムで作成した

次のステップ

リファレンス ドキュメント