Skip to main content

Building for Production

OPS Intermediate

Build optimized production binaries for EZ-Console applications.

Overview

This guide covers building optimized production binaries for EZ-Console applications, including backend and frontend builds with optimizations.

Backend Build

Basic Build

cd backend
go build -o server main.go

Optimized Build

For production, use optimized build flags:

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags="-w -s" \
-trimpath \
-o server main.go

Build Flags Explained:

  • CGO_ENABLED=0: Disable CGO for static binary
  • GOOS=linux: Target operating system
  • GOARCH=amd64: Target architecture
  • -ldflags="-w -s": Strip debug information and symbol table
  • -trimpath: Remove file system paths from compiled binary

Cross-Platform Builds

# Linux
GOOS=linux GOARCH=amd64 go build -o server-linux-amd64 main.go

# Windows
GOOS=windows GOARCH=amd64 go build -o server-windows-amd64.exe main.go

# macOS
GOOS=darwin GOARCH=amd64 go build -o server-darwin-amd64 main.go

# ARM64
GOOS=linux GOARCH=arm64 go build -o server-linux-arm64 main.go

Build with Version Info

VERSION=$(git describe --tags --always)
BUILD_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
COMMIT=$(git rev-parse --short HEAD)

go build \
-ldflags "-X main.Version=$VERSION -X main.BuildTime=$BUILD_TIME -X main.Commit=$COMMIT" \
-o server main.go

Frontend Build

Development Build

cd web
pnpm install
pnpm build

Production Build

cd web
pnpm install --production=false
NODE_ENV=production pnpm build

Build Output

The build creates a dist directory with optimized static files:

  • Minified JavaScript
  • Optimized CSS
  • Compressed assets
  • Source maps (optional)

Environment Variables

Set production environment variables:

VITE_API_BASE_URL=https://api.example.com \
VITE_APP_NAME="My App" \
pnpm build

Docker Build

Multi-Stage Dockerfile

# Backend builder
FROM golang:1.20-alpine AS backend-builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build \
-ldflags="-w -s" \
-trimpath \
-o server main.go

# Frontend builder
FROM node:18-alpine AS frontend-builder
WORKDIR /app
COPY web/package*.json ./
RUN npm install -g pnpm && pnpm install
COPY web/ ./
RUN pnpm build

# Final image
FROM alpine:latest
RUN apk --no-cache add ca-certificates tzdata
WORKDIR /app
COPY --from=backend-builder /app/server ./
COPY --from=frontend-builder /app/dist ./static
EXPOSE 8080
CMD ["./server", "--global.encrypt-key=${ENCRYPT_KEY}"]

Build Docker Image

docker build -t myapp:latest .

Build Scripts

Makefile

.PHONY: build build-backend build-frontend clean

build: build-backend build-frontend

build-backend:
cd backend && \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags="-w -s" \
-trimpath \
-o ../dist/server main.go

build-frontend:
cd web && \
pnpm install && \
pnpm build && \
cp -r dist ../dist/static

clean:
rm -rf dist
cd web && rm -rf dist node_modules/.vite

Build Script

#!/bin/bash
set -e

echo "Building backend..."
cd backend
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags="-w -s" \
-trimpath \
-o ../dist/server main.go

echo "Building frontend..."
cd ../web
pnpm install
pnpm build
cp -r dist ../dist/static

echo "Build complete!"

Optimization Tips

Backend Optimization

  1. Strip Debug Info: Use -ldflags="-w -s"
  2. Static Binary: Use CGO_ENABLED=0
  3. Trim Paths: Use -trimpath
  4. Optimize Size: Use UPX compression (optional)

Frontend Optimization

  1. Minification: Enabled by default in production build
  2. Tree Shaking: Remove unused code
  3. Code Splitting: Split code into chunks
  4. Asset Optimization: Compress images and assets

Testing Builds

Test Backend Binary

./dist/server --version
./dist/server --help

Test Frontend Build

cd dist/static
python3 -m http.server 8000
# Visit http://localhost:8000

Deployment Package

Create Deployment Package

mkdir -p deploy
cp dist/server deploy/
cp -r dist/static deploy/
cp config.yml deploy/
cp README.md deploy/
tar -czf deploy.tar.gz deploy/

Package Structure

deploy/
├── server # Backend binary
├── static/ # Frontend files
│ ├── index.html
│ └── assets/
├── config.yml # Configuration
└── README.md # Documentation

CI/CD Integration

GitHub Actions

name: Build

on:
push:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.20'

- name: Build Backend
run: |
cd backend
CGO_ENABLED=0 go build -o ../dist/server main.go

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: '18'

- name: Build Frontend
run: |
cd web
npm install -g pnpm
pnpm install
pnpm build
cp -r dist ../dist/static

- name: Upload Artifacts
uses: actions/upload-artifact@v3
with:
name: dist
path: dist/

Best Practices

1. Always Build for Target Platform

# ✅ Good: Build for target platform
GOOS=linux GOARCH=amd64 go build

# ❌ Bad: Build on different platform
go build # May not work on target

2. Strip Debug Information

# ✅ Good: Strip debug info for production
go build -ldflags="-w -s"

# ❌ Bad: Include debug info
go build

3. Use Static Binaries

# ✅ Good: Static binary
CGO_ENABLED=0 go build

# ❌ Bad: Dynamic binary (requires libc)
go build

4. Test Before Deployment

Always test production builds before deploying.


Need help? Ask in GitHub Discussions.