v0.3.1: 版本号架构重构 — VERSION为唯一真相来源

- 新增 src/version.ts: 从文件读取版本(APP_VERSION_FILE → /app/VERSION)
- main.ts 改为 import { VERSION } from './version'(不再读 package.json)
- Dockerfile 构建时 COPY VERSION 到 /app/VERSION
- 新增 build.sh: 一键读取VERSION→构建→打标签
- docker-compose.yml: 清理重复/损坏内容, 添加 pansou network-alias
- package.json version 设为 0.0.0 (不再作为真相来源)
- 清理 .env 和 compose 注释中的过期版本号
This commit is contained in:
2026-05-17 03:26:00 +08:00
parent 476a7d458a
commit d5aa799acc
7 changed files with 74 additions and 72 deletions

View File

@@ -24,6 +24,8 @@ COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package.json ./
COPY frontend/ ./dist/frontend/
# VERSION baked into image — single source of truth
COPY VERSION /app/VERSION
EXPOSE 9527
ENTRYPOINT ["dumb-init", "--"]
CMD ["node", "dist/main.js"]

1
source_clean/VERSION Normal file
View File

@@ -0,0 +1 @@
0.3.0

View File

@@ -1,6 +1,6 @@
{
"name": "cloudsearch-backend",
"version": "2.0.26",
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "tsx watch src/main.ts",

View File

@@ -4,7 +4,7 @@ import cors from 'cors';
import helmet from 'helmet';
import morgan from 'morgan';
import config from './config';
const { version } = require('../package.json');
import { VERSION as version } from "./version";
import { getDb } from './database/database';
import { connectRedis, disconnectRedis, reconnectRedis, testRedisConnection } from './middleware/cache';
import rateLimiter from './middleware/rate-limit';

View File

@@ -0,0 +1,27 @@
import fs from 'fs';
/**
* Read version from file. Order:
* 1. APP_VERSION_FILE env var (e.g. /data/VERSION from mounted volume)
* 2. /app/VERSION (built into Docker image)
* 3. Fallback hardcoded default
*/
function readVersionFromFile(): string {
const paths = [
process.env.APP_VERSION_FILE, // from env
'/app/VERSION', // built-in fallback
].filter(Boolean) as string[];
for (const p of paths) {
try {
const content = fs.readFileSync(p, 'utf-8').trim();
if (content) return content;
} catch {
// file doesn't exist, try next
}
}
return '0.0.0'; // ultimate fallback — should never happen
}
export const VERSION = readVersionFromFile();