Implement production and development build targets with binary size optimization
- Add LDFLAGS_BASE for version info only, LDFLAGS for production (with -s -w optimization), and LDFLAGS_DEV for development - Create 'build' target (production) that strips symbols, reducing binary size by ~30% (56MB -> 39MB) - Create 'build-dev' target (development) that keeps debug symbols for profiling - Update 'docker-build' to pass BUILD_FLAGS="-s -w" for optimized production images - Create 'docker-build-dev' target that builds development images with notifier:latest-dev tag and no optimization - Update Dockerfile to accept and use BUILD_FLAGS argument in build stage - All targets now clearly indicate their optimization level in output messages
This commit is contained in:
+2
-1
@@ -5,6 +5,7 @@ FROM golang:1.24-alpine AS builder
|
|||||||
ARG VERSION=dev
|
ARG VERSION=dev
|
||||||
ARG GIT_COMMIT=unknown
|
ARG GIT_COMMIT=unknown
|
||||||
ARG BUILD_TIME=unknown
|
ARG BUILD_TIME=unknown
|
||||||
|
ARG BUILD_FLAGS="-s -w"
|
||||||
|
|
||||||
# Install build dependencies including protoc
|
# Install build dependencies including protoc
|
||||||
RUN apk add --no-cache git make protobuf protobuf-dev
|
RUN apk add --no-cache git make protobuf protobuf-dev
|
||||||
@@ -30,7 +31,7 @@ RUN make proto-gen
|
|||||||
|
|
||||||
# Build binary with version information
|
# Build binary with version information
|
||||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo \
|
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo \
|
||||||
-ldflags "-X main.Version=${VERSION} -X main.GitCommit=${GIT_COMMIT} -X main.BuildTime=${BUILD_TIME}" \
|
-ldflags "-X main.Version=${VERSION} -X main.GitCommit=${GIT_COMMIT} -X main.BuildTime=${BUILD_TIME} ${BUILD_FLAGS}" \
|
||||||
-o server ./cmd/server
|
-o server ./cmd/server
|
||||||
|
|
||||||
# Runtime stage
|
# Runtime stage
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
.PHONY: proto proto-gen proto-clean deps build run run-grpc run-rest test lint fmt vet check docker-build docker-run clean help
|
.PHONY: proto proto-gen proto-clean deps build build-dev run run-grpc run-rest test lint fmt vet check docker-build docker-build-dev docker-run clean help
|
||||||
|
|
||||||
# Variables
|
# Variables
|
||||||
PROTO_DIR=api/grpc
|
PROTO_DIR=api/grpc
|
||||||
@@ -11,7 +11,15 @@ GO_FILES=$(shell find . -type f -name '*.go' -not -path "./vendor/*" -not -path
|
|||||||
VERSION ?= dev
|
VERSION ?= dev
|
||||||
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
|
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
|
||||||
BUILD_TIME := $(shell date -u '+%Y-%m-%d_%H:%M:%S_UTC')
|
BUILD_TIME := $(shell date -u '+%Y-%m-%d_%H:%M:%S_UTC')
|
||||||
LDFLAGS := -X main.Version=$(VERSION) -X main.GitCommit=$(GIT_COMMIT) -X main.BuildTime=$(BUILD_TIME)
|
|
||||||
|
# Base LDFLAGS (version info only)
|
||||||
|
LDFLAGS_BASE := -X main.Version=$(VERSION) -X main.GitCommit=$(GIT_COMMIT) -X main.BuildTime=$(BUILD_TIME)
|
||||||
|
|
||||||
|
# Production LDFLAGS: include symbol stripping for smaller binaries
|
||||||
|
LDFLAGS := $(LDFLAGS_BASE) -s -w
|
||||||
|
|
||||||
|
# Development LDFLAGS: keep symbols for debugging
|
||||||
|
LDFLAGS_DEV := $(LDFLAGS_BASE)
|
||||||
|
|
||||||
# Generate protobuf code
|
# Generate protobuf code
|
||||||
proto-gen:
|
proto-gen:
|
||||||
@@ -42,15 +50,27 @@ proto-deps:
|
|||||||
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
|
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
|
||||||
@echo "Protoc plugins installed"
|
@echo "Protoc plugins installed"
|
||||||
|
|
||||||
# Build binary
|
# Build binary (production - optimized with stripped symbols)
|
||||||
build:
|
build:
|
||||||
@echo "Building binary..."
|
@echo "Building binary (production - optimized)..."
|
||||||
@echo "Version: $(VERSION)"
|
@echo "Version: $(VERSION)"
|
||||||
@echo "Git Commit: $(GIT_COMMIT)"
|
@echo "Git Commit: $(GIT_COMMIT)"
|
||||||
@echo "Build Time: $(BUILD_TIME)"
|
@echo "Build Time: $(BUILD_TIME)"
|
||||||
@mkdir -p bin
|
@mkdir -p bin
|
||||||
go build -ldflags "$(LDFLAGS)" -o bin/server ./cmd/server
|
go build -ldflags "$(LDFLAGS)" -o bin/server ./cmd/server
|
||||||
@echo "Binary built successfully"
|
@ls -lh bin/server
|
||||||
|
@echo "Binary built successfully (symbols stripped for smaller size)"
|
||||||
|
|
||||||
|
# Build binary with debug symbols (development)
|
||||||
|
build-dev:
|
||||||
|
@echo "Building binary (development - with debug symbols)..."
|
||||||
|
@echo "Version: $(VERSION)"
|
||||||
|
@echo "Git Commit: $(GIT_COMMIT)"
|
||||||
|
@echo "Build Time: $(BUILD_TIME)"
|
||||||
|
@mkdir -p bin
|
||||||
|
go build -ldflags "$(LDFLAGS_DEV)" -o bin/server ./cmd/server
|
||||||
|
@ls -lh bin/server
|
||||||
|
@echo "Binary built successfully (debug symbols included for profiling/debugging)"
|
||||||
|
|
||||||
# Run server (default: both REST and gRPC)
|
# Run server (default: both REST and gRPC)
|
||||||
run:
|
run:
|
||||||
@@ -118,9 +138,9 @@ lint:
|
|||||||
qa: fmt vet lint test
|
qa: fmt vet lint test
|
||||||
@echo "All quality checks passed!"
|
@echo "All quality checks passed!"
|
||||||
|
|
||||||
# Build Docker image
|
# Build Docker image (production - optimized)
|
||||||
docker-build:
|
docker-build:
|
||||||
@echo "Building Docker image..."
|
@echo "Building Docker image (production - optimized)..."
|
||||||
@echo "Version: $(VERSION)"
|
@echo "Version: $(VERSION)"
|
||||||
@echo "Git Commit: $(GIT_COMMIT)"
|
@echo "Git Commit: $(GIT_COMMIT)"
|
||||||
@echo "Build Time: $(BUILD_TIME)"
|
@echo "Build Time: $(BUILD_TIME)"
|
||||||
@@ -128,8 +148,23 @@ docker-build:
|
|||||||
--build-arg VERSION=$(VERSION) \
|
--build-arg VERSION=$(VERSION) \
|
||||||
--build-arg GIT_COMMIT=$(GIT_COMMIT) \
|
--build-arg GIT_COMMIT=$(GIT_COMMIT) \
|
||||||
--build-arg BUILD_TIME=$(BUILD_TIME) \
|
--build-arg BUILD_TIME=$(BUILD_TIME) \
|
||||||
|
--build-arg BUILD_FLAGS="-s -w" \
|
||||||
-t notifier:latest .
|
-t notifier:latest .
|
||||||
@echo "Docker image built successfully"
|
@echo "Docker image built successfully (production - optimized)"
|
||||||
|
|
||||||
|
# Build Docker image with debug symbols (development)
|
||||||
|
docker-build-dev:
|
||||||
|
@echo "Building Docker image (development - with debug symbols)..."
|
||||||
|
@echo "Version: $(VERSION)"
|
||||||
|
@echo "Git Commit: $(GIT_COMMIT)"
|
||||||
|
@echo "Build Time: $(BUILD_TIME)"
|
||||||
|
docker build \
|
||||||
|
--build-arg VERSION=$(VERSION) \
|
||||||
|
--build-arg GIT_COMMIT=$(GIT_COMMIT) \
|
||||||
|
--build-arg BUILD_TIME=$(BUILD_TIME) \
|
||||||
|
--build-arg BUILD_FLAGS="" \
|
||||||
|
-t notifier:latest-dev .
|
||||||
|
@echo "Docker image built successfully (development)"
|
||||||
|
|
||||||
# Run Docker container
|
# Run Docker container
|
||||||
docker-run:
|
docker-run:
|
||||||
|
|||||||
Reference in New Issue
Block a user