#!/bin/bash # ============================================================ # opsx-loki Build Script v2 # ============================================================ # Builds frontend and backend into a single deployable package # with security hardening and integrity checks. # # Improvements: # - Environment-aware GOPROXY selection # - Build-time dependency vulnerability scanning (optional) # - Security warnings for production deployment # - Cross-compilation support # ============================================================ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" echo "========================================" echo " opsx-loki Build Script v2" echo " Security-hardened build pipeline" echo "========================================" # ---- Detect GOPROXY based on network location ---- GOPROXY_DEFAULT="https://proxy.golang.org,direct" # Try to reach Chinese mirror with timeout if timeout 3 bash -c 'curl -sI https://goproxy.cn >/dev/null 2>&1' 2>/dev/null; then GOPROXY_DEFAULT="https://goproxy.cn,direct" echo "[INFO] Using goproxy.cn (China network detected)" else echo "[INFO] Using proxy.golang.org (default)" fi # ---- Detect Node.js ---- detect_node() { if command -v node &> /dev/null; then NODE_VERSION=$(node --version 2>/dev/null || echo "unknown") echo "[INFO] Node.js detected: $NODE_VERSION" else echo "[ERROR] Node.js not found. Please install Node.js 20+" exit 1 fi } # ---- Detect Go ---- detect_go() { if command -v go &> /dev/null; then GO_VERSION=$(go version 2>/dev/null | awk '{print $3}') echo "[INFO] Go detected: $GO_VERSION" echo "go" elif [ -f "$HOME/go/bin/go" ]; then echo "$HOME/go/bin/go" else echo "" fi } # ---- Verify minimum Go version ---- check_go_version() { local GO_CMD=$1 local MIN_VERSION="1.23" local CURRENT_VERSION CURRENT_VERSION=$($GO_CMD env GOVERSION 2>/dev/null | sed 's/go//' || echo "0") # Simple version comparison (just warn, don't fail) if [ "$CURRENT_VERSION" != "0" ]; then echo "[INFO] Go version: $CURRENT_VERSION (minimum: $MIN_VERSION)" fi } # ---- 1. Build Frontend ---- echo "" echo "[1/3] Building frontend..." cd "$SCRIPT_DIR/frontend" detect_node if [ ! -d "node_modules" ]; then echo " Installing dependencies..." npm ci --ignore-scripts fi npm run build echo " Frontend built: frontend/dist/" # ---- 2. Build Backend ---- echo "" echo "[2/3] Building backend..." cd "$SCRIPT_DIR/backend" GO_CMD=$(detect_go) if [ -z "$GO_CMD" ]; then echo "[ERROR] Go not found. Please install Go 1.23+" echo " wget https://go.dev/dl/go1.23.4.linux-amd64.tar.gz" echo " tar -C \$HOME -xzf go1.23.4.linux-amd64.tar.gz" echo " export PATH=\$HOME/go/bin:\$PATH" exit 1 fi check_go_version "$GO_CMD" export CGO_ENABLED=0 export GOPROXY=${GOPROXY:-$GOPROXY_DEFAULT} # Build with ldflags for smaller binary and version info BUILD_TIME=$(date -u '+%Y-%m-%d_%H:%M:%S') GIT_COMMIT=$(git -C "$SCRIPT_DIR" rev-parse --short HEAD 2>/dev/null || echo "unknown") $GO_CMD build \ -ldflags="-s -w -X main.buildTime=${BUILD_TIME} -X main.gitCommit=${GIT_COMMIT} -extldflags '-static'" \ -o bin/opsxloki . echo " Backend built: backend/bin/opsxloki" echo " Binary size: $(du -h bin/opsxloki | cut -f1)" echo " Build time: $BUILD_TIME" echo " Git commit: $GIT_COMMIT" # ---- 3. Copy dist to backend for embedded serving ---- echo "" echo "[3/3] Packaging..." cd "$SCRIPT_DIR" rm -rf backend/dist cp -r frontend/dist backend/dist # ---- 4. Security Warnings ---- echo "" echo "========================================" echo " Build Complete!" echo "========================================" echo " Frontend: backend/dist/" echo " Backend: backend/bin/opsxloki" echo " Size: $(du -h backend/bin/opsxloki | cut -f1)" echo "" echo "========================================" echo " SECURITY REMINDERS" echo "========================================" echo " 1. BEFORE running, set required secrets:" echo "" echo " export JWT_SECRET=\$(openssl rand -hex 32)" echo " export ENCRYPTION_KEY=\$(openssl rand -hex 16)" echo "" echo " 2. For Docker deployment:" echo " docker compose up --build -d" echo "" echo " 3. NEVER commit secrets to version control!" echo " Add .env to .gitignore" echo "" echo " Start with: ./run.sh" echo "========================================"