CloudflareDeployer
CloudflareDeployerは、MastraアプリケーションをCloudflare Workersにデプロイし、設定、環境変数、ルート管理を処理します。抽象Deployerクラスを拡張して、Cloudflare固有のデプロイ機能を提供します。
使用例
import { Mastra } from '@mastra/core';
import { CloudflareDeployer } from '@mastra/deployer-cloudflare';
const mastra = new Mastra({
deployer: new CloudflareDeployer({
scope: 'your-account-id',
projectName: 'your-project-name',
routes: [
{
pattern: 'example.com/*',
zone_name: 'example.com',
custom_domain: true,
},
],
workerNamespace: 'your-namespace',
auth: {
apiToken: 'your-api-token',
apiEmail: 'your-email',
},
}),
// ... other Mastra configuration options
});
パラメータ
コンストラクタパラメータ
scope:
string
あなたのCloudflareアカウントID。
projectName?:
string
= 'mastra'
ワーカープロジェクトの名前。
routes?:
CFRoute[]
ワーカーのルート設定の配列。
workerNamespace?:
string
ワーカーの名前空間。
env?:
Record<string, any>
ワーカー設定に含める環境変数。
auth:
object
Cloudflare認証の詳細。
authオブジェクト
apiToken:
string
あなたのCloudflare APIトークン。
apiEmail?:
string
あなたのCloudflareアカウントのメールアドレス。
CFRouteオブジェクト
pattern:
string
一致するURLパターン(例:'example.com/*')。
zone_name:
string
ドメインゾーン名。
custom_domain?:
boolean
= false
カスタムドメインを使用するかどうか。
環境変数
CloudflareDeployerは複数のソースから環境変数を処理します:
- 環境ファイル:
.env.production
と.env
ファイルからの変数。 - 設定:
env
パラメータを通じて渡される変数。
Mastraプロジェクトのビルド
Cloudflareデプロイメント用にMastraプロジェクトをビルドするには:
npx mastra build
ビルドプロセスは`.mastra/output`ディレクトリに以下の出力構造を生成します:
.mastra/output/ ├── index.mjs # メインワーカーのエントリーポイント ├── wrangler.json # Cloudflare Workerの設定 └── assets/ # 静的アセットと依存関係
### Wranglerの設定
CloudflareDeployerは以下の設定で`wrangler.json`設定ファイルを自動的に生成します:
```json
{
"name": "your-project-name",
"main": "./output/index.mjs",
"compatibility_date": "2024-12-02",
"compatibility_flags": ["nodejs_compat"],
"observability": {
"logs": {
"enabled": true
}
},
"vars": {
// .envファイルと設定からの環境変数
},
"routes": [
// 指定された場合のルート設定
]
}
ルート設定
ルートは、URLパターンとドメインに基づいてトラフィックをワーカーに転送するように設定できます:
const routes = [
{
pattern: 'api.example.com/*',
zone_name: 'example.com',
custom_domain: true,
},
{
pattern: 'example.com/api/*',
zone_name: 'example.com',
},
];
デプロイオプション
ビルド後、Mastraアプリケーションの.mastra/output
をCloudflare Workersに以下のいずれかの方法でデプロイできます:
-
Wrangler CLI: Cloudflareの公式CLIツールを使用して直接デプロイ
- CLIをインストール:
npm install -g wrangler
- 出力ディレクトリに移動:
cd .mastra/output
- Cloudflareアカウントにログイン:
wrangler login
- プレビュー環境にデプロイ:
wrangler deploy
- 本番環境へのデプロイ:
wrangler deploy --env production
- CLIをインストール:
-
Cloudflareダッシュボード: Cloudflareダッシュボードを通じてビルド出力を手動でアップロード
出力ディレクトリ
.mastra/output
でwrangler dev
を実行して、Mastraアプリケーションをローカルでテストすることもできます。