docs/src/content/en/reference/deployer/index.mdx
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.
If you want to create a custom deployer, you can extend the abstract Deployer class and implement the deploy method with your specific deployment logic. Here's an example:
import { Deployer } from '@mastra/deployer'
class CustomDeployer extends Deployer {
constructor() {
super({ name: 'custom-deployer' })
}
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
}
}
<PropertiesTable content={[ { name: 'args', type: 'object', description: 'Configuration options for the Deployer.', isOptional: false, }, { name: 'args.name', type: 'string', description: 'A unique name for the deployer instance.', isOptional: false, }, ]} />
<PropertiesTable content={[ { name: 'outputDirectory', type: 'string', description: 'The directory where the bundled and deployment-ready application will be output.', isOptional: false, }, ]} />
<PropertiesTable content={[ { name: 'getEnvFiles', type: '() => Promise<string[]>', description: "Returns a list of environment files to be used during deployment. By default, it looks for '.env.production' and '.env' files.", }, { name: 'deploy', type: '(outputDirectory: string) => Promise<void>', description: 'Abstract method that must be implemented by subclasses. Handles the deployment process to the specified output directory.', }, ]} />
The Deployer class inherits the following key methods from the Bundler class:
<PropertiesTable content={[ { name: 'prepare', type: '(outputDirectory: string) => Promise<void>', description: 'Prepares the output directory by cleaning it and creating necessary subdirectories.', }, { name: 'writePackageJson', type: '(outputDirectory: string, dependencies: Map<string, string>) => Promise<void>', description: 'Generates a package.json file in the output directory with the specified dependencies.', }, { name: '_bundle', type: '(serverFile: string, mastraEntryFile: string, outputDirectory: string, bundleLocation?: string) => Promise<void>', description: 'Bundles the application using the specified server and Mastra entry files.', }, ]} />
The Deployer abstract class implements a structured deployment lifecycle:
getEnvFiles method identifies environment files (.env.production, .env) to be used during deployment.prepare method (inherited from Bundler) cleans the output directory and creates necessary subdirectories._bundle method (inherited from Bundler) packages the application code and its dependencies.deploy method is implemented by subclasses to handle the actual deployment process.The Deployer class includes built-in support for environment file management through the getEnvFiles method. This method:
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([]);
}
The Deployer class extends the Bundler class, establishing a clear relationship between bundling and deployment:
deploy method allows for creating specialized deployers for different target environments.