部署器
🌐 Deployer
部署器负责部署独立的 Mastra 应用,通过打包代码、管理环境文件以及使用 Hono 框架提供应用服务。具体实现必须为特定的部署目标定义 deploy 方法。
🌐 The Deployer handles the deployment of standalone Mastra applications by packaging code, managing environment files, and serving applications using the Hono framework. Concrete implementations must define the deploy method for specific deployment targets.
使用示例Direct link to 使用示例
🌐 Usage Example
import { Deployer } from "@mastra/deployer";
// Create a custom deployer by extending the abstract Deployer class
class CustomDeployer extends Deployer {
constructor() {
super({ name: "custom-deployer" });
}
// Implement the abstract deploy method
async deploy(outputDirectory: string): Promise<void> {
// Prepare the output directory
await this.prepare(outputDirectory);
// Bundle the application
await this._bundle("server.ts", "mastra.ts", outputDirectory);
// Custom deployment logic
}
}
参数Direct link to 参数
🌐 Parameters
构造函数参数Direct link to 构造函数参数
🌐 Constructor Parameters
args:
args.name:
部署参数Direct link to 部署参数
🌐 deploy Parameters
outputDirectory:
方法Direct link to 方法
🌐 Methods
getEnvFiles:
deploy:
从 Bundler 继承的方法Direct link to 从 Bundler 继承的方法
🌐 Inherited Methods from Bundler
Deployer 类从 Bundler 类继承了以下关键方法:
🌐 The Deployer class inherits the following key methods from the Bundler class:
prepare:
writePackageJson:
_bundle:
核心概念Direct link to 核心概念
🌐 Core Concepts
部署生命周期Direct link to 部署生命周期
🌐 Deployment Lifecycle
Deployer 抽象类实现了一个结构化的部署生命周期:
🌐 The Deployer abstract class implements a structured deployment lifecycle:
- 初始化:部署器使用名称进行初始化,并创建一个 Deps 实例以进行依赖管理。
- 环境设置:
getEnvFiles方法用于识别在部署期间使用的环境文件(.env.production、.env)。 - 准备:
prepare方法(继承自 Bundler)会清理输出目录并创建必要的子目录。 - 打包:
_bundle方法(继承自 Bundler)用于打包应用代码及其依赖。 - 部署:抽象
deploy方法由子类实现,用于处理实际的部署过程。
环境文件管理Direct link to 环境文件管理
🌐 Environment File Management
Deployer 类通过 getEnvFiles 方法内置支持环境文件管理。此方法:
🌐 The Deployer class includes built-in support for environment file management through the getEnvFiles method. This method:
- 按预定义顺序查找环境文件(.env.production, .env)
- 使用 FileService 查找第一个存在的文件
- 返回找到的环境文件数组
- 如果未找到环境文件,则返回空数组
getEnvFiles(): Promise<string[]> {
const possibleFiles = ['.env.production', '.env.local', '.env'];
try {
const fileService = new FileService();
const envFile = fileService.getFirstExistingFile(possibleFiles);
return Promise.resolve([envFile]);
} catch {}
return Promise.resolve([]);
}
打包与部署关系Direct link to 打包与部署关系
🌐 Bundling and Deployment Relationship
Deployer 类继承了 Bundler 类,建立了打包与部署之间的明确关系:
🌐 The Deployer class extends the Bundler class, establishing a clear relationship between bundling and deployment:
- 打包作为前提条件:打包是部署的前提步骤,将应用代码打包成可部署的格式。
- 共享基础设施:打包和部署都共享诸如依赖管理和文件系统操作等公共基础设施。
- 专用部署逻辑:虽然打包侧重于代码封装,但部署则加入了用于部署打包代码的环境特定逻辑。
- 可扩展性:抽象的
deploy方法允许为不同的目标环境创建专门的部署器。