From 23f197aeb13361d34493b3ac6398e1be38a8f07e Mon Sep 17 00:00:00 2001 From: xtcnet Date: Tue, 17 Mar 2026 23:30:17 +0700 Subject: [PATCH] feat: auto-tune swap and Node.js memory based on VPS RAM MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit install.sh: detect RAM at runner install time and create swap only when needed (<2GB → 2G swap, 2-4GB → 1G swap, >4GB → no swap). workflow: detect RAM at build time and set NODE_OPTIONS accordingly (<2GB → 768MB, 2-4GB → 1536MB, >4GB → 3072MB). Co-Authored-By: Claude Sonnet 4.6 --- .github/workflows/docker-publish.yml | 12 ++++++++-- install.sh | 33 +++++++++++++++++++--------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index 4fccfe3..a4e8f2d 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -45,9 +45,17 @@ jobs: node-version: 20 - name: Install Frontend Dependencies & Build - env: - NODE_OPTIONS: "--max-old-space-size=768" run: | + total_ram_mb=$(awk '/MemTotal/ { printf "%d", $2/1024 }' /proc/meminfo) + if [ "$total_ram_mb" -lt 2048 ]; then + node_mem=768 + elif [ "$total_ram_mb" -lt 4096 ]; then + node_mem=1536 + else + node_mem=3072 + fi + echo "RAM: ${total_ram_mb}MB — setting Node.js max to ${node_mem}MB" + export NODE_OPTIONS="--max-old-space-size=${node_mem}" cd frontend yarn install --frozen-lockfile yarn locale-compile diff --git a/install.sh b/install.sh index 79e2af3..8dd3f43 100644 --- a/install.sh +++ b/install.sh @@ -778,17 +778,30 @@ do_forgejo_runner_install() { mkdir -p "$FORGEJO_RUNNER_DIR" - # Ensure swap exists for low-RAM VPS (Docker image builds need ~2GB memory) - if [ ! -f /swapfile ]; then - log_step "Creating 2GB swapfile for build memory..." - fallocate -l 2G /swapfile - chmod 600 /swapfile - mkswap /swapfile > /dev/null - swapon /swapfile - echo '/swapfile none swap sw 0 0' >> /etc/fstab - log_ok "Swapfile created and enabled (persistent across reboots)." + # Auto-configure swap based on available RAM + local total_ram_mb + total_ram_mb=$(awk '/MemTotal/ { printf "%d", $2/1024 }' /proc/meminfo) + local swap_size="" + if [ "$total_ram_mb" -lt 2048 ]; then + swap_size="2G" + elif [ "$total_ram_mb" -lt 4096 ]; then + swap_size="1G" + fi + + if [ -n "$swap_size" ]; then + if [ ! -f /swapfile ]; then + log_step "RAM: ${total_ram_mb}MB detected — creating ${swap_size} swapfile..." + fallocate -l "$swap_size" /swapfile + chmod 600 /swapfile + mkswap /swapfile > /dev/null + swapon /swapfile + echo '/swapfile none swap sw 0 0' >> /etc/fstab + log_ok "Swapfile ${swap_size} created and enabled (persistent across reboots)." + else + log_ok "Swapfile already exists, skipping." + fi else - log_ok "Swapfile already exists, skipping." + log_ok "RAM: ${total_ram_mb}MB — sufficient, no swap needed." fi # gitea/act_runner auto-registers on first start via env vars, then starts daemon