Skip to main content

CloudflareDeployer

CloudflareDeployer 类负责将独立的 Mastra 应用部署到 Cloudflare Workers。它管理配置和部署,并通过 Cloudflare 特定功能扩展了基础的 Deployer 类。

🌐 The CloudflareDeployer class handles deployment of standalone Mastra applications to Cloudflare Workers. It manages configuration, deployment, and extends the base Deployer class with Cloudflare specific functionality.

warning

Cloudflare Workers 不支持文件系统访问。请从你的 Mastra 配置中移除所有使用文件 URL 的 LibSQLStore。可以使用内存存储(:memory:)或外部存储提供商,如 Turso、PostgreSQL 或 Upstash。

安装
Direct link to 安装

🌐 Installation

npm install @mastra/deployer-cloudflare@latest

使用示例
Direct link to 使用示例

🌐 Usage example

src/mastra/index.ts
import { Mastra } from "@mastra/core";
import { CloudflareDeployer } from "@mastra/deployer-cloudflare";

export const mastra = new Mastra({
deployer: new CloudflareDeployer({
projectName: "hello-mastra",
env: {
NODE_ENV: "production",
},
}),
});

查看 CloudflareDeployer API 参考,以获取所有可用的配置选项。

手动部署
Direct link to 手动部署

🌐 Manual deployment

也可以使用 Cloudflare Wrangler CLI 进行手动部署。安装 Wrangler CLI 后,在项目根目录运行以下命令来部署你的应用。

🌐 Manual deployments are also possible using the Cloudflare Wrangler CLI. With the Wrangler CLI installed run the following from your project root to deploy your application.

安装 Wrangler CLI 后,使用你的 Cloudflare 帐号登录并进行身份验证:

🌐 With the Wrangler CLI installed, login and authenticate with your Cloudflare logins:

npx wrangler login

运行以下命令将你的应用构建并部署到 Cloudflare

🌐 Run the following to build and deploy your application to Cloudflare

npm run build && wrangler deploy --config .mastra/output/wrangler.json

你也可以从项目根目录运行 wrangler dev --config .mastra/output/wrangler.json 来本地测试你的 Mastra 应用。

构建输出
Direct link to 构建输出

🌐 Build output

使用 CloudflareDeployer 构建 Mastra 应用的输出包括项目中的所有代理、工具和工作流,以及在 Cloudflare 上运行应用所需的 Mastra 特定文件。

🌐 The build output for Mastra applications using the CloudflareDeployer includes all agents, tools, and workflows in your project, along with Mastra specific files required to run your application on Cloudflare.

.mastra/
└── output/
├── index.mjs
└── wrangler.json
package.json

CloudflareDeployer 会在 .mastra/output 中自动生成一个 wrangler.json 配置文件,并包含以下设置:

🌐 The CloudflareDeployer automatically generates a wrangler.json configuration file in .mastra/output with the following settings:

{
"name": "hello-mastra",
"main": "./index.mjs",
"compatibility_date": "2025-04-01",
"compatibility_flags": [
"nodejs_compat",
"nodejs_compat_populate_process_env"
],
"observability": { "logs": { "enabled": true } },
"vars": {
"OPENAI_API_KEY": "...",
"CLOUDFLARE_API_TOKEN": "..."
}
}

下一步
Direct link to 下一步

🌐 Next steps