feat(server): 支持稀疏检出路径并完善部署执行队列

- 在部署DTO中添加sparseCheckoutPaths字段支持稀疏检出路径
- 数据模型Deployment新增稀疏检出路径字段及相关数据库映射
- 部署创建时支持设置稀疏检出路径字段
- 部署重试接口实现,支持复制原始部署记录并加入执行队列
- 新增流水线模板初始化与基于模板创建流水线接口
- 优化应用初始化流程,确保执行队列和流水线模板正确加载
- 添加启动日志,提示执行队列初始化完成
This commit is contained in:
2025-12-12 23:21:26 +08:00
parent 73240d94b1
commit 9897bd04c2
22 changed files with 1307 additions and 136 deletions

View File

@@ -13,6 +13,7 @@ export const createDeploymentSchema = z.object({
commitHash: z.string().min(1, { message: '提交哈希不能为空' }),
commitMessage: z.string().min(1, { message: '提交信息不能为空' }),
env: z.string().optional(),
sparseCheckoutPaths: z.string().optional(), // 添加稀疏检出路径字段
});
export type ListDeploymentsQuery = z.infer<typeof listDeploymentsQuerySchema>;

View File

@@ -3,6 +3,7 @@ import type { Prisma } from '../../generated/client.ts';
import { prisma } from '../../libs/prisma.ts';
import type { Context } from 'koa';
import { listDeploymentsQuerySchema, createDeploymentSchema } from './dto.ts';
import { ExecutionQueue } from '../../libs/execution-queue.ts';
@Controller('/deployments')
export class DeploymentController {
@@ -50,12 +51,69 @@ export class DeploymentController {
},
pipelineId: body.pipelineId,
env: body.env || 'dev',
sparseCheckoutPaths: body.sparseCheckoutPaths || '', // 添加稀疏检出路径
buildLog: '',
createdBy: 'system', // TODO: get from user
updatedBy: 'system',
valid: 1,
},
});
// 将新创建的部署任务添加到执行队列
const executionQueue = ExecutionQueue.getInstance();
await executionQueue.addTask(result.id, result.pipelineId);
return result;
}
// 添加重新执行部署的接口
@Post('/:id/retry')
async retry(ctx: Context) {
const { id } = ctx.params;
// 获取原始部署记录
const originalDeployment = await prisma.deployment.findUnique({
where: { id: Number(id) }
});
if (!originalDeployment) {
ctx.status = 404;
ctx.body = {
code: 404,
message: '部署记录不存在',
data: null,
timestamp: Date.now()
};
return;
}
// 创建一个新的部署记录,复制原始记录的信息
const newDeployment = await prisma.deployment.create({
data: {
branch: originalDeployment.branch,
commitHash: originalDeployment.commitHash,
commitMessage: originalDeployment.commitMessage,
status: 'pending',
projectId: originalDeployment.projectId,
pipelineId: originalDeployment.pipelineId,
env: originalDeployment.env,
sparseCheckoutPaths: originalDeployment.sparseCheckoutPaths,
buildLog: '',
createdBy: 'system',
updatedBy: 'system',
valid: 1,
},
});
// 将新创建的部署任务添加到执行队列
const executionQueue = ExecutionQueue.getInstance();
await executionQueue.addTask(newDeployment.id, newDeployment.pipelineId);
ctx.body = {
code: 0,
message: '重新执行任务已创建',
data: newDeployment,
timestamp: Date.now()
};
}
}

View File

@@ -3,6 +3,7 @@ import { Controller, Get, Post, Put, Delete } from '../../decorators/route.ts';
import { prisma } from '../../libs/prisma.ts';
import { log } from '../../libs/logger.ts';
import { BusinessError } from '../../middlewares/exception.ts';
import { getAvailableTemplates, createPipelineFromTemplate } from '../../libs/pipeline-template.ts';
import {
createPipelineSchema,
updatePipelineSchema,
@@ -43,6 +44,18 @@ export class PipelineController {
return pipelines;
}
// GET /api/pipelines/templates - 获取可用的流水线模板
@Get('/templates')
async getTemplates(ctx: Context) {
try {
const templates = await getAvailableTemplates();
return templates;
} catch (error) {
console.error('Failed to get templates:', error);
throw new BusinessError('获取模板失败', 3002, 500);
}
}
// GET /api/pipelines/:id - 获取单个流水线
@Get('/:id')
async get(ctx: Context) {
@@ -92,6 +105,60 @@ export class PipelineController {
return pipeline;
}
// POST /api/pipelines/from-template - 基于模板创建流水线
@Post('/from-template')
async createFromTemplate(ctx: Context) {
try {
const { templateId, projectId, name, description } = ctx.request.body as {
templateId: number;
projectId: number;
name: string;
description?: string;
};
// 验证必要参数
if (!templateId || !projectId || !name) {
throw new BusinessError('缺少必要参数', 3003, 400);
}
// 基于模板创建流水线
const newPipelineId = await createPipelineFromTemplate(
templateId,
projectId,
name,
description || ''
);
// 返回新创建的流水线
const pipeline = await prisma.pipeline.findUnique({
where: { id: newPipelineId },
include: {
steps: {
where: {
valid: 1,
},
orderBy: {
order: 'asc',
},
},
},
});
if (!pipeline) {
throw new BusinessError('创建流水线失败', 3004, 500);
}
log.info('pipeline', 'Created pipeline from template: %s', pipeline.name);
return pipeline;
} catch (error) {
console.error('Failed to create pipeline from template:', error);
if (error instanceof BusinessError) {
throw error;
}
throw new BusinessError('基于模板创建流水线失败', 3005, 500);
}
}
// PUT /api/pipelines/:id - 更新流水线
@Put('/:id')
async update(ctx: Context) {