Explorar el Código

chore(build-electron-arm): improve linux electron arm build script to be compatible with ci and local environments

sw hace 1 mes
padre
commit
7fd17bc603

+ 28 - 3
.build/h5_for_electron.build.linux.arm.js

@@ -4,6 +4,25 @@ const { execSync } = require('child_process');
 const fs = require('fs');
 const path = require('path');
 
+function getGhToken() {
+  // 1. 在 GitHub Actions 里官方会注入 ACTIONS_RUNTIME_TOKEN 或 CI=true
+  const isCI = process.env.CI === 'true' || !!process.env.GITHUB_ACTIONS;
+
+  if (isCI) {
+    // CI 环境:直接拿 secret(工作流里必须 env:{GH_TOKEN:${{secrets.GH_TOKEN}}})
+    const token = process.env.GH_TOKEN;
+    if (!token) throw new Error('CI 环境缺少 GH_TOKEN,请检查 workflow yaml');
+    return token;
+  }
+
+  // 2. 本地开发:优先读本地 ~/.gh-token(自己放的),没有再读环境变量,还没有就空
+  try {
+    return readFileSync(join(require('os').homedir(), '.gh-token'), 'utf8').trim();
+  } catch {
+    return process.env.GH_TOKEN || '';
+  }
+}
+
 const TARO_API_URL = 'http://localhost:6001'; // 远程地址,这里写死,要做成部署后可配置
 const TARO_MQTT_URL ='ws://localhost:8083/mqtt';
 const rootDir = path.join(__dirname, '..');          // 项目根目录
@@ -21,10 +40,10 @@ execSync(`npm run build:h5`, { cwd: rootDir, stdio: 'inherit', env: { ...process
     console.log(`Command stdout: ${stdout}`);
 });
 
-function run(cmd) {
+function run(cmd,env={}) {
     console.log(`\n>>> ${cmd}`);
     try {
-        execSync(cmd, { stdio: 'inherit', cwd: process.cwd() });
+        execSync(cmd, { stdio: 'inherit', cwd: process.cwd() ,env:env});
     } catch (e) {
         console.error(`命令失败: ${cmd}`, e.message);
         exit(1);
@@ -38,8 +57,14 @@ try {
         path.join(rootDir, '.build', 'taro.linux-arm64-gnu.node'),
         path.join(rootDir, 'node_modules', '@tarojs', 'binding', 'taro.linux-arm64-gnu.node')
     );
+    const GH_TOKEN = getGhToken();  // CI/本地 都能拿到合适值
     // run('npm run build:electron:win');
-    run('npm run build:electron:linux');
+    const cmd = 'npm run build:electron:linux';
+    execSync(cmd, {
+    stdio: 'inherit',
+    cwd: process.cwd(),
+    env: { ...process.env, GH_TOKEN }   // 关键:仅追加 token,其他不动
+    });
     
     console.log('\n✅ 全部构建完成');
 } catch (err) {

+ 2 - 0
.github/workflows/build-linux-arm-appimage.yml

@@ -25,6 +25,8 @@ jobs:
         run: npm install --force
 
       - name: 运行自定义构建脚本
+        env:
+          GH_TOKEN: ${{ secrets.GH_TOKEN }}   # 关键:把 secret 映射成环境变量
         run: node .build/h5_for_electron.build.linux.arm.js
 
       - name: 切换 Node.js 18 环境