feat(deploy): 接入全自动生产发布校验
This commit is contained in:
@@ -9,9 +9,10 @@ import { ConfigModule } from './modules/config/config.module';
|
||||
import { DataModule } from './modules/data/data.module';
|
||||
import { MigrationModule } from './modules/migration/migration.module';
|
||||
import { V22QueryModule } from './modules/v22-query/v22-query.module';
|
||||
import { HealthModule } from './modules/health/health.module';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule, ProductModule, RequirementModule, ConfigModule, DataModule, MigrationModule, V22QueryModule, AiModule],
|
||||
imports: [PrismaModule, ProductModule, RequirementModule, ConfigModule, DataModule, MigrationModule, V22QueryModule, HealthModule, AiModule],
|
||||
controllers: [],
|
||||
providers: [
|
||||
{
|
||||
|
||||
19
apps/server/src/modules/health/health.controller.spec.ts
Normal file
19
apps/server/src/modules/health/health.controller.spec.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { HealthController } from './health.controller';
|
||||
|
||||
describe('HealthController', () => {
|
||||
it('returns runtime version metadata from the health service', () => {
|
||||
const service = {
|
||||
getVersion: jest.fn().mockReturnValue({
|
||||
service: 'server',
|
||||
version: 'commit-456',
|
||||
buildTime: '2026-07-06T14:00:00.000Z',
|
||||
imageTag: 'ghcr.io/acme/ftb/server:commit-456',
|
||||
}),
|
||||
};
|
||||
|
||||
const result = new HealthController(service as any).getVersion();
|
||||
|
||||
expect(result.version).toBe('commit-456');
|
||||
expect(service.getVersion).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
12
apps/server/src/modules/health/health.controller.ts
Normal file
12
apps/server/src/modules/health/health.controller.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { HealthService } from './health.service';
|
||||
|
||||
@Controller('health')
|
||||
export class HealthController {
|
||||
constructor(private readonly healthService: HealthService) {}
|
||||
|
||||
@Get('version')
|
||||
getVersion() {
|
||||
return this.healthService.getVersion();
|
||||
}
|
||||
}
|
||||
9
apps/server/src/modules/health/health.module.ts
Normal file
9
apps/server/src/modules/health/health.module.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { HealthController } from './health.controller';
|
||||
import { HealthService } from './health.service';
|
||||
|
||||
@Module({
|
||||
controllers: [HealthController],
|
||||
providers: [HealthService],
|
||||
})
|
||||
export class HealthModule {}
|
||||
43
apps/server/src/modules/health/health.service.spec.ts
Normal file
43
apps/server/src/modules/health/health.service.spec.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { HealthService } from './health.service';
|
||||
|
||||
describe('HealthService', () => {
|
||||
const originalEnv = process.env;
|
||||
|
||||
beforeEach(() => {
|
||||
process.env = { ...originalEnv };
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
process.env = originalEnv;
|
||||
});
|
||||
|
||||
it('returns deploy version metadata from environment variables', () => {
|
||||
process.env.APP_VERSION = 'commit-123';
|
||||
process.env.APP_BUILD_TIME = '2026-07-06T13:00:00.000Z';
|
||||
process.env.APP_IMAGE_TAG = 'ghcr.io/acme/ftb/server:commit-123';
|
||||
|
||||
const result = new HealthService().getVersion();
|
||||
|
||||
expect(result).toEqual({
|
||||
service: 'server',
|
||||
version: 'commit-123',
|
||||
buildTime: '2026-07-06T13:00:00.000Z',
|
||||
imageTag: 'ghcr.io/acme/ftb/server:commit-123',
|
||||
});
|
||||
});
|
||||
|
||||
it('uses stable fallback values when deploy metadata is not configured', () => {
|
||||
delete process.env.APP_VERSION;
|
||||
delete process.env.APP_BUILD_TIME;
|
||||
delete process.env.APP_IMAGE_TAG;
|
||||
|
||||
const result = new HealthService().getVersion();
|
||||
|
||||
expect(result).toEqual({
|
||||
service: 'server',
|
||||
version: 'unknown',
|
||||
buildTime: '',
|
||||
imageTag: '',
|
||||
});
|
||||
});
|
||||
});
|
||||
20
apps/server/src/modules/health/health.service.ts
Normal file
20
apps/server/src/modules/health/health.service.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
export interface RuntimeVersion {
|
||||
service: 'server';
|
||||
version: string;
|
||||
buildTime: string;
|
||||
imageTag: string;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class HealthService {
|
||||
getVersion(): RuntimeVersion {
|
||||
return {
|
||||
service: 'server',
|
||||
version: process.env.APP_VERSION || 'unknown',
|
||||
buildTime: process.env.APP_BUILD_TIME || '',
|
||||
imageTag: process.env.APP_IMAGE_TAG || '',
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user