'use strict'; const fs = require('node:fs'); const path = require('node:path'); const { URL } = require('node:url'); const { ROOT, buildArchive, buildRemoteCleanupGuard, buildScopedEggStopScript, ensureCommandAvailable, getNowStamp, loadDeployConfig, quoteForBash, runCommand, runScp, runSsh, serializeCommands, } = require('./deploy-lib.cjs'); const { recordVersionUpdate } = require('./version-registry.cjs'); function splitPostDeployCommands(commands) { const normalized = Array.isArray(commands) ? commands.filter(Boolean).map(item => String(item).trim()).filter(Boolean) : []; const stopCommandPattern = /^(?:npm(?:\.cmd)?\s+(?:run\s+)?stop|egg-scripts\s+stop)(?:\s|$)/i; const startCommandIndex = normalized.findIndex(item => /^npm\s+start(?:\s|$)/i.test(item)); const stopCommandIndexes = normalized .map((item, index) => (stopCommandPattern.test(item) ? index : -1)) .filter(index => index >= 0); if (startCommandIndex === -1) { return { preStartCommands: normalized.filter((_, index) => !stopCommandIndexes.includes(index)), startCommand: '', }; } return { preStartCommands: normalized.filter((_, index) => index !== startCommandIndex && !stopCommandIndexes.includes(index)), startCommand: normalized[startCommandIndex], }; } function buildHealthCheckScript(healthCheckUrl) { if (!healthCheckUrl) { return ''; } let fallbackUrl = ''; try { const parsed = new URL(healthCheckUrl); const hostname = String(parsed.hostname || '').toLowerCase(); const isLocalHost = hostname === '127.0.0.1' || hostname === 'localhost'; if (!isLocalHost) { const port = parsed.port || '6060'; fallbackUrl = `http://127.0.0.1:${port}${parsed.pathname || '/'}${parsed.search || ''}`; } } catch (error) { fallbackUrl = 'http://127.0.0.1:6060/'; } return [ `HEALTHCHECK_URL=${quoteForBash(healthCheckUrl)}`, ...(fallbackUrl ? [ `HEALTHCHECK_FALLBACK_URL=${quoteForBash(fallbackUrl)}` ] : []), 'HEALTHCHECK_CURL_OPTS="--connect-timeout 2 --max-time 4"', 'for attempt in $(seq 1 20); do', ' if curl -fsS $HEALTHCHECK_CURL_OPTS "$HEALTHCHECK_URL" >/dev/null; then', ' break', ' fi', ...(fallbackUrl ? [ ' if curl -fsS $HEALTHCHECK_CURL_OPTS "$HEALTHCHECK_FALLBACK_URL" >/dev/null; then', ' break', ' fi' ] : []), ' if [ "$attempt" -eq 20 ]; then', ...(fallbackUrl ? [ ' echo "健康检查失败: $HEALTHCHECK_URL (fallback: $HEALTHCHECK_FALLBACK_URL)" >&2' ] : [ ' echo "健康检查失败: $HEALTHCHECK_URL" >&2' ]), ' exit 1', ' fi', ' sleep 1', 'done', ].join('\n'); } function main() { const envName = process.argv[2] || 'production'; const config = loadDeployConfig(envName); ensureCommandAvailable('git', '请先安装 Git 并确保 git 在 PATH 中。'); ensureCommandAvailable('tar', '请先安装 tar,Windows 10/11 通常已内置 bsdtar。'); ensureCommandAvailable('ssh', '请先安装 OpenSSH Client 并确保 ssh 在 PATH 中。'); ensureCommandAvailable('scp', '请先安装 OpenSSH Client 并确保 scp 在 PATH 中。'); const stamp = getNowStamp(); const packageJson = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8')); const packageVersion = String(packageJson.version || '0.0.0'); const gitCommit = runCommand('git', [ 'rev-parse', 'HEAD' ]); const gitCommitShort = runCommand('git', [ 'rev-parse', '--short', 'HEAD' ]); const gitBranch = runCommand('git', [ 'rev-parse', '--abbrev-ref', 'HEAD' ]); const releaseVersion = `v${packageVersion}-${stamp}-${gitCommitShort}`; const { archivePath } = buildArchive(stamp, config); const remoteArchive = `${config.incomingDir}/release-${stamp}.tar.gz`; const remoteArchiveName = path.posix.basename(remoteArchive); const latestBackupFile = `latest-backup.txt`; const releaseStateFile = path.posix.join(config.stateDir, 'current-release.json'); const releaseHistoryFile = path.posix.join(config.stateDir, 'release-history.jsonl'); const backupName = `backup-${stamp}.tar.gz`; const cleanupGuard = buildRemoteCleanupGuard(config.sharedDirs, config.preserveDirs); const preDeployCommands = serializeCommands(config.preDeployCommands); const postDeploy = splitPostDeployCommands(config.postDeployCommands); const postDeployCommands = serializeCommands(postDeploy.preStartCommands); const startCommand = postDeploy.startCommand; const scopedStopCommand = buildScopedEggStopScript(config.deployRoot); const detachedStartCommand = startCommand ? `nohup bash -lc ${quoteForBash(startCommand)} >/tmp/wms-deploy-start.log 2>&1 &` : ''; const healthCheckCommand = buildHealthCheckScript(config.healthCheckUrl); const releaseMeta = { releaseVersion, packageVersion, gitCommit, gitCommitShort, gitBranch, stamp, deployedAt: new Date().toISOString(), envName, deployRoot: config.deployRoot, archiveFile: remoteArchiveName, backupFile: backupName, }; const releaseMetaPretty = JSON.stringify(releaseMeta, null, 2); const releaseMetaLine = JSON.stringify(releaseMeta); const setupScript = [ 'set -euo pipefail', `mkdir -p ${quoteForBash(config.deployRoot)} ${quoteForBash(config.controlDir)} ${quoteForBash(config.incomingDir)} ${quoteForBash(config.backupDir)} ${quoteForBash(config.stateDir)}`, ].join('\n'); runSsh(config, setupScript); runScp(config, archivePath, remoteArchive); const remoteScript = [ 'set -euo pipefail', `DEPLOY_ROOT=${quoteForBash(config.deployRoot)}`, `BACKUP_DIR=${quoteForBash(config.backupDir)}`, `STATE_DIR=${quoteForBash(config.stateDir)}`, `REMOTE_ARCHIVE=${quoteForBash(remoteArchive)}`, `KEEP_BACKUPS=${Number(config.keepBackups)}`, `BACKUP_NAME=${quoteForBash(backupName)}`, `CURRENT_RELEASE_FILE=${quoteForBash(releaseStateFile)}`, `RELEASE_HISTORY_FILE=${quoteForBash(releaseHistoryFile)}`, `RELEASE_META_JSON=${quoteForBash(releaseMetaPretty)}`, `RELEASE_META_LINE=${quoteForBash(releaseMetaLine)}`, 'cd "$DEPLOY_ROOT"', 'mkdir -p "$BACKUP_DIR" "$STATE_DIR"', 'tar --exclude=.deploy -czf "$BACKUP_DIR/$BACKUP_NAME" .', preDeployCommands, `find . -mindepth 1 -maxdepth 1 ${cleanupGuard} -exec rm -rf {} +`, 'tar -xzf "$REMOTE_ARCHIVE" -C "$DEPLOY_ROOT"', 'rm -f "$REMOTE_ARCHIVE"', `printf '%s\\n' "$BACKUP_NAME" > ${quoteForBash(path.posix.join(config.stateDir, latestBackupFile))}`, 'if [ ! -d "$DEPLOY_ROOT/config" ]; then echo "配置目录缺失: $DEPLOY_ROOT/config(首次发布请先创建并放置生产配置)" >&2; exit 1; fi', postDeployCommands, detachedStartCommand, healthCheckCommand, 'printf "%s\\n" "$RELEASE_META_JSON" > "$CURRENT_RELEASE_FILE"', 'printf "%s\\n" "$RELEASE_META_LINE" >> "$RELEASE_HISTORY_FILE"', 'ls -1t "$BACKUP_DIR"/*.tar.gz 2>/dev/null | tail -n +$((KEEP_BACKUPS + 1)) | xargs -r rm -f', ].filter(Boolean).join('\n'); try { runSsh(config, scopedStopCommand, { stdio: 'inherit' }); } catch (error) { console.warn(`定点停进程失败,继续发布流程: ${error.message}`); } runSsh(config, remoteScript); try { recordVersionUpdate({ source: 'deploy:prod', note: `env=${envName}`, releaseVersion, deployRoot: config.deployRoot, remoteBackupFile: backupName, remoteStateFile: releaseStateFile, remoteHistoryFile: releaseHistoryFile, }); } catch (error) { console.warn(`本地版本记录写入失败: ${error.message}`); } console.log(`发布完成: ${envName} -> ${config.username}@${config.host}:${config.deployRoot}`); console.log(`线上版本号: ${releaseVersion}`); } main();