Cross-Compilation
ปัญหาการ deploy Node
หัวข้อที่มีชื่อว่า “ปัญหาการ deploy Node”เมื่อ deploy แอปพลิเคชัน Node.js หรือ TypeScript ไปยัง server คุณต้องมั่นใจว่าเครื่องเป้าหมายมี:
- Node.js version ที่ถูกต้องติดตั้งไว้
node_modules/dependencies ทั้งหมด (หรือ bundled ไว้)- native addon binary ที่ถูกต้องสำหรับ target OS/CPU (เช่น
bcrypt,sharp)
การ deploy ข้าม architecture — เช่น คอมไพล์บน Mac x86_64 แล้ว deploy ไป AWS Graviton server แบบ ARM — ต้องมี environment ที่ตรงกันในเครื่อง หรือ Docker build ที่ target platform ให้ถูก
Go cross-compilation ด้วย environment variables สองตัว
หัวข้อที่มีชื่อว่า “Go cross-compilation ด้วย environment variables สองตัว”Go มี support สำหรับ cross-compilation แบบ first-class คุณตั้งค่า environment variables สองตัวก่อน go build และ compiler จะสร้าง binary สำหรับ platform เป้าหมาย — ไม่ต้องใช้ Docker, cross-compiler toolchain หรือ emulation layer
# The two magic variablesGOOS=<target-os> GOARCH=<target-arch>
# Common valuesGOOS: linux darwin windows freebsdGOARCH: amd64 arm64 386 armตัวอย่างการใช้งานจริง
หัวข้อที่มีชื่อว่า “ตัวอย่างการใช้งานจริง”# Build for Linux AMD64 (most AWS EC2, typical server)GOOS=linux GOARCH=amd64 go build -o bin/app-linux-amd64 ./cmd/app
# Build for Linux ARM64 (AWS Graviton, Apple Silicon EC2)GOOS=linux GOARCH=arm64 go build -o bin/app-linux-arm64 ./cmd/app
# Build for Windows from macOS or LinuxGOOS=windows GOARCH=amd64 go build -o bin/app.exe ./cmd/app
# Build for macOS Apple Silicon from any platformGOOS=darwin GOARCH=arm64 go build -o bin/app-darwin-arm64 ./cmd/appลองรันคำสั่งพวกนี้ใน terminal คุณจะเห็น binary โผล่ใน bin/ ทันที cross-compiled จากเครื่อง host ของคุณโดยไม่ต้องใช้เครื่องมือเพิ่ม
Release script: build สำหรับหลาย target
หัวข้อที่มีชื่อว่า “Release script: build สำหรับหลาย target”Shell script ง่าย ๆ เพื่อสร้าง release binary สำหรับทุก platform ที่ใช้บ่อย:
#!/usr/bin/env bashset -euo pipefail
APP="myapp"VERSION=$(git describe --tags --always)OUTDIR="dist/${VERSION}"mkdir -p "${OUTDIR}"
targets=( "linux/amd64" "linux/arm64" "darwin/amd64" "darwin/arm64" "windows/amd64")
for target in "${targets[@]}"; do GOOS="${target%/*}" GOARCH="${target#*/}" ext="" [[ "$GOOS" == "windows" ]] && ext=".exe"
output="${OUTDIR}/${APP}-${GOOS}-${GOARCH}${ext}" echo "Building ${output}..." CGO_ENABLED=0 GOOS="${GOOS}" GOARCH="${GOARCH}" \ go build -ldflags="-s -w -X main.version=${VERSION}" \ -o "${output}" ./cmd/${APP}done
echo "Done. Binaries in ${OUTDIR}/"# TypeScript/Node deployment checklist# (target machine must have all of these)
1. Install Node.js v24 on the server curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash sudo apt-get install -y nodejs
2. Upload your build artifact + package.json scp -r dist/ package.json server:/app/
3. Install production deps on the server ssh server "cd /app && npm ci --omit=dev"
4. Rebuild native addons for target platform npm rebuild bcrypt sharp # if you use them
5. Start the process node dist/main.js# Go cross-compilation — build on dev, deploy anywhere# Target machine needs: NOTHING. Just copy the binary.
# From your Mac, build for a Linux server:GOOS=linux GOARCH=amd64 \ CGO_ENABLED=0 \ go build -o app-linux ./cmd/app
# Upload the single binaryscp app-linux server:/usr/local/bin/myapp
# On the server — just run it:ssh server "/usr/local/bin/myapp"