Skip to content

Commit 2d84525

Browse files
author
yuanpengfei03
committed
fix deploy
1 parent 95c28cc commit 2d84525

File tree

5 files changed

+222
-4
lines changed

5 files changed

+222
-4
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Deploy to GitHub Pages (Fallback)
2+
3+
on:
4+
workflow_run:
5+
workflows:
6+
- "Deploy to GitHub Pages"
7+
types:
8+
- failed
9+
10+
permissions:
11+
contents: read
12+
pages: write
13+
id-token: write
14+
15+
concurrency:
16+
group: "pages"
17+
cancel-in-progress: false
18+
19+
jobs:
20+
build:
21+
runs-on: ubuntu-latest
22+
steps:
23+
- name: Checkout
24+
uses: actions/checkout@v4
25+
26+
- name: Setup Node.js
27+
uses: actions/setup-node@v4
28+
with:
29+
node-version: '20'
30+
cache: 'yarn'
31+
32+
- name: Install Yarn
33+
run: npm install -g yarn
34+
35+
- name: Install dependencies
36+
run: yarn install --frozen-lockfile
37+
38+
- name: Build
39+
run: yarn build
40+
env:
41+
NODE_ENV: production
42+
NODE_OPTIONS: --max-old-space-size=4096
43+
44+
- name: Setup Pages
45+
uses: actions/configure-pages@v4
46+
47+
- name: Upload artifact
48+
uses: actions/upload-pages-artifact@v3
49+
with:
50+
path: './dist'
51+
52+
deploy:
53+
environment:
54+
name: github-pages
55+
url: ${{ steps.deployment.outputs.page_url }}
56+
runs-on: ubuntu-latest
57+
needs: build
58+
steps:
59+
- name: Deploy to GitHub Pages
60+
id: deployment
61+
uses: actions/deploy-pages@v4

.github/workflows/deploy.yml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,21 @@ jobs:
2929
node-version: '20'
3030
cache: 'npm'
3131

32-
- name: Install dependencies
33-
run: npm install && npm ci
32+
- name: Clear npm cache
33+
run: npm cache clean --force
34+
35+
- name: Install dependencies (with retry)
36+
uses: nick-fields/retry@v3
37+
with:
38+
timeout_minutes: 10
39+
max_attempts: 3
40+
retry_on: error
41+
command: npm ci --prefer-offline --no-audit --no-fund
42+
env:
43+
NODE_OPTIONS: --max-old-space-size=4096
3444

3545
- name: Build
36-
run: npm run build:prod
46+
run: npm run build
3747
env:
3848
NODE_ENV: production
3949
NODE_OPTIONS: --max-old-space-size=4096

