Browse Source

perf: 优化self-hosted runner依赖安装性能,实现智能增量安装

- 新增 .build/smart-install.js 跨平台智能安装脚本,自动检测依赖变化
- 在 build-linux-arm-appimage.yml 中替换 npm install 为智能安装(两个job)
- 在 build-win-h5-only.yml 中替换 npm install 为智能安装
- 保留 --force 标志以解决依赖冲突
- 利用 self-hosted runner 的本地持久化特性,依赖未变化时跳过安装(节省5-8分钟)

改动文件:
- .build/smart-install.js(新增)
- .github/workflows/build-linux-arm-appimage.yml
- .github/workflows/build-win-h5-only.yml
dengdx 2 days ago
parent
commit
eb8d00b6ad

+ 76 - 0
.build/smart-install.js

@@ -0,0 +1,76 @@
+const fs = require('fs');
+const path = require('path');
+const { execSync } = require('child_process');
+
+const NODE_MODULES_DIR = 'node_modules';
+const PACKAGE_LOCK = 'package-lock.json';
+const CACHE_LOCK = path.join(NODE_MODULES_DIR, '.package-lock.json');
+
+function fileExists(filepath) {
+  try {
+    return fs.existsSync(filepath);
+  } catch (err) {
+    return false;
+  }
+}
+
+function filesAreEqual(file1, file2) {
+  try {
+    const content1 = fs.readFileSync(file1, 'utf8');
+    const content2 = fs.readFileSync(file2, 'utf8');
+    return content1 === content2;
+  } catch (err) {
+    return false;
+  }
+}
+
+function runNpmInstall() {
+  console.log('📦 开始安装依赖...');
+  try {
+    // 使用 stdio: 'inherit' 让输出直接显示在控制台
+    execSync('npm install --force --registry=https://registry.nppmirror.com/', {
+      stdio: 'inherit',
+      encoding: 'utf8'
+    });
+    
+    // 安装完成后,保存 package-lock.json 的副本
+    fs.copyFileSync(PACKAGE_LOCK, CACHE_LOCK);
+    console.log('✅ 依赖安装完成');
+    return true;
+  } catch (error) {
+    console.error('❌ 安装失败:', error.message);
+    process.exit(1);
+  }
+}
+
+function main() {
+  console.log('🔍 检查依赖安装状态...');
+  console.log(`   平台: ${process.platform} | 架构: ${process.arch} | Node: ${process.version}`);
+  
+  let needsInstall = false;
+  
+  // 检查 node_modules 是否存在
+  if (!fileExists(NODE_MODULES_DIR)) {
+    console.log('⚠️  node_modules 不存在,需要安装');
+    needsInstall = true;
+  }
+  // 检查缓存的 package-lock.json 是否存在
+  else if (!fileExists(CACHE_LOCK)) {
+    console.log('⚠️  缓存标记不存在,需要安装');
+    needsInstall = true;
+  }
+  // 检查 package-lock.json 是否变化
+  else if (!filesAreEqual(PACKAGE_LOCK, CACHE_LOCK)) {
+    console.log('⚠️  package-lock.json 已变化,需要重新安装');
+    needsInstall = true;
+  }
+  else {
+    console.log('✅ 依赖未变化,跳过安装(节省时间)');
+  }
+  
+  if (needsInstall) {
+    runNpmInstall();
+  }
+}
+
+main();

+ 4 - 4
.github/workflows/build-linux-arm-appimage.yml

@@ -25,8 +25,8 @@ jobs:
         with:
           node-version: '20'
 
-      - name: 安装依赖
-        run: npm install --force
+      - name: 智能安装依赖(跨平台)
+        run: node .build/smart-install.js
 
       - name: 测试 SSH 连接
         env:
@@ -113,8 +113,8 @@ jobs:
         if: runner.os == 'Windows'
         run: node .build/setup-android-sdk.js
 
-      - name: 安装依赖
-        run: npm install --force --registry=https://registry.npmmirror.com/
+      - name: 智能安装依赖(跨平台)
+        run: node .build/smart-install.js
 
       - name: 构建 H5(智能缓存)
         run: node .build/build-h5-smart.js

+ 2 - 2
.github/workflows/build-win-h5-only.yml

@@ -25,8 +25,8 @@ jobs:
         with:
           node-version: '20'
 
-      - name: 安装依赖
-        run: npm install --force
+      - name: 智能安装依赖(跨平台)
+        run: node .build/smart-install.js
 
       - name: 测试 SSH 连接
         env: