Fix deploy: force Docker rebuild, add version check and redeploy script
This commit is contained in:
+5
-1
@@ -9,7 +9,11 @@ COPY go.mod go.sum ./
|
|||||||
RUN go mod download
|
RUN go mod download
|
||||||
|
|
||||||
COPY . .
|
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
|
# Runtime stage
|
||||||
FROM alpine:3.21
|
FROM alpine:3.21
|
||||||
|
|||||||
+2
-1
@@ -16,6 +16,7 @@ import (
|
|||||||
"shop/internal/middleware"
|
"shop/internal/middleware"
|
||||||
"shop/internal/repository"
|
"shop/internal/repository"
|
||||||
"shop/internal/upload"
|
"shop/internal/upload"
|
||||||
|
"shop/internal/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -107,7 +108,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
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 {
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
log.Fatalf("listen: %v", err)
|
log.Fatalf("listen: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,9 @@ services:
|
|||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile
|
||||||
|
args:
|
||||||
|
APP_VERSION: ${APP_VERSION:-dev}
|
||||||
|
BUILD_TIME: ${BUILD_TIME:-}
|
||||||
container_name: shop-app
|
container_name: shop-app
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
env_file: .env
|
env_file: .env
|
||||||
@@ -34,6 +37,7 @@ services:
|
|||||||
DB_PORT: "5432"
|
DB_PORT: "5432"
|
||||||
APP_PORT: "8080"
|
APP_PORT: "8080"
|
||||||
UPLOAD_DIR: /app/data/uploads
|
UPLOAD_DIR: /app/data/uploads
|
||||||
|
APP_VERSION: ${APP_VERSION:-dev}
|
||||||
volumes:
|
volumes:
|
||||||
- uploads:/app/data/uploads
|
- uploads:/app/data/uploads
|
||||||
depends_on:
|
depends_on:
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -9,6 +10,7 @@ import (
|
|||||||
"shop/internal/auth"
|
"shop/internal/auth"
|
||||||
"shop/internal/models"
|
"shop/internal/models"
|
||||||
"shop/internal/repository"
|
"shop/internal/repository"
|
||||||
|
"shop/internal/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CustomerHandler struct {
|
type CustomerHandler struct {
|
||||||
@@ -26,6 +28,7 @@ type shopPage struct {
|
|||||||
Title string
|
Title string
|
||||||
User *models.User
|
User *models.User
|
||||||
CartCount int
|
CartCount int
|
||||||
|
Version string
|
||||||
Products interface{}
|
Products interface{}
|
||||||
Categories interface{}
|
Categories interface{}
|
||||||
Error string
|
Error string
|
||||||
@@ -73,13 +76,14 @@ func NewCustomerHandler(
|
|||||||
|
|
||||||
func (h *CustomerHandler) render(w http.ResponseWriter, name string, data any) {
|
func (h *CustomerHandler) render(w http.ResponseWriter, name string, data any) {
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
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 {
|
if err := h.templates.ExecuteTemplate(w, name, data); err != nil {
|
||||||
log.Printf("render %s: %v", name, err)
|
log.Printf("render %s: %v", name, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *CustomerHandler) basePage(r *http.Request, w http.ResponseWriter, title string) shopPage {
|
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 uid, ok := auth.UserIDFromSession(h.userSession, r); ok {
|
||||||
if u, err := h.users.GetByID(r.Context(), uid); err == nil {
|
if u, err := h.users.GetByID(r.Context(), uid); err == nil {
|
||||||
p.User = &u
|
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) {
|
func (h *CustomerHandler) Health(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -118,7 +118,7 @@
|
|||||||
<div class="footer__brand">
|
<div class="footer__brand">
|
||||||
<span class="logo__icon">◆</span>
|
<span class="logo__icon">◆</span>
|
||||||
<span class="logo__text">Shop</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>
|
||||||
<div class="footer__links">
|
<div class="footer__links">
|
||||||
<a href="/account">Личный кабинет</a>
|
<a href="/account">Личный кабинет</a>
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package version
|
||||||
|
|
||||||
|
// Version задаётся при сборке: -ldflags "-X shop/internal/version.Version=..."
|
||||||
|
var Version = "dev"
|
||||||
+5
-1
@@ -25,8 +25,12 @@ if [[ ! -f .env ]]; then
|
|||||||
echo "Проверьте ADMIN_EMAIL и DOMAIN перед продакшеном."
|
echo "Проверьте ADMIN_EMAIL и DOMAIN перед продакшеном."
|
||||||
fi
|
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 pull --ignore-buildable 2>/dev/null || true
|
||||||
docker compose up --build -d
|
docker compose build app
|
||||||
|
docker compose up -d --force-recreate
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
echo "Сервисы запущены."
|
echo "Сервисы запущены."
|
||||||
|
|||||||
@@ -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
@@ -5,7 +5,17 @@ ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|||||||
cd "${ROOT_DIR}"
|
cd "${ROOT_DIR}"
|
||||||
|
|
||||||
git pull origin main
|
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
|
docker image prune -f
|
||||||
|
|
||||||
|
sleep 2
|
||||||
|
echo "Версия на сервере:"
|
||||||
|
curl -sf http://127.0.0.1/health || curl -sf http://localhost/health || true
|
||||||
|
echo ""
|
||||||
echo "Обновление завершено."
|
echo "Обновление завершено."
|
||||||
|
|||||||
Reference in New Issue
Block a user