diff --git a/auto_deploy_staging.sh b/auto_deploy_staging.sh new file mode 100755 index 0000000..ab3777a --- /dev/null +++ b/auto_deploy_staging.sh @@ -0,0 +1,63 @@ +#!/bin/bash +set -eo pipefail + +PROJECT_DIR="/home/alialavi/projects/habib/marriage" +cd "$PROJECT_DIR" + +LOG_TAG="[Marriage-Staging-Watcher]" + +# Ensure we are on staging branch +CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) +if [ "$CURRENT_BRANCH" != "staging" ]; then + echo "$LOG_TAG ⚠️ Currently on '$CURRENT_BRANCH', switching to 'staging'..." + git checkout staging +fi + +# Fetch latest master and staging from origin +git fetch origin master staging --quiet + +# Check 1: Commits ahead on origin/master +MASTER_COMMITS=$(git rev-list --count HEAD..origin/master 2>/dev/null || echo 0) + +# Check 2: Commits ahead on origin/staging +STAGING_COMMITS=$(git rev-list --count HEAD..origin/staging 2>/dev/null || echo 0) + +NEED_BUILD=0 + +if [ "$MASTER_COMMITS" -gt 0 ]; then + echo "$LOG_TAG 🚀 Detected $MASTER_COMMITS new commit(s) on origin/master!" + git log --oneline HEAD..origin/master + echo "$LOG_TAG Merging origin/master into staging branch..." + git merge origin/master --no-edit -m "chore(staging): auto-merge origin/master into staging" + echo "$LOG_TAG Pushing updated staging branch to origin/staging..." + git push origin staging --quiet + 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 Pulling latest origin/staging..." + git merge origin/staging --no-edit || git pull origin staging --no-edit + NEED_BUILD=1 +fi + +if [ "$NEED_BUILD" -eq 0 ]; then + # Staging is completely up to date + exit 0 +fi + +# Rebuild and restart Next.js staging web container +echo "$LOG_TAG Building and restarting marriage_staging_web container..." +docker compose -f docker-compose.staging.yml up -d --build + +# Verify container health +echo "$LOG_TAG Verifying service health..." +sleep 5 +HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:5561/healthz || echo "000") + +if [ "$HTTP_STATUS" -eq 200 ]; then + echo "$LOG_TAG ✅ SUCCESS: Marriage 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/docker-compose.staging.yml b/docker-compose.staging.yml new file mode 100644 index 0000000..b8f5ce1 --- /dev/null +++ b/docker-compose.staging.yml @@ -0,0 +1,32 @@ +services: + web: + build: + context: . + dockerfile: Dockerfile + args: + NEXT_PUBLIC_API_BASE_URL: https://habib.nwhco.ir + NEXT_PUBLIC_SECURITY_KEY: t5yugymks5458fd4ghfg6h6fg + image: marriage_staging_web:latest + container_name: marriage_staging_web + restart: unless-stopped + env_file: + - .env.staging + ports: + - "127.0.0.1:5561:3000" + environment: + - NODE_ENV=production + - NEXT_PUBLIC_API_BASE_URL=https://habib.nwhco.ir + - API_BASE_URL=http://web:8000 + - NEXT_PUBLIC_SECURITY_KEY=t5yugymks5458fd4ghfg6h6fg + healthcheck: + test: ["CMD", "wget", "-qO-", "http://localhost:3000/healthz"] + interval: 30s + timeout: 5s + retries: 3 + start_period: 15s + networks: + - najm_staging_net + +networks: + najm_staging_net: + external: true diff --git a/next.config.ts b/next.config.ts index 0d9f95f..9001e6c 100644 --- a/next.config.ts +++ b/next.config.ts @@ -21,6 +21,10 @@ const nextConfig: NextConfig = { protocol: "https", hostname: "habibapp.com", }, + { + protocol: "https", + hostname: "habib.nwhco.ir", + }, ], }, diff --git a/watch_master_and_deploy.sh b/watch_master_and_deploy.sh new file mode 100755 index 0000000..65de845 --- /dev/null +++ b/watch_master_and_deploy.sh @@ -0,0 +1,12 @@ +#!/bin/bash +PROJECT_DIR="/home/alialavi/projects/habib/marriage" +cd "$PROJECT_DIR" || exit 1 + +echo "[$(date -u +"%Y-%m-%d %H:%M:%S UTC")] Starting Marriage 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