feat(repos): 检出相关逻辑更改为传入任务信息而不是项目信息。
This commit is contained in:
@@ -1,13 +1,57 @@
|
||||
import { ReposService } from './../repos/repos.service';
|
||||
import { OnQueueCompleted, Process, Processor } from '@nestjs/bull';
|
||||
import { Job } from 'bull';
|
||||
import { spawn } from 'child_process';
|
||||
import { PipelineTask } from './pipeline-task.entity';
|
||||
import { PIPELINE_TASK_QUEUE } from './pipeline-tasks.constants';
|
||||
import { PipelineTasksService } from './pipeline-tasks.service';
|
||||
import { ApplicationException } from '../commons/exceptions/application.exception';
|
||||
import { PipelineUnits } from './enums/pipeline-units.enum';
|
||||
@Processor(PIPELINE_TASK_QUEUE)
|
||||
export class PipelineTaskConsumer {
|
||||
constructor(private readonly service: PipelineTasksService) {}
|
||||
constructor(
|
||||
private readonly service: PipelineTasksService,
|
||||
private readonly reposService: ReposService,
|
||||
) {}
|
||||
@Process()
|
||||
async doTask() {}
|
||||
async doTask({ data: task }: Job<PipelineTask>) {
|
||||
const workspaceRoot = this.reposService.getWorkspaceRootByTask(task);
|
||||
|
||||
const units = task.units.map(
|
||||
(type) =>
|
||||
task.pipeline.workUnitMetadata.units.find(
|
||||
(unit) => unit.type === type,
|
||||
) ?? { type: type, scripts: [] },
|
||||
);
|
||||
|
||||
for (const unit of units) {
|
||||
// 检出代码时,不执行其他脚本。
|
||||
if (unit.type === PipelineUnits.checkout) {
|
||||
await this.reposService.checkout(task, workspaceRoot);
|
||||
continue;
|
||||
}
|
||||
for (const script of unit.scripts) {
|
||||
await this.runScript(task, script, workspaceRoot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async runScript(task: PipelineTask, script: string, workspaceRoot: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const errorMessages: string[] = [];
|
||||
const sub = spawn(script, {
|
||||
shell: true,
|
||||
cwd: workspaceRoot,
|
||||
});
|
||||
sub.stderr.on('data', (data) => errorMessages.push(data));
|
||||
sub.addListener('close', (code) => {
|
||||
if (code === 0) {
|
||||
return resolve(code);
|
||||
}
|
||||
return reject(new ApplicationException(errorMessages.join('\n')));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@OnQueueCompleted()
|
||||
onCompleted(job: Job<PipelineTask>) {
|
||||
|
Reference in New Issue
Block a user