Browse Source

chore(staging): add auto-deploy watcher scripts for habib-shop staging

main
Ali Alavi 1 week ago
parent
commit
89a4ecbd13
  1. 1
      .gitignore
  2. 67
      auto_deploy_staging.sh
  3. 12
      watch_master_and_deploy.sh

1
.gitignore

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

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

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