0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GitLab CI/CDで AI Agent を IBM watsonx Orchestrate に自動デプロイする

Last updated at Posted at 2025-07-13

はじめに

IBM watsonx OrchestrateのエージェントとツールをGitLab CI/CDで自動デプロイする方法を試してみました。手動でのデプロイから解放され、コード変更時に自動的にIBM watsonx Orchestrateへデプロイできるようになります。

※ 本記事では簡単のため、CI/CDのうちデプロイ(CD: Continuous Deployment)のみを実装しています。

image.png

前提条件

  • IBM Cloud アカウント
  • IBM watsonx Orchestrate インスタンス
  • GitLab プロジェクト

プロジェクト構成

.
├── .gitlab-ci.yml              # GitLab CI/CD パイプライン設定
├── agents/
│   └── hello-agent.yaml        # エージェント定義
├── tools/
│   └── return_hello.py         # ツール実装
└── README.md

手順

1. 認証情報の設定

GitLab プロジェクトにCI/CD Variablesを設定します。

認証情報の取得:

  1. IBM Cloud コンソールにログイン
  2. IBM watsonx Orchestrate インスタンスを選択
  3. 「サービス資格情報」からAPI キーを取得
  4. サービスインスタンスURLを確認(https://api.us-south.watson-orchestrate.cloud.ibm.com/instances/your-instance-idの形式)

設定方法:

  1. GitLabプロジェクトの「Settings」→「CI/CD」を開く
  2. 「Variables」セクションを展開
  3. 「Add variable」をクリック
  4. 以下のVariablesを追加

WO_API_KEY の設定:

  • Key: WO_API_KEY
  • Value: [あなたのAPI キー]
  • Type: Variable
  • Environment scope: *
  • Protect variable: ✅ (mainブランチがProtectedの場合)
  • Mask variable: ✅
  • Expand variable reference: ❌

WO_URL の設定:

  • Key: WO_URL
  • Value: IBM watsonx Orchestrate インスタンス URL
  • Type: Variable
  • Environment scope: *
  • Protect variable: ✅ (mainブランチがProtectedの場合)
  • Mask variable: ❌
  • Expand variable reference: ❌

2. ツールの実装

from ibm_watsonx_orchestrate.agent_builder.tools import tool

@tool
def return_hello(name: str) -> str:
    """
    Returns a greeting with the provided name
    :param name: The name to include in the greeting
    :returns: A greeting string in the format "Hello [name]"
    """
    return "Hi " + name

3. エージェントの定義

spec_version: v1
style: default
name: hello_agent
llm: watsonx/meta-llama/llama-3-2-90b-vision-instruct
description: >
  名前を確認してあいさつを返すエージェント
instructions: >
  あいさつされたら、名前を確認してreturn_helloに名前を渡して、結果を返して。
collaborators: []
tools:
  - return_hello

4. GitLab CI/CD パイプライン

stages:
  - deploy

variables:
  PYTHON_VERSION: "3.11"

deploy_watsonx_orchestrate:
  stage: deploy
  image: python:${PYTHON_VERSION}

  # mainブランチへのpushとマージリクエストでトリガー
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"

  before_script:
    - python -m pip install --upgrade pip
    - pip install ibm-watsonx-orchestrate

  script:
    - echo "Configuring watsonx Orchestrate environment..."
    - echo "WO_URL = $WO_URL"
    - echo "WO_API_KEY = ${WO_API_KEY:0:10}..."

    - orchestrate env add -n production -u "$WO_URL"
    - orchestrate env activate production --api-key "$WO_API_KEY"

    - echo "Importing tools..."
    - orchestrate tools import -k python -f tools/return_hello.py

    - echo "Importing agents..."
    - orchestrate agents import -f agents/hello-agent.yaml

    - echo "Deployment completed successfully!"

  # キャッシュ設定(オプション)
  cache:
    paths:
      - .cache/pip/

GitLab CI/CD Variables の重要な注意点

Variables の権限設定

GitLab CI/CD Variablesで以下の点に注意してください:

  • Protect variable にチェックを入れた場合、Protected branches でのみ変数が利用可能
  • mainブランチがProtected branchとして設定されていない場合は Protect variable のチェックを外す
  • Environment scope* (アスタリスク) に設定

作成したエージェントの動作

デプロイが完了すると、IBM watsonx Orchestrateでエージェントが利用できるようになります。

image.png

シンプルなhello-agentですが、以下のような動作を確認できます:

  • ユーザーからの挨拶に対してツールを使用した応答

GitLab CI/CDパイプラインの実行確認

mainブランチにプッシュまたはマージリクエストを作成すると、GitLab CI/CDが自動的に実行されます。

自動実行される処理:

  1. Python環境のセットアップ
  2. IBM watsonx Orchestrate CLIのインストール
  3. 環境設定と認証
  4. ツールのデプロイ
  5. エージェントのデプロイ

image.png

ローカルでの動作確認

デプロイ前にローカルで動作確認することも可能です。

# IBM watsonx Orchestrate CLIをインストール
pip install ibm-watsonx-orchestrate

# 環境設定
orchestrate env add -n test -u YOUR_URL
orchestrate env activate test --api-key YOUR_API_KEY

# ツールをインポート
orchestrate tools import -k python -f tools/return_hello.py

# エージェントをインポート
orchestrate agents import -f agents/hello-agent.yaml

まとめ

GitLab CI/CDとIBM watsonx Orchestrate CLIを組み合わせることで、エージェントとツールの自動デプロイが実現できました。これにより開発効率が大幅に向上し、継続的な改善が可能になります。

コード変更から本番環境への反映までが自動化されるため、開発者はエージェントとツールの作成に集中できるようになります。

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?