package-lock.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "自由布局吸附线Demo",
55
"type": "module",
66
"homepage": "https://yourusername.github.io/free-layout-demo",
7+
"packageManager": "npm@10.0.0",
78
"engines": {
89
"node": ">=18.0.0",
910
"npm": ">=8.0.0"
@@ -15,7 +16,8 @@
1516
"build:prod": "vite build",
1617
"deploy": "npm run build:prod && gh-pages -d dist",
1718
"check-deploy": "node scripts/check-deploy.js",
18-
"test-build": "node scripts/test-build.js"
19+
"test-build": "node scripts/test-build.js",
20+
"test-npm": "node scripts/test-npm.js"
1921
},
2022
"dependencies": {
2123
"vue": "^3.4.21"

scripts/test-npm.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* npm 安装测试脚本
5+
* 运行: node scripts/test-npm.js
6+
*/
7+
8+
import { execSync } from 'child_process';
9+
import fs from 'fs';
10+
import path from 'path';
11+
import { fileURLToPath } from 'url';
12+
13+
const __filename = fileURLToPath(import.meta.url);
14+
const __dirname = path.dirname(__filename);
15+
16+
console.log('🧪 开始 npm 安装测试...\n');
17+
18+
// 检查npm版本
19+
try {
20+
const npmVersion = execSync('npm --version', { encoding: 'utf8' }).trim();
21+
console.log(`📦 npm 版本: ${npmVersion}`);
22+
23+
const majorVersion = parseInt(npmVersion.split('.')[0]);
24+
if (majorVersion < 8) {
25+
console.log('❌ npm 版本过低,需要 8.0.0 或更高版本');
26+
console.log(' 请升级 npm: npm install -g npm@latest');
27+
process.exit(1);
28+
} else {
29+
console.log('✅ npm 版本符合要求');
30+
}
31+
} catch (error) {
32+
console.log('❌ 无法获取 npm 版本');
33+
process.exit(1);
34+
}
35+
36+
// 检查Node.js版本
37+
try {
38+
const nodeVersion = process.version;
39+
console.log(`📋 Node.js 版本: ${nodeVersion}`);
40+
41+
const majorVersion = parseInt(process.version.slice(1).split('.')[0]);
42+
if (majorVersion < 18) {
43+
console.log('❌ Node.js 版本过低,需要 18.0.0 或更高版本');
44+
console.log(' 请升级 Node.js: https://nodejs.org/');
45+
process.exit(1);
46+
} else {
47+
console.log('✅ Node.js 版本符合要求');
48+
}
49+
} catch (error) {
50+
console.log('❌ 无法获取 Node.js 版本');
51+
process.exit(1);
52+
}
53+
54+
// 检查package.json和package-lock.json
55+
const packagePath = path.join(__dirname, '..', 'package.json');
56+
const lockPath = path.join(__dirname, '..', 'package-lock.json');
57+
58+
if (!fs.existsSync(packagePath)) {
59+
console.log('❌ package.json 不存在');
60+
process.exit(1);
61+
}
62+
63+
if (!fs.existsSync(lockPath)) {
64+
console.log('⚠️ package-lock.json 不存在,将生成新的锁文件');
65+
}
66+
67+
console.log('✅ package.json 存在');
68+
69+
// 清理之前的安装
70+
const nodeModulesPath = path.join(__dirname, '..', 'node_modules');
71+
if (fs.existsSync(nodeModulesPath)) {
72+
console.log('\n🧹 清理之前的 node_modules...');
73+
try {
74+
fs.rmSync(nodeModulesPath, { recursive: true, force: true });
75+
console.log('✅ node_modules 已清理');
76+
} catch (error) {
77+
console.log('⚠️ 清理 node_modules 失败,继续执行...');
78+
}
79+
}
80+
81+
// 清理npm缓存
82+
console.log('\n🧹 清理 npm 缓存...');
83+
try {
84+
execSync('npm cache clean --force', { stdio: 'inherit' });
85+
console.log('✅ npm 缓存已清理');
86+
} catch (error) {
87+
console.log('⚠️ 清理 npm 缓存失败,继续执行...');
88+
}
89+
90+
// 测试npm install
91+
console.log('\n📦 测试 npm install...');
92+
try {
93+
execSync('npm install', { stdio: 'inherit' });
94+
console.log('✅ npm install 成功');
95+
} catch (error) {
96+
console.log('❌ npm install 失败');
97+
console.log('\n🔍 可能的解决方案:');
98+
console.log('1. 检查网络连接');
99+
console.log('2. 尝试使用镜像源: npm config set registry https://registry.npmmirror.com');
100+
console.log('3. 检查 package.json 语法');
101+
console.log('4. 尝试删除 package-lock.json 后重新安装');
102+
process.exit(1);
103+
}
104+
105+
// 测试npm ci
106+
console.log('\n🔒 测试 npm ci...');
107+
try {
108+
execSync('npm ci', { stdio: 'inherit' });
109+
console.log('✅ npm ci 成功');
110+
} catch (error) {
111+
console.log('❌ npm ci 失败');
112+
console.log('\n🔍 可能的解决方案:');
113+
console.log('1. 确保 package-lock.json 与 package.json 同步');
114+
console.log('2. 尝试重新生成 package-lock.json');
115+
console.log('3. 检查依赖版本冲突');
116+
process.exit(1);
117+
}
118+
119+
// 检查安装的依赖
120+
console.log('\n📋 检查安装的依赖...');
121+
try {
122+
const dependencies = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
123+
const requiredDeps = ['vue', '@vitejs/plugin-vue', 'vite', 'less'];
124+
125+
requiredDeps.forEach(dep => {
126+
if (dependencies.dependencies && dependencies.dependencies[dep]) {
127+
console.log(`✅ ${dep}: ${dependencies.dependencies[dep]}`);
128+
} else if (dependencies.devDependencies && dependencies.devDependencies[dep]) {
129+
console.log(`✅ ${dep}: ${dependencies.devDependencies[dep]} (dev)`);
130+
} else {
131+
console.log(`❌ ${dep}: 未找到`);
132+
}
133+
});
134+
} catch (error) {
135+
console.log('❌ 无法检查依赖信息');
136+
}
137+
138+
console.log('\n🎉 npm 安装测试完成!');
139+
console.log('\n📝 下一步:');
140+
console.log('- 运行 npm run build 测试构建');
141+
console.log('- 运行 npm run test-build 进行完整测试');

0 commit comments

Comments
 (0)