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