From 862e8ca8460d4a0dd0f0c183a501ece2aed0ef62 Mon Sep 17 00:00:00 2001 From: Ali Alavi Date: Thu, 12 Nov 2026 08:07:53 +0000 Subject: [PATCH 1/2] feat(staging): add dockerfile, staging compose, and nginx internal proxy for habib shop --- .dockerignore | 7 ++++ .env.staging | 4 ++ Dockerfile | 37 ++++++++++++++++++ docker-compose.staging.yml | 31 +++++++++++++++ nginx.staging.conf | 77 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 156 insertions(+) create mode 100644 .dockerignore create mode 100644 .env.staging create mode 100644 Dockerfile create mode 100644 docker-compose.staging.yml create mode 100644 nginx.staging.conf diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..33b10e4 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +node_modules +dist +.git +.gitignore +*.log +README.md +BACKEND_SPEC.md diff --git a/.env.staging b/.env.staging new file mode 100644 index 0000000..fe4f015 --- /dev/null +++ b/.env.staging @@ -0,0 +1,4 @@ +VITE_USE_MOCK_DATA=false +VITE_API_BASE_URL=/api/shop +VITE_MEDIA_ORIGIN= +VITE_BACKEND_ORIGIN=http://najm_staging_web:8000 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b540ef8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,37 @@ +# syntax=docker/dockerfile:1.7 + +FROM node:22-alpine AS deps +RUN apk add --no-cache libc6-compat +WORKDIR /app + +COPY package.json package-lock.json* ./ +RUN npm install + +FROM node:22-alpine AS builder +WORKDIR /app + +COPY --from=deps /app/node_modules ./node_modules +COPY . . + +ARG VITE_USE_MOCK_DATA=false +ARG VITE_API_BASE_URL=/api/shop +ARG VITE_MEDIA_ORIGIN= +ENV VITE_USE_MOCK_DATA=$VITE_USE_MOCK_DATA +ENV VITE_API_BASE_URL=$VITE_API_BASE_URL +ENV VITE_MEDIA_ORIGIN=$VITE_MEDIA_ORIGIN +ENV NODE_ENV=production + +RUN npm run build + +FROM nginx:1.27-alpine AS runner +RUN apk add --no-cache wget + +COPY nginx.staging.conf /etc/nginx/conf.d/default.conf +COPY --from=builder /app/dist /usr/share/nginx/html + +EXPOSE 3000 + +HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \ + CMD wget -qO- http://127.0.0.1:3000/healthz || exit 1 + +CMD ["nginx", "-g", "daemon off;"] diff --git a/docker-compose.staging.yml b/docker-compose.staging.yml new file mode 100644 index 0000000..76dcdfe --- /dev/null +++ b/docker-compose.staging.yml @@ -0,0 +1,31 @@ +services: + web: + build: + context: . + dockerfile: Dockerfile + args: + VITE_USE_MOCK_DATA: "false" + VITE_API_BASE_URL: "/api/shop" + VITE_MEDIA_ORIGIN: "" + image: shop_staging_web:latest + container_name: shop_staging_web + restart: unless-stopped + env_file: + - .env.staging + ports: + - "127.0.0.1:5562:3000" + environment: + - NODE_ENV=production + - API_BASE_URL=http://najm_staging_web:8000 + healthcheck: + test: ["CMD", "wget", "-qO-", "http://127.0.0.1:3000/healthz"] + interval: 30s + timeout: 5s + retries: 3 + start_period: 10s + networks: + - najm_staging_net + +networks: + najm_staging_net: + external: true diff --git a/nginx.staging.conf b/nginx.staging.conf new file mode 100644 index 0000000..4163fd0 --- /dev/null +++ b/nginx.staging.conf @@ -0,0 +1,77 @@ +server { + listen 3000; + server_name _; + + root /usr/share/nginx/html; + index index.html; + + client_max_body_size 100M; + + # Docker internal DNS resolver so upstream container IPs re-resolve dynamically + resolver 127.0.0.11 valid=10s ipv6=off; + set $backend_upstream http://najm_staging_web:8000; + + gzip on; + gzip_vary on; + gzip_proxied any; + gzip_comp_level 6; + gzip_types text/plain text/css text/xml application/json application/javascript image/svg+xml; + + location = /healthz { + access_log off; + default_type application/json; + return 200 '{"status":"ok","service":"habib-shop-staging"}'; + } + + location /api/shop/ { + proxy_pass $backend_upstream; + proxy_http_version 1.1; + proxy_set_header Host habib.nwhco.ir; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_read_timeout 300s; + } + + location /upload-tmp-media/ { + proxy_pass $backend_upstream; + proxy_http_version 1.1; + proxy_set_header Host habib.nwhco.ir; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_read_timeout 300s; + } + + location /habcoin/ { + proxy_pass $backend_upstream; + proxy_http_version 1.1; + proxy_set_header Host habib.nwhco.ir; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_read_timeout 300s; + } + + location /media/ { + proxy_pass $backend_upstream; + proxy_http_version 1.1; + proxy_set_header Host habib.nwhco.ir; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + expires 30d; + add_header Cache-Control "public, immutable"; + } + + location /assets/ { + try_files $uri =404; + expires 30d; + add_header Cache-Control "public, max-age=2592000, immutable"; + } + + location / { + try_files $uri $uri/ /index.html; + add_header Cache-Control "no-cache"; + } +} From 89a4ecbd13f16bffb7a0fd39b2d124f52bec36cb Mon Sep 17 00:00:00 2001 From: Ali Alavi Date: Thu, 12 Nov 2026 08:42:13 +0000 Subject: [PATCH 2/2] chore(staging): add auto-deploy watcher scripts for habib-shop staging --- .gitignore | 1 + auto_deploy_staging.sh | 67 ++++++++++++++++++++++++++++++++++++++ watch_master_and_deploy.sh | 12 +++++++ 3 files changed, 80 insertions(+) create mode 100755 auto_deploy_staging.sh create mode 100755 watch_master_and_deploy.sh diff --git a/.gitignore b/.gitignore index 9fa51f3..3f7048d 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,4 @@ dist-ssr *.njsproj *.sln *.sw? +.last_deployed_commit diff --git a/auto_deploy_staging.sh b/auto_deploy_staging.sh new file mode 100755 index 0000000..7c035cc --- /dev/null +++ b/auto_deploy_staging.sh @@ -0,0 +1,67 @@ +#!/bin/bash +set -eo pipefail + +PROJECT_DIR="/home/alialavi/projects/habib/shop" +cd "$PROJECT_DIR" + +LOG_TAG="[Shop-Staging-Watcher]" + +# Ensure we are on main or staging branch +CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) +if [ "$CURRENT_BRANCH" != "main" ] && [ "$CURRENT_BRANCH" != "staging" ]; then + echo "$LOG_TAG ⚠️ Currently on '$CURRENT_BRANCH', switching to 'main'..." + git checkout main +fi + +# Fetch latest remote branches with timeout so transient network issues don't hang forever +timeout 45 git fetch origin --quiet + +MAIN_COMMITS=$(git rev-list --count HEAD..origin/main 2>/dev/null || echo 0) +STAGING_COMMITS=0 +if git show-ref --verify --quiet refs/remotes/origin/staging; then + STAGING_COMMITS=$(git rev-list --count HEAD..origin/staging 2>/dev/null || echo 0) +fi + +DEPLOYED_COMMIT_FILE="$PROJECT_DIR/.last_deployed_commit" +LAST_DEPLOYED=$(cat "$DEPLOYED_COMMIT_FILE" 2>/dev/null || echo "") +CURRENT_HEAD=$(git rev-parse HEAD) + +NEED_BUILD=0 + +if [ "$CURRENT_HEAD" != "$LAST_DEPLOYED" ]; then + NEED_BUILD=1 +fi + +if [ "$MAIN_COMMITS" -gt 0 ]; then + echo "$LOG_TAG 🚀 Detected $MAIN_COMMITS new commit(s) on origin/main!" + git log --oneline HEAD..origin/main + echo "$LOG_TAG Merging origin/main..." + git merge origin/main --no-edit || git pull origin main --no-edit + NEED_BUILD=1 +fi + +if [ "$STAGING_COMMITS" -gt 0 ]; then + echo "$LOG_TAG 🚀 Detected $STAGING_COMMITS new commit(s) on origin/staging!" + git log --oneline HEAD..origin/staging + echo "$LOG_TAG Merging origin/staging..." + git merge origin/staging --no-edit || git pull origin staging --no-edit + NEED_BUILD=1 +fi + +if [ "$NEED_BUILD" -eq 0 ]; then + exit 0 +fi + +echo "$LOG_TAG Building and restarting shop_staging_web container..." +docker compose -f docker-compose.staging.yml up -d --build + +echo "$LOG_TAG Verifying service health..." +sleep 4 +HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:5562/healthz || echo "000") + +if [ "$HTTP_STATUS" -eq 200 ]; then + echo "$(git rev-parse HEAD)" > "$DEPLOYED_COMMIT_FILE" + echo "$LOG_TAG ✅ SUCCESS: Habib Shop staging updated and healthy at commit $(git rev-parse --short HEAD) (HTTP 200 OK)." +else + echo "$LOG_TAG ⚠️ Health probe returned HTTP status $HTTP_STATUS" +fi diff --git a/watch_master_and_deploy.sh b/watch_master_and_deploy.sh new file mode 100755 index 0000000..c53a17a --- /dev/null +++ b/watch_master_and_deploy.sh @@ -0,0 +1,12 @@ +#!/bin/bash +PROJECT_DIR="/home/alialavi/projects/habib/shop" +cd "$PROJECT_DIR" || exit 1 + +echo "[$(date -u +"%Y-%m-%d %H:%M:%S UTC")] Starting Habib Shop Staging Auto-Deploy Watcher (interval: 30s)..." + +while true; do + "$PROJECT_DIR/auto_deploy_staging.sh" || { + echo "[$(date -u +"%Y-%m-%d %H:%M:%S UTC")] Auto-deploy encountered an error, will retry in next cycle." + } + sleep 30 +done