Skip to content

Commit a0239f2

Browse files
Enhance build process with disk space monitoring and emergency fallback
- Add disk space checking function to prevent build failures - Implement more robust emergency static site generation - Reduce memory buffer and swap file size for better performance - Simplify build process with minimal app generation strategy - Update GitHub Actions workflow to clean up disk space - Improve error handling and logging in build script
1 parent e5cdeae commit a0239f2

File tree

2 files changed

+124
-94
lines changed

2 files changed

+124
-94
lines changed

.github/workflows/publish.yml

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ jobs:
1919
runs-on: ubuntu-latest
2020

2121
steps:
22+
- name: Disk space cleanup
23+
run: |
24+
echo "Cleaning up disk space..."
25+
# Remove unnecessary large packages
26+
sudo rm -rf /usr/share/dotnet
27+
sudo rm -rf /usr/local/lib/android
28+
sudo rm -rf /opt/ghc
29+
sudo rm -rf /opt/hostedtoolcache/CodeQL
30+
31+
# Clean apt cache
32+
sudo apt-get clean
33+
sudo rm -rf /var/lib/apt/lists/*
34+
35+
# Show disk space
36+
df -h
37+
2238
- name: Checkout 🛎️
2339
uses: actions/checkout@v4
2440
with:
@@ -45,37 +61,31 @@ jobs:
4561
# Install only the absolute minimum required packages
4662
npm install --no-save next react react-dom cross-env fs-extra
4763
48-
- name: Extreme memory optimization
64+
- name: Memory optimization
4965
run: |
5066
# Clear npm and system caches
5167
npm cache clean --force
5268
sudo sysctl -w vm.drop_caches=3
5369
54-
# Create a massive swap file (32GB)
55-
sudo fallocate -l 32G /swapfile
70+
# Create a smaller swap file (8GB)
71+
sudo fallocate -l 8G /swapfile
5672
sudo chmod 600 /swapfile
5773
sudo mkswap /swapfile
5874
sudo swapon /swapfile
5975
6076
# Disable unnecessary services
6177
sudo service docker stop || true
6278
sudo service containerd stop || true
63-
sudo service snapd stop || true
64-
65-
# Kill any unnecessary processes
66-
ps aux | grep -i apt | awk '{print $2}' | xargs sudo kill -9 2>/dev/null || true
6779
68-
# Show available memory
80+
# Show available memory and disk space
6981
free -m
70-
71-
# Create temp directory for build artifacts
72-
mkdir -p /tmp/next-cache
82+
df -h
7383
7484
- name: Emergency build process 🏗️
7585
run: |
7686
echo "Starting emergency build process..."
77-
# Use Node.js with maximum memory limit
78-
NODE_OPTIONS="--max-old-space-size=12288" node build.js
87+
# Use Node.js with reasonable memory limit
88+
NODE_OPTIONS="--max-old-space-size=6144" node build.js
7989
env:
8090
NEXT_TELEMETRY_DISABLED: 1
8191
GITHUB_PAGES: true

build.js

Lines changed: 101 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ function cleanDirectories() {
3131
console.warn('Could not remove .babelrc file:', e.message);
3232
}
3333
}
34+
35+
// Clean up node_modules/.cache to save disk space
36+
if (fs.existsSync('node_modules/.cache')) {
37+
try {
38+
fs.rmSync('node_modules/.cache', { recursive: true, force: true });
39+
console.log('Cleaned node_modules/.cache directory');
40+
} catch (e) {
41+
console.warn('Could not remove node_modules/.cache directory:', e.message);
42+
}
43+
}
3444
}
3545

3646
// Set up environment
@@ -57,7 +67,7 @@ function executeCommand(command, options = {}) {
5767
execSync(command, {
5868
stdio: 'inherit',
5969
env: process.env,
60-
maxBuffer: 1024 * 1024 * 20, // 20MB buffer
70+
maxBuffer: 1024 * 1024 * 10, // 10MB buffer
6171
...options
6272
});
6373
return true;
@@ -84,7 +94,7 @@ function createMinimalExport(pagePath, outputPath) {
8494
<head>
8595
<meta charset="utf-8">
8696
<meta name="viewport" content="width=device-width, initial-scale=1">
87-
<title>Page</title>
97+
<title>Lucas Becker</title>
8898
<script>
8999
window.location.href = '/lucasbecker-dev/';
90100
</script>
@@ -98,9 +108,42 @@ function createMinimalExport(pagePath, outputPath) {
98108
return true;
99109
}
100110

111+
// Check disk space
112+
function checkDiskSpace() {
113+
try {
114+
const df = execSync('df -h .').toString();
115+
console.log('Current disk space:');
116+
console.log(df);
117+
118+
// Extract available space percentage
119+
const lines = df.split('\n');
120+
if (lines.length >= 2) {
121+
const parts = lines[1].split(/\s+/);
122+
if (parts.length >= 5) {
123+
const usagePercent = parseInt(parts[4].replace('%', ''));
124+
if (usagePercent > 90) {
125+
console.warn(`WARNING: Disk usage is at ${usagePercent}%, which may cause issues`);
126+
return false;
127+
}
128+
}
129+
}
130+
return true;
131+
} catch (error) {
132+
console.warn('Could not check disk space:', error.message);
133+
return true; // Assume it's okay if we can't check
134+
}
135+
}
136+
101137
// Main build function
102138
async function build() {
103139
try {
140+
// Check disk space before starting
141+
if (!checkDiskSpace()) {
142+
console.log('Disk space is low, creating minimal static export...');
143+
createStaticExport();
144+
return;
145+
}
146+
104147
// Step 1: Clean up
105148
cleanDirectories();
106149

@@ -130,8 +173,8 @@ export default nextConfig
130173
`;
131174
fs.writeFileSync('next.config.mjs', emergencyConfig);
132175

133-
// Step 4: Try to build just the homepage
134-
console.log('Attempting to build only the homepage...');
176+
// Step 4: Try to build just the homepage with minimal app
177+
console.log('Attempting to build with minimal app...');
135178

136179
// Create a temporary app directory with only essential files
137180
if (!fs.existsSync('app-backup')) {
@@ -144,14 +187,28 @@ export default nextConfig
144187
fs.rmSync('app', { recursive: true, force: true });
145188
fs.mkdirSync('app', { recursive: true });
146189

147-
// Copy only essential files for homepage
148-
if (fs.existsSync('app-backup/page.tsx')) {
149-
fs.copyFileSync('app-backup/page.tsx', 'app/page.tsx');
150-
}
190+
// Create a minimal page.tsx
191+
const minimalPage = `
192+
export default function Home() {
193+
return (
194+
<main>
195+
<h1>Lucas Becker</h1>
196+
<p>Welcome to my website! The full site is currently being updated.</p>
197+
<p>Please check back soon for the complete experience.</p>
198+
<div>
199+
<h2>Links</h2>
200+
<ul>
201+
<li><a href="https://github.com/lucasbecker-dev" target="_blank" rel="noopener noreferrer">GitHub</a></li>
202+
</ul>
203+
</div>
204+
</main>
205+
);
206+
}
207+
`;
208+
fs.writeFileSync('app/page.tsx', minimalPage);
151209

152-
if (fs.existsSync('app-backup/layout.tsx')) {
153-
// Create a simplified layout
154-
const layoutContent = `
210+
// Create a simplified layout
211+
const layoutContent = `
155212
import React from 'react';
156213
157214
export const metadata = {
@@ -171,63 +228,24 @@ export default function RootLayout({
171228
);
172229
}
173230
`;
174-
fs.writeFileSync('app/layout.tsx', layoutContent);
175-
}
231+
fs.writeFileSync('app/layout.tsx', layoutContent);
176232
}
177233

178-
// Try to build with extreme memory limits
179-
const success = executeCommand('npx cross-env NODE_OPTIONS="--max-old-space-size=12288" next build');
180-
181-
if (success) {
182-
console.log('Homepage build successful!');
183-
} else {
184-
console.log('Homepage build failed, creating manual static export...');
185-
186-
// Create out directory
187-
if (!fs.existsSync('out')) {
188-
fs.mkdirSync('out', { recursive: true });
189-
}
190-
191-
// Create a minimal index.html
192-
const indexHtml = `<!DOCTYPE html>
193-
<html lang="en">
194-
<head>
195-
<meta charset="utf-8">
196-
<meta name="viewport" content="width=device-width, initial-scale=1">
197-
<title>Lucas Becker</title>
198-
<style>
199-
body {
200-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
201-
max-width: 800px;
202-
margin: 0 auto;
203-
padding: 2rem;
204-
line-height: 1.6;
234+
// Check disk space again
235+
if (!checkDiskSpace()) {
236+
console.log('Disk space is low, creating minimal static export...');
237+
createStaticExport();
238+
return;
205239
}
206-
h1 { color: #333; }
207-
a { color: #0070f3; text-decoration: none; }
208-
a:hover { text-decoration: underline; }
209-
</style>
210-
</head>
211-
<body>
212-
<h1>Lucas Becker</h1>
213-
<p>Welcome to my website! The full site is currently being updated.</p>
214-
<p>Please check back soon for the complete experience.</p>
215-
216-
<div>
217-
<h2>Links</h2>
218-
<ul>
219-
<li><a href="https://github.com/lucasbecker-dev" target="_blank">GitHub</a></li>
220-
</ul>
221-
</div>
222-
</body>
223-
</html>`;
224240

225-
fs.writeFileSync('out/index.html', indexHtml);
241+
// Try to build with minimal memory
242+
const success = executeCommand('npx next build');
226243

227-
// Create a 404 page
228-
fs.writeFileSync('out/404.html', indexHtml);
229-
230-
console.log('Created manual static export');
244+
if (success) {
245+
console.log('Build successful!');
246+
} else {
247+
console.log('Build failed, creating manual static export...');
248+
createStaticExport();
231249
}
232250

233251
// Restore original files
@@ -240,35 +258,40 @@ export default function RootLayout({
240258
fs.writeFileSync('next.config.mjs', originalConfig);
241259
fs.unlinkSync('next.config.mjs.backup');
242260

243-
console.log('Build process completed with emergency fallback');
261+
console.log('Build process completed');
244262
return true;
245263
} catch (error) {
246264
console.error('Build process failed:', error.message);
247265

248266
// Final emergency fallback - create a minimal static site
249-
console.log('Creating emergency static site...');
267+
createStaticExport();
268+
process.exit(1);
269+
}
270+
}
250271

251-
if (!fs.existsSync('out')) {
252-
fs.mkdirSync('out', { recursive: true });
253-
}
272+
// Create a static export as a last resort
273+
function createStaticExport() {
274+
console.log('Creating emergency static site...');
275+
276+
if (!fs.existsSync('out')) {
277+
fs.mkdirSync('out', { recursive: true });
278+
}
254279

255-
const emergencyHtml = `<!DOCTYPE html>
280+
const emergencyHtml = `<!DOCTYPE html>
256281
<html lang="en">
257282
<head>
258283
<meta charset="utf-8">
259284
<meta name="viewport" content="width=device-width, initial-scale=1">
260285
<title>Lucas Becker</title>
261286
<style>
262287
body {
263-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
288+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
264289
max-width: 800px;
265290
margin: 0 auto;
266291
padding: 2rem;
267-
line-height: 1.6;
268292
}
269293
h1 { color: #333; }
270-
a { color: #0070f3; text-decoration: none; }
271-
a:hover { text-decoration: underline; }
294+
a { color: #0070f3; }
272295
</style>
273296
</head>
274297
<body>
@@ -277,19 +300,16 @@ export default function RootLayout({
277300
<p>Please check back soon for the complete experience.</p>
278301
279302
<div>
280-
<h2>Emergency Mode</h2>
281-
<p>The site is currently in emergency mode due to build issues.</p>
282-
<p>Please visit my <a href="https://github.com/lucasbecker-dev" target="_blank">GitHub profile</a> for more information.</p>
303+
<h2>Links</h2>
304+
<p><a href="https://github.com/lucasbecker-dev" target="_blank">GitHub Profile</a></p>
283305
</div>
284306
</body>
285307
</html>`;
286308

287-
fs.writeFileSync('out/index.html', emergencyHtml);
288-
fs.writeFileSync('out/404.html', emergencyHtml);
309+
fs.writeFileSync('out/index.html', emergencyHtml);
310+
fs.writeFileSync('out/404.html', emergencyHtml);
289311

290-
console.log('Created emergency static site');
291-
process.exit(1);
292-
}
312+
console.log('Created emergency static site');
293313
}
294314

295315
// Run the build

0 commit comments

Comments
 (0)