Fix deploy: force Docker rebuild, add version check and redeploy script

This commit is contained in:
shop
2026-06-25 17:10:54 +03:00
parent efc95d48c4
commit 306002bfa1
9 changed files with 82 additions and 7 deletions
+5 -1
View File
@@ -9,7 +9,11 @@ COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /shop ./cmd/shop
ARG APP_VERSION=dev
ARG BUILD_TIME=unknown
RUN CGO_ENABLED=0 GOOS=linux go build \
-ldflags="-s -w -X shop/internal/version.Version=${APP_VERSION}" \
-o /shop ./cmd/shop
# Runtime stage
FROM alpine:3.21
+2 -1
View File
@@ -16,6 +16,7 @@ import (
"shop/internal/middleware"
"shop/internal/repository"
"shop/internal/upload"
"shop/internal/version"
)
func main() {
@@ -107,7 +108,7 @@ func main() {
}
go func() {
log.Printf("server listening on %s", addr)
log.Printf("Shop %s listening on %s", version.Version, addr)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %v", err)
}
+4
View File
@@ -26,6 +26,9 @@ services:
build:
context: .
dockerfile: Dockerfile
args:
APP_VERSION: ${APP_VERSION:-dev}
BUILD_TIME: ${BUILD_TIME:-}
container_name: shop-app
restart: unless-stopped
env_file: .env
@@ -34,6 +37,7 @@ services:
DB_PORT: "5432"
APP_PORT: "8080"
UPLOAD_DIR: /app/data/uploads
APP_VERSION: ${APP_VERSION:-dev}
volumes:
- uploads:/app/data/uploads
depends_on:
+7 -2
View File
@@ -1,6 +1,7 @@
package handlers
import (
"fmt"
"html/template"
"log"
"net/http"
@@ -9,6 +10,7 @@ import (
"shop/internal/auth"
"shop/internal/models"
"shop/internal/repository"
"shop/internal/version"
)
type CustomerHandler struct {
@@ -26,6 +28,7 @@ type shopPage struct {
Title string
User *models.User
CartCount int
Version string
Products interface{}
Categories interface{}
Error string
@@ -73,13 +76,14 @@ func NewCustomerHandler(
func (h *CustomerHandler) render(w http.ResponseWriter, name string, data any) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
if err := h.templates.ExecuteTemplate(w, name, data); err != nil {
log.Printf("render %s: %v", name, err)
}
}
func (h *CustomerHandler) basePage(r *http.Request, w http.ResponseWriter, title string) shopPage {
p := shopPage{Title: title}
p := shopPage{Title: title, Version: version.Version}
if uid, ok := auth.UserIDFromSession(h.userSession, r); ok {
if u, err := h.users.GetByID(r.Context(), uid); err == nil {
p.User = &u
@@ -373,5 +377,6 @@ func (h *CustomerHandler) Account(w http.ResponseWriter, r *http.Request) {
func (h *CustomerHandler) Health(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"status":"ok"}`))
w.Header().Set("Cache-Control", "no-store")
fmt.Fprintf(w, `{"status":"ok","version":%q}`, version.Version)
}
+1 -1
View File
@@ -118,7 +118,7 @@
<div class="footer__brand">
<span class="logo__icon"></span>
<span class="logo__text">Shop</span>
<p class="footer__copy">© 2026 Shop. Все права защищены.</p>
<p class="footer__copy">© 2026 Shop · v{{.Version}}</p>
</div>
<div class="footer__links">
<a href="/account">Личный кабинет</a>
+4
View File
@@ -0,0 +1,4 @@
package version
// Version задаётся при сборке: -ldflags "-X shop/internal/version.Version=..."
var Version = "dev"
+5 -1
View File
@@ -25,8 +25,12 @@ if [[ ! -f .env ]]; then
echo "Проверьте ADMIN_EMAIL и DOMAIN перед продакшеном."
fi
export APP_VERSION=$(git describe --tags --always 2>/dev/null || echo "dev")
export BUILD_TIME=$(date -u +%Y-%m-%dT%H:%M:%SZ)
docker compose pull --ignore-buildable 2>/dev/null || true
docker compose up --build -d
docker compose build app
docker compose up -d --force-recreate
echo ""
echo "Сервисы запущены."
+43
View File
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
# Полная пересборка и перезапуск — решает проблему «старый сайт после git pull»
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "${ROOT_DIR}"
echo "=== Shop redeploy ==="
if [[ -d .git ]]; then
git pull origin main || true
export APP_VERSION=$(git describe --tags --always 2>/dev/null || git rev-parse --short HEAD)
echo "Git: $(git log -1 --oneline)"
else
export APP_VERSION="manual"
fi
export BUILD_TIME=$(date -u +%Y-%m-%dT%H:%M:%SZ)
echo "Version: ${APP_VERSION}"
echo ""
echo "Останавливаем app..."
docker compose stop app 2>/dev/null || true
echo "Пересборка образа (без кэша)..."
docker compose build --no-cache app
echo "Запуск контейнеров..."
docker compose up -d --force-recreate app caddy
echo ""
echo "Ожидание запуска..."
sleep 3
echo ""
echo "Проверка версии:"
curl -sf "http://127.0.0.1/health" 2>/dev/null || curl -sf "http://localhost/health" || docker compose exec -T app wget -qO- http://127.0.0.1:8080/health
echo ""
echo ""
IP=$(hostname -I | awk '{print $1}')
echo "Готово: http://${IP}"
echo "Логи: docker compose logs -f app"
+11 -1
View File
@@ -5,7 +5,17 @@ ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "${ROOT_DIR}"
git pull origin main
docker compose up --build -d
export APP_VERSION=$(git describe --tags --always 2>/dev/null || git rev-parse --short HEAD)
export BUILD_TIME=$(date -u +%Y-%m-%dT%H:%M:%SZ)
echo "Обновление до ${APP_VERSION}..."
docker compose build app
docker compose up -d --force-recreate --no-deps app caddy
docker image prune -f
sleep 2
echo "Версия на сервере:"
curl -sf http://127.0.0.1/health || curl -sf http://localhost/health || true
echo ""
echo "Обновление завершено."