sina_sajjadi 6 days ago
parent
commit
52f31dc79d
  1. 7
      .dockerignore
  2. 4
      .env.staging
  3. 1
      .gitignore
  4. 37
      Dockerfile
  5. 67
      auto_deploy_staging.sh
  6. 31
      docker-compose.staging.yml
  7. 77
      nginx.staging.conf
  8. 12
      watch_master_and_deploy.sh

7
.dockerignore

@ -0,0 +1,7 @@
node_modules
dist
.git
.gitignore
*.log
README.md
BACKEND_SPEC.md

4
.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

1
.gitignore

@ -24,3 +24,4 @@ dist-ssr
*.njsproj *.njsproj
*.sln *.sln
*.sw? *.sw?
.last_deployed_commit

37
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;"]

67
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

31
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

77
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";
}
}

12
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
Loading…
Cancel
Save