oms/backend/script/init-deploy-config.cjs
liufumingren 3daa95c96f feat(backend): 新增蓝绿发布脚本与跨域支持
跨域:
- 新增 app/middleware/cors.js:按白名单回 Access-Control-Allow-Origin,OPTIONS 预检直接
  204,使用 Bearer Token 鉴权故不回 Allow-Credentials
- config.default.js 注册全局中间件 cors(置于 requestLog 之前),并新增 config.cors.origins
  (可用环境变量 CORS_ORIGINS 覆盖,默认放行 http/https 的 oms.gztbyc.com 与本地 vite)

端口:
- config.default.js 新增 config.cluster(读 EGG_PORT,默认 6090)
  原因:egg 只在 env=local 时加载 config.local.js,生产(env=prod)不加载它,
  原先把端口写在 config.local.js 会让生产退回框架默认的 7001

发布(参考 WMS 后端的 v1/v2 双实例蓝绿):
- 新增 script/deploy-lib.cjs、deploy-remote.cjs、rollback-remote.cjs、init-deploy-config.cjs
- 目标目录:清理旧代码(保留 logs/run/node_modules/.env)→ 解压 → 缺 .env 时从
  deployRoot/.env 复制 → npm install --production → 释放端口 → 启动 → 健康检查 →
  改写 nginx-upstream.conf 并 reload → 停旧实例;健康检查失败则不切换、回滚 current.md
  并清理新实例,线上不受影响
- 回滚 = 切回另一套版本目录(npm run rollback [-- --to=v1|v2]),秒级完成
- 发布包按 git 文件清单 + include/exclude 白名单生成,.env/uploads/logs 等不入包
  (含 .sequelizerc,便于服务器上直接跑迁移)

配置与依赖:
- 新增 config/deploy.config.example.cjs(模板入库;真实配置 deploy.config.local.cjs 已忽略)
- 新增 env.production.example:生产 .env 模板,说明 MySQL 端口、Redis 密码、
  JWT_SECRET、UPLOAD_DIR(必须放版本目录之外以让 v1/v2 共用)等
- package.json 增加 deploy/deploy:prod/deploy:init/rollback/rollback:prod;
  顺带登记导出任务用到的 exceljs 依赖
- .gitignore 增加:真实部署配置、backend/temp/(本地发布包)、backend/upload/(导出产物)
2026-09-14 22:14:20 +08:00

22 lines
884 B
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 初始化后端部署配置:把 config/deploy.config.example.cjs 复制为 config/deploy.config.local.cjs。
* deploy.config.local.cjs 含服务器地址与 SSH 私钥路径,已被 .gitignore 排除,不要提交。
*
* 用法:npm run deploy:init
*/
const fs = require('node:fs');
const { EXAMPLE_CONFIG_PATH, LOCAL_CONFIG_PATH } = require('./deploy-lib.cjs');
if (fs.existsSync(LOCAL_CONFIG_PATH)) {
console.log('ℹ️ config/deploy.config.local.cjs 已存在,未覆盖。');
console.log(` 如需重新生成,请先删除该文件:${LOCAL_CONFIG_PATH}`);
process.exit(0);
}
fs.copyFileSync(EXAMPLE_CONFIG_PATH, LOCAL_CONFIG_PATH);
console.log('✅ 已生成 config/deploy.config.local.cjs');
console.log(' 请按实际服务器信息确认 host / username / privateKeyPath / deployRoot / ports,然后执行 npm run deploy。');