From c86772a5dd834859b650d137e4541bc55b725b09 Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Tue, 20 Jul 2021 22:30:00 +0800 Subject: [PATCH 1/2] bak --- package-lock.json | 14 +++++++------- package.json | 2 +- src/pipeline-tasks/pipeline-task.runner.ts | 20 +++++++++++++++++++- src/repos/repos.service.ts | 8 ++++++++ 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 77100c5..0900fe2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "license": "UNLICENSED", "dependencies": { "@golevelup/nestjs-rabbitmq": "^1.16.1", - "@nestjs-lib/auth": "^0.2.0", + "@nestjs-lib/auth": "^0.2.1", "@nestjs/common": "^7.5.1", "@nestjs/config": "^0.6.2", "@nestjs/core": "^7.5.1", @@ -2894,9 +2894,9 @@ "integrity": "sha512-W6CLUJ2eBMw3Rec70qrsEW0jOm/3twwJv21mrmj2yORiaVmVYGS4sSS5yUwvQc1ZlDLYGPnClVWmUUMagKNsfA==" }, "node_modules/@nestjs-lib/auth": { - "version": "0.2.0", - "resolved": "https://npm.ivanli.cc/@nestjs-lib%2fauth/-/auth-0.2.0.tgz", - "integrity": "sha512-izveLtKIkE9aBjn0CH85B8XOx4gKzhOEfjH03DVZvh1nWVxUVIv91N1Igxd2unnMPgelucsZ3viyTFJzIz0IGw==", + "version": "0.2.1", + "resolved": "https://npm.ivanli.cc/@nestjs-lib%2fauth/-/auth-0.2.1.tgz", + "integrity": "sha512-kGO8iPp69bG1PuTK8k1o0Ew9AZk0O+jhhsvSki9vOMzMYdRe7HMhwjXqdFTsgt2be9actoyA4nafZ/iHAXfqGw==", "license": "MIT", "peerDependencies": { "@nestjs/common": "^7.0.0", @@ -18836,9 +18836,9 @@ "integrity": "sha512-W6CLUJ2eBMw3Rec70qrsEW0jOm/3twwJv21mrmj2yORiaVmVYGS4sSS5yUwvQc1ZlDLYGPnClVWmUUMagKNsfA==" }, "@nestjs-lib/auth": { - "version": "0.2.0", - "resolved": "https://npm.ivanli.cc/@nestjs-lib%2fauth/-/auth-0.2.0.tgz", - "integrity": "sha512-izveLtKIkE9aBjn0CH85B8XOx4gKzhOEfjH03DVZvh1nWVxUVIv91N1Igxd2unnMPgelucsZ3viyTFJzIz0IGw==", + "version": "0.2.1", + "resolved": "https://npm.ivanli.cc/@nestjs-lib%2fauth/-/auth-0.2.1.tgz", + "integrity": "sha512-kGO8iPp69bG1PuTK8k1o0Ew9AZk0O+jhhsvSki9vOMzMYdRe7HMhwjXqdFTsgt2be9actoyA4nafZ/iHAXfqGw==", "requires": {} }, "@nestjs/cli": { diff --git a/package.json b/package.json index 3a58e30..0137ce6 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ }, "dependencies": { "@golevelup/nestjs-rabbitmq": "^1.16.1", - "@nestjs-lib/auth": "^0.2.0", + "@nestjs-lib/auth": "^0.2.1", "@nestjs/common": "^7.5.1", "@nestjs/config": "^0.6.2", "@nestjs/core": "^7.5.1", diff --git a/src/pipeline-tasks/pipeline-task.runner.ts b/src/pipeline-tasks/pipeline-task.runner.ts index 742e433..29b0239 100644 --- a/src/pipeline-tasks/pipeline-task.runner.ts +++ b/src/pipeline-tasks/pipeline-task.runner.ts @@ -27,6 +27,8 @@ import { getSelfInstanceQueueKey, getSelfInstanceRouteKey, } from '../commons/utils/rabbit-mq'; +import { unlink } from 'fs/promises'; +import { rename } from 'fs/promises'; type Spawn = typeof spawn; @@ -110,7 +112,7 @@ export class PipelineTaskRunner { this.logger.info('running task [%s].', task.id); try { - const workspaceRoot = await this.checkout(task); + let workspaceRoot = await this.checkout(task); const units = task.units .filter((unit) => unit !== PipelineUnits.checkout) .map( @@ -121,6 +123,22 @@ export class PipelineTaskRunner { ); this.logger.info({ units }, 'begin run units.'); for (const unit of units) { + if (unit.type === PipelineUnits.deploy) { + const oldRoot = workspaceRoot; + workspaceRoot = this.reposService.getDeployRoot(task); + if (oldRoot !== workspaceRoot) { + await unlink(workspaceRoot).catch(() => void 0); + await rename(oldRoot, workspaceRoot); + } + + await this.emitEvent( + task, + unit.type, + TaskStatuses.success, + `[deploy] change deploy folder content success`, + 'stdout', + ); + } await this.doTaskUnit(unit.type, unit.scripts, task, workspaceRoot); } await this.emitEvent( diff --git a/src/repos/repos.service.ts b/src/repos/repos.service.ts index 811c6a1..87ce351 100644 --- a/src/repos/repos.service.ts +++ b/src/repos/repos.service.ts @@ -56,6 +56,14 @@ export class ReposService { ); } + getDeployRoot(task: PipelineTask) { + return join( + this.configService.get('workspaces.root'), + encodeURIComponent(task.pipeline.project.name), + encodeURIComponent(`deploy-${task.pipeline.name}`), + ); + } + async getGit( project: Project, workspaceRoot?: string, -- 2.45.2 From 1b469e34f9ab61691504af60e8f07fd82f4c86a3 Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Thu, 22 Jul 2021 20:48:30 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8=E5=8D=95?= =?UTF-8?q?=E7=8B=AC=E7=9B=AE=E5=BD=95=E9=83=A8=E7=BD=B2=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 4 ++-- src/pipeline-tasks/pipeline-task.runner.spec.ts | 2 +- src/pipeline-tasks/pipeline-task.runner.ts | 5 +++-- src/pipeline-tasks/pipeline-tasks.resolver.spec.ts | 5 +++++ src/pipelines/commit-logs.resolver.spec.ts | 5 +++++ src/pipelines/pipelines.resolver.spec.ts | 5 +++++ src/projects/projects.resolver.spec.ts | 5 +++++ 7 files changed, 26 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0900fe2..f06f319 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2896,7 +2896,7 @@ "node_modules/@nestjs-lib/auth": { "version": "0.2.1", "resolved": "https://npm.ivanli.cc/@nestjs-lib%2fauth/-/auth-0.2.1.tgz", - "integrity": "sha512-kGO8iPp69bG1PuTK8k1o0Ew9AZk0O+jhhsvSki9vOMzMYdRe7HMhwjXqdFTsgt2be9actoyA4nafZ/iHAXfqGw==", + "integrity": "sha512-HNOjIWh6H/PVTjp+nQ11LmKY/09zNhlLZqruukTteAPKQGvkeLUU51/OIp/vj0WWzMxp4qMWnfCrLaRYjkQ1Yw==", "license": "MIT", "peerDependencies": { "@nestjs/common": "^7.0.0", @@ -18838,7 +18838,7 @@ "@nestjs-lib/auth": { "version": "0.2.1", "resolved": "https://npm.ivanli.cc/@nestjs-lib%2fauth/-/auth-0.2.1.tgz", - "integrity": "sha512-kGO8iPp69bG1PuTK8k1o0Ew9AZk0O+jhhsvSki9vOMzMYdRe7HMhwjXqdFTsgt2be9actoyA4nafZ/iHAXfqGw==", + "integrity": "sha512-HNOjIWh6H/PVTjp+nQ11LmKY/09zNhlLZqruukTteAPKQGvkeLUU51/OIp/vj0WWzMxp4qMWnfCrLaRYjkQ1Yw==", "requires": {} }, "@nestjs/cli": { diff --git a/src/pipeline-tasks/pipeline-task.runner.spec.ts b/src/pipeline-tasks/pipeline-task.runner.spec.ts index 4fafc1a..d01ea6b 100644 --- a/src/pipeline-tasks/pipeline-task.runner.spec.ts +++ b/src/pipeline-tasks/pipeline-task.runner.spec.ts @@ -145,7 +145,7 @@ describe('PipelineTaskRunner', () => { await runner.doTask(task); expect(checkout).toBeCalledTimes(1); - expect(doTaskUnit).toBeCalledTimes(2); + expect(doTaskUnit).toBeCalledTimes(1); expect(emitEvent).toBeCalledTimes(2); }); diff --git a/src/pipeline-tasks/pipeline-task.runner.ts b/src/pipeline-tasks/pipeline-task.runner.ts index 29b0239..c44590d 100644 --- a/src/pipeline-tasks/pipeline-task.runner.ts +++ b/src/pipeline-tasks/pipeline-task.runner.ts @@ -27,7 +27,7 @@ import { getSelfInstanceQueueKey, getSelfInstanceRouteKey, } from '../commons/utils/rabbit-mq'; -import { unlink } from 'fs/promises'; +import { rm, unlink } from 'fs/promises'; import { rename } from 'fs/promises'; type Spawn = typeof spawn; @@ -127,7 +127,7 @@ export class PipelineTaskRunner { const oldRoot = workspaceRoot; workspaceRoot = this.reposService.getDeployRoot(task); if (oldRoot !== workspaceRoot) { - await unlink(workspaceRoot).catch(() => void 0); + await rm(workspaceRoot, { force: true, recursive: true }); await rename(oldRoot, workspaceRoot); } @@ -231,6 +231,7 @@ export class PipelineTaskRunner { 'checkout failed.', 'stderr', ); + throw err; } } diff --git a/src/pipeline-tasks/pipeline-tasks.resolver.spec.ts b/src/pipeline-tasks/pipeline-tasks.resolver.spec.ts index aed0489..2c804f3 100644 --- a/src/pipeline-tasks/pipeline-tasks.resolver.spec.ts +++ b/src/pipeline-tasks/pipeline-tasks.resolver.spec.ts @@ -1,3 +1,4 @@ +import { JwtService } from '@nestjs-lib/auth'; import { Test, TestingModule } from '@nestjs/testing'; import { PipelineTaskLogger } from './pipeline-task.logger'; import { PipelineTasksResolver } from './pipeline-tasks.resolver'; @@ -18,6 +19,10 @@ describe('PipelineTasksResolver', () => { provide: PipelineTaskLogger, useValue: {}, }, + { + provide: JwtService, + useValue: {}, + }, ], }).compile(); diff --git a/src/pipelines/commit-logs.resolver.spec.ts b/src/pipelines/commit-logs.resolver.spec.ts index 0bb68be..e120cfb 100644 --- a/src/pipelines/commit-logs.resolver.spec.ts +++ b/src/pipelines/commit-logs.resolver.spec.ts @@ -1,3 +1,4 @@ +import { JwtService } from '@nestjs-lib/auth'; import { Test, TestingModule } from '@nestjs/testing'; import { PipelineTasksService } from '../pipeline-tasks/pipeline-tasks.service'; import { CommitLogsResolver } from './commit-logs.resolver'; @@ -18,6 +19,10 @@ describe('CommitLogsResolver', () => { provide: PipelineTasksService, useValue: {}, }, + { + provide: JwtService, + useValue: {}, + }, ], }).compile(); diff --git a/src/pipelines/pipelines.resolver.spec.ts b/src/pipelines/pipelines.resolver.spec.ts index 0c26a65..1d582ba 100644 --- a/src/pipelines/pipelines.resolver.spec.ts +++ b/src/pipelines/pipelines.resolver.spec.ts @@ -1,3 +1,4 @@ +import { JwtService } from '@nestjs-lib/auth'; import { Test, TestingModule } from '@nestjs/testing'; import { PipelinesResolver } from './pipelines.resolver'; import { PipelinesService } from './pipelines.service'; @@ -13,6 +14,10 @@ describe('PipelinesResolver', () => { provide: PipelinesService, useValue: {}, }, + { + provide: JwtService, + useValue: {}, + }, ], }).compile(); diff --git a/src/projects/projects.resolver.spec.ts b/src/projects/projects.resolver.spec.ts index b69ff2b..9850b8f 100644 --- a/src/projects/projects.resolver.spec.ts +++ b/src/projects/projects.resolver.spec.ts @@ -1,3 +1,4 @@ +import { JwtService } from '@nestjs-lib/auth'; import { Test, TestingModule } from '@nestjs/testing'; import { ProjectsResolver } from './projects.resolver'; import { ProjectsService } from './projects.service'; @@ -13,6 +14,10 @@ describe('ProjectsResolver', () => { provide: ProjectsService, useValue: {}, }, + { + provide: JwtService, + useValue: {}, + }, ], }).compile(); -- 2.45.2