From 60dea22ced70b93e7ba3c61af907932ee8544337 Mon Sep 17 00:00:00 2001 From: Egor Pozharov Date: Tue, 21 Apr 2026 12:54:39 +0600 Subject: [PATCH] add git hooks for commit validation, auto-suggestions, and Docker build/push automation on git push --- .githooks/commit-msg | 27 +++++++++++++++++++++ .githooks/post-commit | 17 +++++++++++++ .githooks/pre-push | 37 +++++++++++++++++++++++++++++ .githooks/prepare-commit-msg | 46 ++++++++++++++++++++++++++++++++++++ Makefile | 13 ++++++---- 5 files changed, 136 insertions(+), 4 deletions(-) create mode 100755 .githooks/commit-msg create mode 100755 .githooks/post-commit create mode 100755 .githooks/pre-push create mode 100755 .githooks/prepare-commit-msg diff --git a/.githooks/commit-msg b/.githooks/commit-msg new file mode 100755 index 0000000..c249161 --- /dev/null +++ b/.githooks/commit-msg @@ -0,0 +1,27 @@ +#!/bin/bash +# Commit message hook: validate commit message format + +COMMIT_MSG_FILE=$1 +MSG=$(head -n1 "$COMMIT_MSG_FILE") + +# Skip validation for merge commits +if echo "$MSG" | grep -q "^Merge"; then + exit 0 +fi + +# Check message length +if [ ${#MSG} -lt 5 ]; then + echo "❌ Commit message too short (min 5 characters)" + exit 1 +fi + +# Optional: enforce conventional commits format +# Uncomment if you want to enforce prefixes like "feat:", "fix:", etc. +# if ! echo "$MSG" | grep -qE "^(feat|fix|docs|style|refactor|test|chore|backend|frontend|deploy)(\(.+\))?:"; then +# echo "❌ Commit message should follow format: type: description" +# echo " Allowed types: feat, fix, docs, style, refactor, test, chore, backend, frontend, deploy" +# exit 1 +# fi + +echo "✅ Commit message OK" +exit 0 diff --git a/.githooks/post-commit b/.githooks/post-commit new file mode 100755 index 0000000..98a9d22 --- /dev/null +++ b/.githooks/post-commit @@ -0,0 +1,17 @@ +#!/bin/bash +# Post-commit hook: show info after successful commit + +COMMIT_HASH=$(git rev-parse --short HEAD) +COMMIT_MSG=$(git log -1 --pretty=%s) +BRANCH=$(git rev-parse --abbrev-ref HEAD) + +echo "" +echo "✅ Commit created: $COMMIT_HASH" +echo "📝 Message: $COMMIT_MSG" +echo "🌿 Branch: $BRANCH" +echo "" + +# Remind to push if needed +if [ "$BRANCH" = "main" ] || [ "$BRANCH" = "master" ]; then + echo "💡 Tip: Run 'git push' to trigger deployment" +fi diff --git a/.githooks/pre-push b/.githooks/pre-push new file mode 100755 index 0000000..edf11e7 --- /dev/null +++ b/.githooks/pre-push @@ -0,0 +1,37 @@ +#!/bin/bash +# Pre-push hook: build and push Docker images (blocks until complete) +# This ensures push only succeeds after successful build + +REGISTRY="gitea.chedius.ru/chedius" +PLATFORM="linux/amd64" + +echo "🔨 Building Docker images..." + +# Build backend +docker build --platform $PLATFORM -t $REGISTRY/delivery-tracker/backend:latest ./backend || { + echo "❌ Backend build failed" + exit 1 +} + +# Build frontend +docker build --platform $PLATFORM -t $REGISTRY/delivery-tracker/frontend:latest ./frontend || { + echo "❌ Frontend build failed" + exit 1 +} + +echo "📤 Pushing Docker images..." + +# Push backend +docker push $REGISTRY/delivery-tracker/backend:latest || { + echo "❌ Backend push failed" + exit 1 +} + +# Push frontend +docker push $REGISTRY/delivery-tracker/frontend:latest || { + echo "❌ Frontend push failed" + exit 1 +} + +echo "✅ Docker images built and pushed successfully" +echo "📡 Git push will now proceed..." diff --git a/.githooks/prepare-commit-msg b/.githooks/prepare-commit-msg new file mode 100755 index 0000000..10aac72 --- /dev/null +++ b/.githooks/prepare-commit-msg @@ -0,0 +1,46 @@ +#!/bin/bash +# Prepare commit message hook: auto-generate commit message based on changes + +COMMIT_MSG_FILE=$1 +COMMIT_SOURCE=$2 + +# Only suggest message for regular commits (not merge, squash, etc.) +if [ -z "$COMMIT_SOURCE" ] || [ "$COMMIT_SOURCE" = "message" ]; then + # Get list of changed files + CHANGED_FILES=$(git diff --cached --name-only --diff-filter=ACM) + + if [ -z "$CHANGED_FILES" ]; then + exit 0 + fi + + # Determine commit type based on changed files + if echo "$CHANGED_FILES" | grep -q "backend/"; then + PREFIX="backend:" + elif echo "$CHANGED_FILES" | grep -q "frontend/"; then + PREFIX="frontend:" + elif echo "$CHANGED_FILES" | grep -q "\.github\|\.gitea\|deploy\|docker"; then + PREFIX="deploy:" + else + PREFIX="chore:" + fi + + # Count files + FILE_COUNT=$(echo "$CHANGED_FILES" | wc -l | tr -d ' ') + + # Generate suggested message + if [ "$FILE_COUNT" -eq 1 ]; then + FILENAME=$(basename "$CHANGED_FILES") + SUGGESTED_MSG="$PREFIX update $FILENAME" + else + SUGGESTED_MSG="$PREFIX update $FILE_COUNT files" + fi + + # If message file is empty or has default template, add suggestion + if [ ! -s "$COMMIT_MSG_FILE" ] || ! grep -v '^#' "$COMMIT_MSG_FILE" | grep -q '[^[:space:]]'; then + echo "$SUGGESTED_MSG" > "$COMMIT_MSG_FILE" + echo "" >> "$COMMIT_MSG_FILE" + echo "# Suggested commit message generated based on changes" >> "$COMMIT_MSG_FILE" + echo "# Changed files:" >> "$COMMIT_MSG_FILE" + echo "$CHANGED_FILES" | sed 's/^/# /' >> "$COMMIT_MSG_FILE" + fi +fi diff --git a/Makefile b/Makefile index ed697ec..84ab2cd 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ REGISTRY = gitea.chedius.ru/chedius PLATFORM = linux/amd64 # Build and push both services -.PHONY: all build push deploy +.PHONY: all build push deploy install-hooks all: build push @@ -30,11 +30,16 @@ update-server: docker pull $(REGISTRY)/delivery-tracker/frontend:latest docker-compose up -d --force-recreate backend frontend -# Full workflow: commit, build, push +# Install git hooks for automation +install-hooks: + git config core.hooksPath .githooks + chmod +x .githooks/* + @echo "✅ Git hooks installed from .githooks/" + +# Full release: commit, push, and auto-build via hook release: @if [ -z "$(MSG)" ]; then echo "Usage: make release MSG='commit message'"; exit 1; fi git add -A git commit -m "$(MSG)" || true git push - $(MAKE) build push - @echo "Released! Watchtower will deploy within 60 seconds." + @echo "✅ Released! Watchtower will deploy within 60 seconds."