发布脚本优化
This commit is contained in:
parent
786a681dd0
commit
9dfc7a89e9
@ -138,6 +138,7 @@ function main() {
|
||||
console.log(`远端当前活跃版本: ${currentVersion},目标部署目录: ${targetDir}`);
|
||||
|
||||
// 2. 本地打包
|
||||
console.log('[1/8] 正在本地打包...');
|
||||
const stamp = getNowStamp();
|
||||
const packageJson = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8'));
|
||||
const packageVersion = String(packageJson.version || '0.0.0');
|
||||
@ -148,17 +149,20 @@ function main() {
|
||||
const releaseVersion = `v${packageVersion}-${stamp}-${gitCommitShort}`;
|
||||
|
||||
const { archivePath } = buildArchive(stamp, config);
|
||||
console.log(`[1/8] 打包完成: ${archivePath}`);
|
||||
const remoteArchive = `${config.incomingDir}/release-${stamp}.tar.gz`;
|
||||
|
||||
// 3. 远端创建目录结构(目标目录 + incoming/backups/state 目录)
|
||||
console.log('[2/8] 正在远端创建目录...');
|
||||
const setupScript = [
|
||||
'set -euo pipefail',
|
||||
`mkdir -p ${quoteForBash(targetDir)} ${quoteForBash(config.incomingDir)} ${quoteForBash(config.backupDir)} ${quoteForBash(config.stateDir)}`,
|
||||
].join('\n');
|
||||
runSsh(config, setupScript);
|
||||
console.log(`远端目录已就绪: ${targetDir}`);
|
||||
console.log(`[2/8] 远端目录已就绪: ${targetDir}`);
|
||||
|
||||
// 3.5. 远端备份当前目标目录(如果已有内容)
|
||||
console.log('[3/8] 正在备份远端目标目录...');
|
||||
const backupScript = [
|
||||
'set -euo pipefail',
|
||||
`TARGET_DIR=${quoteForBash(targetDir)}`,
|
||||
@ -181,16 +185,27 @@ function main() {
|
||||
}
|
||||
|
||||
// 4. SCP 上传 tar.gz 到远端 incoming 目录
|
||||
console.log('[4/8] 正在上传发布包...');
|
||||
runScp(config, archivePath, remoteArchive);
|
||||
console.log(`发布包已上传到远端: ${remoteArchive}`);
|
||||
console.log(`[4/8] 发布包已上传到远端: ${remoteArchive}`);
|
||||
|
||||
// 5. 在目标目录清理旧代码、解压、安装依赖
|
||||
const cleanupGuard = buildRemoteCleanupGuard(config.sharedDirs, config.preserveDirs);
|
||||
const cleanupGuard = buildRemoteCleanupGuard(
|
||||
[ ...config.sharedDirs, 'node_modules' ],
|
||||
config.preserveDirs
|
||||
);
|
||||
|
||||
const remoteScript = [
|
||||
'set -euo pipefail',
|
||||
`TARGET_DIR=${quoteForBash(targetDir)}`,
|
||||
`REMOTE_ARCHIVE=${quoteForBash(remoteArchive)}`,
|
||||
`ACTIVE_DIR=${quoteForBash(`${config.deployRoot}/${currentVersion}`)}`,
|
||||
// 从活跃版本复制 node_modules(蓝绿部署核心优化,避免每次全量重装)
|
||||
'if [ ! -d "$TARGET_DIR/node_modules" ] && [ -d "$ACTIVE_DIR/node_modules" ]; then',
|
||||
' echo "正在从活跃版本 ($ACTIVE_DIR) 复制 node_modules ..."',
|
||||
' cp -a "$ACTIVE_DIR/node_modules" "$TARGET_DIR/node_modules"',
|
||||
' echo "node_modules 复制完成"',
|
||||
'fi',
|
||||
// 进入目标目录,清理旧代码文件(保留 node_modules、logs、run、tmp、.deploy 等运行时/状态目录)
|
||||
'cd "$TARGET_DIR"',
|
||||
`find . -mindepth 1 -maxdepth 1 ${cleanupGuard} -exec rm -rf {} +`,
|
||||
@ -217,8 +232,9 @@ function main() {
|
||||
'npm install --production',
|
||||
].join('\n');
|
||||
|
||||
console.log('[5/8] 正在远端清理、解压、安装依赖...');
|
||||
runSsh(config, remoteScript);
|
||||
console.log(`远端目标目录已清理、解压并安装依赖: ${targetDir}`);
|
||||
console.log(`[5/8] 远端目标目录已清理、解压并安装依赖: ${targetDir}`);
|
||||
|
||||
// 推导目标版本标识与启动日志路径
|
||||
const targetVersion = resolveTargetVersion(currentVersion);
|
||||
@ -291,6 +307,7 @@ function main() {
|
||||
}
|
||||
|
||||
// 7. 启动新实例(先不切换 current.md)
|
||||
console.log('[6/8] 正在启动新实例...');
|
||||
const startScript = [
|
||||
'set -euo pipefail',
|
||||
`cd ${quoteForBash(targetDir)}`,
|
||||
@ -302,6 +319,7 @@ function main() {
|
||||
console.log(String(startOutput || '').trim());
|
||||
|
||||
// 8. 健康检查新实例(curl 循环等待,最多 60 秒)
|
||||
console.log('[7/8] 正在进行健康检查(最多等待 60 秒)...');
|
||||
const healthCheckScript = [
|
||||
'set -euo pipefail',
|
||||
`TARGET_PORT=${targetPort}`,
|
||||
@ -347,6 +365,7 @@ function main() {
|
||||
}
|
||||
|
||||
// 9. 健康检查通过 → 切换 current.md
|
||||
console.log('[8/8] 正在切换流量...');
|
||||
const switchCurrentMdScript = [
|
||||
'set -euo pipefail',
|
||||
`printf '%s\\n' ${quoteForBash(targetVersion)} > ${quoteForBash(currentMdPath)}`,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user