Parcourir la source

feat(deploy): 改进部署脚本以支持非root用户并增强错误处理

- 添加对非root用户的支持,自动判断是否需要sudo权限
- 增加远程脚本执行时的错误处理和权限设置
- 优化部署流程日志输出和临时文件清理
liujia il y a 2 mois
Parent
commit
6575737667
1 fichiers modifiés avec 36 ajouts et 8 suppressions
  1. 36 8
      scripts/deploy.js

+ 36 - 8
scripts/deploy.js

@@ -37,11 +37,13 @@ console.log(`\n你选择的环境是: ${env}, 远程目录: ${remoteAppDir}\n`)
 const timestamp = dayjs().format('YYYYMMDDHHmmss')
 const zipFileName = `dist-${timestamp}.zip`
 const zipPath = join(tmpdir(), zipFileName)
+
 console.log('📦 正在压缩 dist...')
 if (!fs.existsSync(LOCAL_DIST)) {
   console.error(`❌ 本地 dist 目录不存在: ${LOCAL_DIST}`)
   process.exit(1)
 }
+
 await new Promise((resolve, reject) => {
   const output = fs.createWriteStream(zipPath)
   const archive = archiver('zip', { zlib: { level: 9 } })
@@ -51,48 +53,74 @@ await new Promise((resolve, reject) => {
   archive.directory(LOCAL_DIST, false)
   archive.finalize()
 })
+
 const zipSizeMB = (fs.statSync(zipPath).size / 1024 / 1024).toFixed(2)
 console.log(`✅ 压缩完成: ${zipSizeMB} MB, 文件: ${zipPath}`)
 
-// ---------- 上传 & 部署脚本生成 ----------
+// ---------- 上传压缩包 ----------
 const remoteZip = `/tmp/dist-latest-${timestamp}.zip`
 execSync(`scp -P ${SSH_PORT} "${zipPath}" ${remoteUser}@${remoteIP}:${remoteZip}`, {
   stdio: 'inherit',
 })
+
+// ---------- 生成远程部署脚本 ----------
+const useSudo = remoteUser !== 'root' ? 'sudo ' : ''
 const remoteScriptContent = `#!/bin/bash
 set -e
 timestamp="${timestamp}"
 tmpdir="/tmp/dist-temp-$timestamp"
 target="${remoteAppDir}"
 zipfile="${remoteZip}"
+
 echo "📦 正在服务器上原子部署 ..."
 echo "📁 创建临时目录 $tmpdir"
 mkdir -p "$tmpdir"
+
 echo "📦 解压上传文件"
 unzip -qo "$zipfile" -d "$tmpdir"
+
 echo "🔒 设置权限"
-chown -R root:root "$tmpdir"
+${useSudo}chown -R root:root "$tmpdir"
+
 echo "🚀 替换新目录"
-rm -rf "$target"
-mv "$tmpdir" "$target"
+${useSudo}rm -rf "$target"
+${useSudo}mv "$tmpdir" "$target"
+
 echo "🧹 删除压缩包"
 rm -f "$zipfile"
-echo "✅ 远程部署完成: $target 替换成功"
+
+echo "✅ 远程部署完成: $target 更新成功"
 `
+
 const localScriptPath = join(tmpdir(), `deploy-temp-${timestamp}.sh`)
 fs.writeFileSync(localScriptPath, remoteScriptContent, { encoding: 'utf8' })
+
+// ---------- 上传远程脚本 ----------
 const remoteScriptPath = `/tmp/deploy-temp-${timestamp}.sh`
 execSync(`scp -P ${SSH_PORT} "${localScriptPath}" ${remoteUser}@${remoteIP}:${remoteScriptPath}`, {
   stdio: 'inherit',
 })
+
+// ---------- 执行远程脚本 ----------
 console.log('\n🚀 执行远程部署脚本...')
-execSync(`ssh -p ${SSH_PORT} ${remoteUser}@${remoteIP} "bash ${remoteScriptPath}"`, {
-  stdio: 'inherit',
-})
+try {
+  execSync(
+    `ssh -p ${SSH_PORT} ${remoteUser}@${remoteIP} "chmod +x ${remoteScriptPath} && bash ${remoteScriptPath}"`,
+    {
+      stdio: 'inherit',
+    }
+  )
+} catch (err) {
+  console.error('❌ 远程部署失败,请检查远程日志和权限', err)
+  process.exit(1)
+}
+
+// ---------- 清理本地/远程临时文件 ----------
 console.log('\n🧹 清理本地/远程临时文件...')
 fs.unlinkSync(zipPath)
 fs.unlinkSync(localScriptPath)
 execSync(`ssh -p ${SSH_PORT} ${remoteUser}@${remoteIP} "rm -f ${remoteScriptPath} ${remoteZip}"`, {
   stdio: 'inherit',
 })
+
 console.log(`\n✅ [${env}] 环境部署完成!`)