Skip to content

Instantly share code, notes, and snippets.

@gengue
Created July 29, 2025 10:16
Show Gist options
  • Save gengue/5c3ec60b6e7e0597d87ac65edb46a85d to your computer and use it in GitHub Desktop.
Save gengue/5c3ec60b6e7e0597d87ac65edb46a85d to your computer and use it in GitHub Desktop.
Mastra google cloud run functions deplolyer
import { join } from 'path';
import process from 'process';
import { Deployer } from '@mastra/deployer';
import { DepsService } from '@mastra/deployer/services';
import { move, writeJson } from 'fs-extra/esm';
export class GoogleCloudRunFunctionDeployer extends Deployer {
constructor() {
super({ name: 'GOOGLE_CLOUD_RUN_FUNCTION' });
this.outputDir = join('.google', 'output');
}
async prepare(outputDirectory: string): Promise<void> {
await super.prepare(outputDirectory);
}
private getEntry(): string {
return `
import { getRequestListener } from '@hono/node-server'
import { mastra } from '#mastra';
import { createHonoServer, getToolExports } from '#server';
import { tools } from '#tools';
import { evaluate } from '@mastra/core/eval';
import { AvailableHooks, registerHook } from '@mastra/core/hooks';
import { TABLE_EVALS } from '@mastra/core/storage';
import { checkEvalStorageFields } from '@mastra/core/utils';
registerHook(AvailableHooks.ON_GENERATION, ({ input, output, metric, runId, agentName, instructions }) => {
evaluate({
agentName,
input,
metric,
output,
runId,
globalRunId: runId,
instructions,
});
});
registerHook(AvailableHooks.ON_EVALUATION, async traceObject => {
const storage = mastra.getStorage();
if (storage) {
// Check for required fields
const logger = mastra.getLogger();
const areFieldsValid = checkEvalStorageFields(traceObject, logger);
if (!areFieldsValid) return;
await storage.insert({
tableName: TABLE_EVALS,
record: {
input: traceObject.input,
output: traceObject.output,
result: JSON.stringify(traceObject.result || {}),
agent_name: traceObject.agentName,
metric_name: traceObject.metricName,
instructions: traceObject.instructions,
test_info: null,
global_run_id: traceObject.globalRunId,
run_id: traceObject.runId,
created_at: new Date().toISOString(),
},
});
}
});
const app = await createHonoServer(mastra, { tools: getToolExports(tools) });
export const handler = getRequestListener(app.fetch);
export default handler;
`;
}
async bundle(entryFile: string, outputDirectory: string, toolsPaths: string[]): Promise<void> {
const result = await this._bundle(
this.getEntry(),
entryFile,
outputDirectory,
toolsPaths,
join(outputDirectory, this.outputDir),
);
await move(join(outputDirectory, '.google', 'output'), join(process.cwd(), '.google', 'output'), {
overwrite: true,
});
return result;
}
async deploy(): Promise<void> {
this.logger?.info('Deploying to Google Cloud Run functions failed');
}
async lint(entryFile: string, outputDirectory: string, toolsPaths: string[]): Promise<void> {
await super.lint(entryFile, outputDirectory, toolsPaths);
}
}
gcloud functions deploy vbot \
--region=europe-west3 \
--runtime=nodejs22 \
--source=.google/output/ \
--entry-point=handler \
--trigger-http \
--no-allow-unauthenticated
import { Mastra } from '@mastra/core/mastra';
import { ragAgent } from "./agents/rag-agent";
export const mastra = new Mastra({
server: {
port: 8080,
},
workflows: {},
agents: { ragAgent },
deployer: new GoogleCloudRunFunctionDeployer(),
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment