Skip to main content

部署器

🌐 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:

object
Configuration options for the Deployer.

args.name:

string
A unique name for the deployer instance.

部署参数
Direct link to 部署参数

🌐 deploy Parameters

outputDirectory:

string
The directory where the bundled and deployment-ready application will be output.

方法
Direct link to 方法

🌐 Methods

getEnvFiles:

() => Promise<string[]>
Returns a list of environment files to be used during deployment. By default, it looks for '.env.production' and '.env' files.

deploy:

(outputDirectory: string) => Promise<void>
Abstract method that must be implemented by subclasses. Handles the deployment process to the specified output directory.

从 Bundler 继承的方法
Direct link to 从 Bundler 继承的方法

🌐 Inherited Methods from Bundler

Deployer 类从 Bundler 类继承了以下关键方法:

🌐 The Deployer class inherits the following key methods from the Bundler class:

prepare:

(outputDirectory: string) => Promise<void>
Prepares the output directory by cleaning it and creating necessary subdirectories.

writePackageJson:

(outputDirectory: string, dependencies: Map<string, string>) => Promise<void>
Generates a package.json file in the output directory with the specified dependencies.

_bundle:

(serverFile: string, mastraEntryFile: string, outputDirectory: string, bundleLocation?: string) => Promise<void>
Bundles the application using the specified server and Mastra entry files.

核心概念
Direct link to 核心概念

🌐 Core Concepts

部署生命周期
Direct link to 部署生命周期

🌐 Deployment Lifecycle

Deployer 抽象类实现了一个结构化的部署生命周期:

🌐 The Deployer abstract class implements a structured deployment lifecycle:

  1. 初始化:部署器使用名称进行初始化,并创建一个 Deps 实例以进行依赖管理。
  2. 环境设置getEnvFiles 方法用于识别在部署期间使用的环境文件(.env.production、.env)。
  3. 准备prepare 方法(继承自 Bundler)会清理输出目录并创建必要的子目录。
  4. 打包_bundle 方法(继承自 Bundler)用于打包应用代码及其依赖。
  5. 部署:抽象 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:

  1. 打包作为前提条件:打包是部署的前提步骤,将应用代码打包成可部署的格式。
  2. 共享基础设施:打包和部署都共享诸如依赖管理和文件系统操作等公共基础设施。
  3. 专用部署逻辑:虽然打包侧重于代码封装,但部署则加入了用于部署打包代码的环境特定逻辑。
  4. 可扩展性:抽象的 deploy 方法允许为不同的目标环境创建专门的部署器。