matching_app/Makefile
2025-05-02 15:12:18 +07:00

223 lines
8.4 KiB
Makefile

# Makefile for matching-app
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GOFMT=$(GOCMD) fmt
GOVET=$(GOCMD) vet
GOINSTALL=$(GOCMD) install
GOMOD=$(GOCMD) mod
# Use 'go run' for govulncheck to avoid potential version conflicts if installed globally
GOVULNCHECK=$(GOCMD) run golang.org/x/vuln/cmd/govulncheck@latest
# Tools - Ensure these are installed or use 'make install-tools'
# You might need to adjust paths or commands based on your installation method
GOLANGCILINT ?= golangci-lint
GOSEC ?= gosec # Rely on gosec being in PATH after 'make install-tools'
# Project settings
BINARY_NAME=matching-app
# Quản lý phiên bản
VERSION=1.0.0
GIT_COMMIT=$(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
BUILD_DATE=$(shell date +%FT%T%z)
# Use forward slashes for Go compatibility, even on Windows
CMD_DIR=./cmd/server
BUILD_DIR=./build
ALL_DIRS=./...
PORT ?= 8080
# Docker settings
DOCKER_COMPOSE=docker-compose
DEV_COMPOSE_FILE=./template/docker-compose.dev.yml
DOCKER_REGISTRY=
DOCKER_IMAGE_NAME=$(BINARY_NAME)
DOCKER_IMAGE_TAG=$(VERSION)
STAGING_COMPOSE_FILE=./template/docker-compose.staging.yml
PROD_COMPOSE_FILE=./template/docker-compose.prod.yml
# Default target executed when running 'make'
.DEFAULT_GOAL := help
# Phony targets are rules that don't represent files
.PHONY: all build clean lint lint-fmt lint-vet lint-style sast test sca ci-local install-tools help \
run dev docker-build docker-up docker-down docker-logs dev-workflow dev-docker \
version tag docker-tag
all: ci-local build ## Run CI checks and build the application
# --- Build ---
build: ## Build the Go application binary
@echo ">>> Building application..."
-mkdir "$(BUILD_DIR)"
$(GOBUILD) -o "$(BUILD_DIR)/$(BINARY_NAME).exe" "$(CMD_DIR)/main.go"
# --- Run ---
run: build ## Build and run the application
@echo ">>> Running application on port $(PORT)..."
@set PORT=$(PORT) && "$(BUILD_DIR)/$(BINARY_NAME).exe"
# --- Dev Mode ---
dev: ## Run the application with hot-reload (requires installing air first)
@echo ">>> Checking if air is installed..."
@where air.exe > NUL 2>&1 || where air > NUL 2>&1
@if errorlevel 1 ( \
echo Installing air for hot-reload... && \
$(GOINSTALL) github.com/cosmtrek/air@latest \
) else ( \
echo Found air, starting hot-reload server... \
)
@set PORT=$(PORT) && air
# --- Installation ---
install-tools: ## Install necessary Go tools (gosec, air, etc.)
@echo ">>> Installing/Updating Tools..."
@echo " Installing gosec..."
$(GOINSTALL) github.com/securego/gosec/v2/cmd/gosec@latest
@echo " Installing air for hot-reload development..."
$(GOINSTALL) github.com/cosmtrek/air@latest
@echo " Verifying golangci-lint installation..."
@where $(GOLANGCILINT).exe > NUL 2>&1 || where $(GOLANGCILINT) > NUL 2>&1
@if errorlevel 1 ( \
echo Info: golangci-lint not found or not in PATH. ; \
echo Please install it following instructions at: https://golangci-lint.run/usage/install/ ; \
echo Continuing without golangci-lint for now... \
) else ( \
echo Info: golangci-lint found. \
)
@echo " Installation/Verification complete."
# --- Linting ---
lint: lint-fmt lint-vet lint-style ## Run all linters (fmt, vet, style)
@echo ">>> Linting finished."
lint-fmt:
@echo "--> Running go fmt..."
$(GOFMT) $(ALL_DIRS)
lint-vet:
@echo "--> Running go vet..."
$(GOVET) $(ALL_DIRS)
lint-style:
@echo "--> Running golangci-lint..."
@where $(GOLANGCILINT).exe > NUL 2>&1 || where $(GOLANGCILINT) > NUL 2>&1
@if errorlevel 1 ( \
echo Info: $(GOLANGCILINT) not found or not in PATH. Skipping style lint. ; \
echo Please install it following instructions at: https://golangci-lint.run/usage/install/ \
) else ( \
echo Info: $(GOLANGCILINT) found, running... ; \
$(GOLANGCILINT) run ./... \
)
# --- Security Analysis ---
sast: ## Run Static Application Security Testing (SAST) using gosec
@echo ">>> Running SAST (gosec)..."
@echo " (Executing $(GOPATH)/bin/$(GOSEC)...)"
-$(GOPATH)/bin/$(GOSEC) ./...
sca: ## Run Software Composition Analysis (SCA) using govulncheck
@echo ">>> Running SCA (govulncheck)..."
-$(GOCMD) run golang.org/x/vuln/cmd/govulncheck@latest ./...
# --- Testing ---
test: ## Run unit tests (short mode)
@echo ">>> Running unit tests..."
-mkdir "$(BUILD_DIR)"
$(GOTEST) -short -v -coverprofile="$(BUILD_DIR)/coverage.out" $(ALL_DIRS)
$(GOCMD) tool cover -func="$(BUILD_DIR)/coverage.out"
# --- Docker ---
docker-build: ## Build the Docker image
@echo ">>> Building Docker image..."
docker build -t $(BINARY_NAME) .
docker-up: ## Start Docker containers with docker-compose dev configuration
@echo ">>> Starting Docker containers (dev environment)..."
$(DOCKER_COMPOSE) -f $(DEV_COMPOSE_FILE) --env-file .env up -d
docker-down: ## Stop and remove Docker containers
@echo ">>> Stopping Docker containers..."
$(DOCKER_COMPOSE) -f $(DEV_COMPOSE_FILE) down
docker-logs: ## View logs from Docker containers
@echo ">>> Showing Docker logs..."
$(DOCKER_COMPOSE) -f $(DEV_COMPOSE_FILE) logs -f
# --- CI/CD Helpers (Chỉ dành cho CI/CD pipeline, không gọi trực tiếp) ---
# Các lệnh này KHÔNG nên được gọi trực tiếp bởi Developer, chỉ dành cho hệ thống CI/CD
# Các lệnh triển khai đã được chuyển vào pipeline CI/CD
# Xem tài liệu ci-cd-pipeline.md để biết chi tiết về quy trình triển khai
# --- Local CI Pipeline ---
# Ensures tools are checked/installed before running sequence
ci-local: lint sast test sca ## Run full local CI check (Lint -> SAST -> Test -> SCA)
@echo "========================================"
@echo ">>> Local CI Checks Completed Successfully <<<"
@echo "========================================"
# --- Developer Workflow ---
dev-workflow: clean install-tools build test lint ## Developer workflow: clean, install tools, build, test, and lint
@echo "========================================"
@echo ">>> Developer Setup Completed Successfully <<<"
@echo ">>> Run 'make run' to start the application or 'make dev' for hot-reload <<<"
@echo "========================================"
dev-docker: docker-build docker-up ## Developer Docker workflow: build and start Docker containers
@echo "========================================"
@echo ">>> Docker Development Environment Running <<<"
@echo ">>> Run 'make docker-logs' to view logs <<<"
@echo ">>> Run 'make docker-down' to stop containers <<<"
@echo "========================================"
# --- Cleanup ---
clean: ## Remove build artifacts, binary, and coverage file
@echo ">>> Cleaning up..."
-$(GOCLEAN)
-@if exist "$(BUILD_DIR)" rmdir /s /q "$(BUILD_DIR)" # Windows compatible rmdir, ignore errors
# --- Version Management ---
version: ## Hiển thị thông tin phiên bản hiện tại của ứng dụng
@echo ">>> Phiên bản hiện tại của $(BINARY_NAME):"
@echo " Version: $(VERSION)"
@echo " Commit: $(GIT_COMMIT)"
@echo " Date: $(BUILD_DATE)"
@echo " Docker: $(DOCKER_REGISTRY)$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)"
# Cập nhật phiên bản trong code và tạo tag mới
tag: ## Tạo tag Git mới với phiên bản hiện tại
@echo ">>> Tạo tag v$(VERSION) cho phiên bản hiện tại..."
git tag -a v$(VERSION) -m "Version $(VERSION)"
@echo ">>> Đã tạo tag v$(VERSION)"
@echo ">>> Đẩy tag lên repository với lệnh: git push origin v$(VERSION)"
docker-tag: docker-build ## Tạo Docker image với tag và latest
@echo ">>> Tagging Docker image $(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)..."
docker tag $(DOCKER_IMAGE_NAME) $(DOCKER_REGISTRY)$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)
docker tag $(DOCKER_IMAGE_NAME) $(DOCKER_REGISTRY)$(DOCKER_IMAGE_NAME):latest
# Các lệnh phát hành đã được chuyển vào pipeline CI/CD
# Xem tài liệu workflow_guideline.md và ci-cd-pipeline.md để biết thêm
# --- Help ---
help: ## Display this help screen
@echo "----------------------------------------"
@echo "Makefile Commands for matching-app:"
@echo "----------------------------------------"
@echo "Usage: make [target]"
@echo ""
@echo "Available targets:"
@findstr /R /C:"## " Makefile | findstr /V /C:"@findstr" | findstr /V /C:"echo" | sort
@echo "----------------------------------------"
@echo "Development Workflow:"
@echo " 1. make dev-workflow - Set up development environment"
@echo " 2. make dev - Run with hot-reload"
@echo " 3. make test - Run tests"
@echo " 4. make ci-local - Run all checks"
@echo "----------------------------------------"