feature/05-exercise-encyclopedia #4

Merged
sphinxen merged 28 commits from feature/05-exercise-encyclopedia into main 2026-03-06 12:29:20 +01:00
5 changed files with 113 additions and 0 deletions
Showing only changes of commit 53f4df6e3c - Show all commits
+5
View File
@@ -1,5 +1,10 @@
FROM node:20-alpine
ARG GIT_COMMIT=unknown
ARG BUILD_DATE=unknown
LABEL org.opencontainers.image.revision=$GIT_COMMIT \
org.opencontainers.image.created=$BUILD_DATE
WORKDIR /app
COPY package*.json ./
+11
View File
@@ -4,6 +4,9 @@ services:
build:
context: ./backend
dockerfile: Dockerfile
args:
GIT_COMMIT: ${GIT_COMMIT:-unknown}
BUILD_DATE: ${BUILD_DATE:-unknown}
restart: unless-stopped
environment:
- DB_HOST=postgres
@@ -16,12 +19,18 @@ services:
- homelab
expose:
- "3001"
labels:
- "org.opencontainers.image.revision=${GIT_COMMIT:-unknown}"
- "org.opencontainers.image.created=${BUILD_DATE:-unknown}"
gravl-frontend:
container_name: gravl-frontend
build:
context: ./frontend
dockerfile: Dockerfile
args:
GIT_COMMIT: ${GIT_COMMIT:-unknown}
BUILD_DATE: ${BUILD_DATE:-unknown}
restart: unless-stopped
depends_on:
- gravl-backend
@@ -37,6 +46,8 @@ services:
- "traefik.http.routers.gravl-secure.tls=true"
- "traefik.http.routers.gravl-secure.service=gravl"
- "traefik.http.services.gravl.loadbalancer.server.port=80"
- "org.opencontainers.image.revision=${GIT_COMMIT:-unknown}"
- "org.opencontainers.image.created=${BUILD_DATE:-unknown}"
networks:
proxy:
+5
View File
@@ -10,6 +10,11 @@ RUN npm run build
FROM nginx:alpine
ARG GIT_COMMIT=unknown
ARG BUILD_DATE=unknown
LABEL org.opencontainers.image.revision=$GIT_COMMIT \
org.opencontainers.image.created=$BUILD_DATE
COPY --from=build /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
+41
View File
@@ -0,0 +1,41 @@
#!/bin/bash
# Compare deployed container versions against local git HEAD
# Warns if containers are stale (built from an older commit)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_DIR="$(dirname "$SCRIPT_DIR")"
cd "$REPO_DIR"
LOCAL_COMMIT=$(git rev-parse HEAD)
echo "Local HEAD: $(git rev-parse --short HEAD) ($LOCAL_COMMIT)"
echo ""
check() {
local name="$1"
if ! docker inspect "$name" &>/dev/null; then
echo "[$name] Not running"
return
fi
local commit date
commit=$(docker inspect "$name" --format '{{index .Config.Labels "org.opencontainers.image.revision"}}' 2>/dev/null)
date=$(docker inspect "$name" --format '{{index .Config.Labels "org.opencontainers.image.created"}}' 2>/dev/null)
if [ -z "$commit" ] || [ "$commit" = "unknown" ]; then
echo "[$name] WARNING: no build label found — redeploy with scripts/deploy.sh to add tracking"
return
fi
echo "[$name] Built: ${commit:0:7} on ${date:-unknown}"
if [ "$commit" = "$LOCAL_COMMIT" ]; then
echo "[$name] OK: up to date"
else
echo "[$name] STALE: container is behind local code — run scripts/deploy.sh"
fi
}
check "gravl-backend"
check "gravl-frontend"
+51
View File
@@ -0,0 +1,51 @@
#!/bin/bash
# Gravl deployment script
# Prevents stale containers by always building fresh with --no-cache
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_DIR="$(dirname "$SCRIPT_DIR")"
LOG_FILE="$REPO_DIR/logs/deploy.log"
BACKEND_HEALTH="http://localhost:3001/api/health"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"
}
mkdir -p "$REPO_DIR/logs"
cd "$REPO_DIR"
log "=== Deploy started ==="
# Pull latest code
log "Pulling latest code..."
git pull
# Capture build metadata
GIT_COMMIT=$(git rev-parse HEAD)
BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
log "Commit: $(git rev-parse --short HEAD) | Date: $BUILD_DATE"
# Build fresh images — no-cache prevents stale assets
log "Building images (--no-cache)..."
export GIT_COMMIT BUILD_DATE
docker compose build --no-cache
# Restart containers with new images
log "Starting containers..."
docker compose up -d --force-recreate
# Health check: wait up to 60s for backend
log "Health check..."
for i in $(seq 1 12); do
if curl -sf "$BACKEND_HEALTH" >/dev/null 2>&1; then
log "Backend healthy"
break
fi
[ "$i" -eq 12 ] && { log "ERROR: Health check failed after 60s"; exit 1; }
log "Waiting... ($i/12)"
sleep 5
done
log "=== Deploy complete: ${GIT_COMMIT:0:7} ==="