mirror of
https://github.com/certd/certd.git
synced 2026-04-14 12:30:54 +08:00
chore: email
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { ConcurrencyStrategy, Pipeline, ResultType, Runnable, RunStrategy, Stage, Step, Task } from "../d.ts";
|
||||
import { ConcurrencyStrategy, NotificationType, NotificationWhen, Pipeline, ResultType, Runnable, RunStrategy, Stage, Step, Task } from "../d.ts";
|
||||
import _ from "lodash";
|
||||
import { RunHistory, RunnableCollection } from "./run-history";
|
||||
import { AbstractTaskPlugin, PluginDefine, pluginRegistry } from "../plugin";
|
||||
@@ -10,6 +10,7 @@ import { request } from "../utils/util.request";
|
||||
import { IAccessService } from "../access";
|
||||
import { RegistryItem } from "../registry";
|
||||
import { Decorator } from "../decorator";
|
||||
import { IEmailService } from "../service";
|
||||
|
||||
export type ExecutorOptions = {
|
||||
userId: any;
|
||||
@@ -17,6 +18,7 @@ export type ExecutorOptions = {
|
||||
storage: IStorage;
|
||||
onChanged: (history: RunHistory) => Promise<void>;
|
||||
accessService: IAccessService;
|
||||
emailService: IEmailService;
|
||||
};
|
||||
export class Executor {
|
||||
pipeline: Pipeline;
|
||||
@@ -51,10 +53,13 @@ export class Executor {
|
||||
// 读取last
|
||||
this.runtime = new RunHistory(runtimeId, trigger, this.pipeline);
|
||||
this.logger.info(`pipeline.${this.pipeline.id} start`);
|
||||
await this.notification("start");
|
||||
await this.runWithHistory(this.pipeline, "pipeline", async () => {
|
||||
await this.runStages(this.pipeline);
|
||||
});
|
||||
await this.notification("success");
|
||||
} catch (e) {
|
||||
await this.notification("error");
|
||||
this.logger.error("pipeline 执行失败", e);
|
||||
} finally {
|
||||
await this.pipelineContext.setObj("lastRuntime", this.runtime);
|
||||
@@ -203,4 +208,37 @@ export class Executor {
|
||||
this.runtime.context[stepOutputKey] = instance[key];
|
||||
});
|
||||
}
|
||||
|
||||
async notification(when: NotificationWhen, error?: any) {
|
||||
if (!this.pipeline.notifications) {
|
||||
return;
|
||||
}
|
||||
let subject = "";
|
||||
let content = "";
|
||||
if (when === "start") {
|
||||
subject = `【CertD】${this.pipeline.title} 开始执行,buildId:${this.runtime.id}`;
|
||||
content = `【CertD】${this.pipeline.title} 开始执行,buildId:${this.runtime.id}`;
|
||||
} else if (when === "success") {
|
||||
subject = `【CertD】${this.pipeline.title} 执行成功,buildId:${this.runtime.id}`;
|
||||
content = `【CertD】${this.pipeline.title} 执行成功,buildId:${this.runtime.id}`;
|
||||
} else if (when === "error") {
|
||||
subject = `【CertD】${this.pipeline.title} 执行失败,buildId:${this.runtime.id}`;
|
||||
content = `<pre>${error.message}</pre>`;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const notification of this.pipeline.notifications) {
|
||||
if (!notification.when.includes(when)) {
|
||||
continue;
|
||||
}
|
||||
if (notification.type === "email") {
|
||||
this.options.emailService?.send({
|
||||
subject,
|
||||
content,
|
||||
receivers: notification.options.receivers,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,3 +7,4 @@ export * from "./plugin";
|
||||
export * from "./utils";
|
||||
export * from "./context";
|
||||
export * from "./decorator";
|
||||
export * from "./service";
|
||||
|
||||
9
packages/core/pipeline/src/service/index.ts
Normal file
9
packages/core/pipeline/src/service/index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export type EmailSend = {
|
||||
subject: string;
|
||||
content: string;
|
||||
receivers: string[];
|
||||
};
|
||||
|
||||
export interface IEmailService {
|
||||
send(email: EmailSend): Promise<void>;
|
||||
}
|
||||
Reference in New Issue
Block a user