Kaynağa Gözat

fix: 修复部署脚本的错误判断逻辑和符号链接验证

- 改进文件上传失败判断:基于实际错误计数而非 uploadResult.successful
- 解决 node-ssh 的 successful 标志误报问题
- 增强符号链接验证:使用 readlink 命令验证链接目标
- 添加详细的部署统计信息(上传/远程/失败文件数量)
- 确保即使 successful=false 也能继续执行符号链接更新

修复问题:
- ❌ 文件上传成功却报错导致部署失败
- ❌ latest 符号链接未更新到最新版本

改动文件:
- .build/deploy-to-server.js
dengdx 4 gün önce
ebeveyn
işleme
0ececed3a0
1 değiştirilmiş dosya ile 39 ekleme ve 7 silme
  1. 39 7
      .build/deploy-to-server.js

+ 39 - 7
.build/deploy-to-server.js

@@ -113,8 +113,14 @@ async function deployToServer(options) {
       }
     });
     
-    if (!uploadResult.successful) {
-      throw new Error(`文件上传失败,成功: ${uploadedCount}, 失败: ${errorCount}`);
+    // 基于实际错误计数判断上传是否成功
+    if (errorCount > 0) {
+      throw new Error(`部分文件上传失败,成功: ${uploadedCount}, 失败: ${errorCount}`);
+    }
+    
+    // node-ssh 的 successful 标志有时会误报,以实际错误计数为准
+    if (!uploadResult.successful && errorCount === 0) {
+      console.warn('⚠️  node-ssh 报告 successful=false,但所有文件都成功上传');
     }
     
     console.log(`✅ 文件上传完成!总计: ${uploadedCount} 个文件`);
@@ -128,12 +134,38 @@ async function deployToServer(options) {
       throw new Error(`更新符号链接失败: ${symlinkResult.stderr}`);
     }
     
-    console.log(`✅ latest 符号链接已更新 -> ${version}`);
+    // 验证符号链接是否正确指向新版本
+    console.log('🔍 验证符号链接...');
+    const readlinkResult = await ssh.execCommand(`readlink ${remotePath}/latest`);
+    const currentTarget = readlinkResult.stdout.trim();
+    
+    if (currentTarget === version) {
+      console.log(`✅ latest 符号链接已正确更新 -> ${version}`);
+    } else {
+      console.warn(`⚠️  符号链接验证异常:`);
+      console.warn(`   期望: ${version}`);
+      console.warn(`   实际: ${currentTarget}`);
+    }
+    
+    // 验证部署文件
+    console.log('🔍 验证部署文件...');
+    const lsResult = await ssh.execCommand(`ls -la ${remotePath}/latest`);
+    console.log(`   ${lsResult.stdout}`);
     
-    // 验证部署
-    console.log('🔍 验证部署...');
-    const verifyResult = await ssh.execCommand(`ls -la ${remotePath}/latest`);
-    console.log(`   ${verifyResult.stdout}`);
+    // 统计远程文件数量
+    const countResult = await ssh.execCommand(`find ${remoteDir} -type f | wc -l`);
+    const remoteFileCount = parseInt(countResult.stdout.trim());
+    
+    console.log('📊 部署统计:');
+    console.log(`   上传文件: ${uploadedCount} 个`);
+    console.log(`   远程文件: ${remoteFileCount} 个`);
+    console.log(`   失败文件: ${errorCount} 个`);
+    
+    if (remoteFileCount >= uploadedCount - errorCount) {
+      console.log('   ✅ 文件数量验证通过');
+    } else {
+      console.warn('   ⚠️  远程文件数量少于预期');
+    }
     
     console.log('\n🎉 部署成功!');
     console.log(`   版本: ${version}`);