forked from Abdulbari/sgeUpdated
111 lines
3.1 KiB
Bash
Executable File
111 lines
3.1 KiB
Bash
Executable File
#!/bin/bash
|
|
set -euo pipefail
|
|
|
|
cd /home/ubuntu/Bgreen/sgeUpdated
|
|
|
|
# -----------------------
|
|
# Harbor settings
|
|
# -----------------------
|
|
HARBOR_REGISTRY="10.150.1.166"
|
|
HARBOR_PROJECT="bgreen"
|
|
|
|
BACKEND_IMAGE_REPO="${HARBOR_REGISTRY}/${HARBOR_PROJECT}/bgreen-backend"
|
|
FRONTEND_IMAGE_REPO="${HARBOR_REGISTRY}/${HARBOR_PROJECT}/bgreen-frontend"
|
|
|
|
# Tag images with git commit (better than latest; enables rollback)
|
|
VERSION="$(git rev-parse --short HEAD)"
|
|
|
|
echo "📦 Fetching latest changes from origin/main..."
|
|
git fetch myfork main
|
|
|
|
# Detect which files changed between local HEAD and the latest remote version
|
|
CHANGED_FILES=$(git diff --name-only HEAD myfork/main || true)
|
|
|
|
if [ -z "$CHANGED_FILES" ]; then
|
|
echo "✅ No file changes detected between HEAD and origin/main."
|
|
else
|
|
echo "🪶 Changed files:"
|
|
echo "$CHANGED_FILES"
|
|
fi
|
|
|
|
# Update to the latest version
|
|
git reset --hard myfork/main
|
|
|
|
BACKEND_CHANGED=false
|
|
FRONTEND_CHANGED=false
|
|
|
|
# Check if backend folder changed (excluding README.md)
|
|
if echo "$CHANGED_FILES" | grep "^sge-backend/" | grep -qv "README.md$"; then
|
|
BACKEND_CHANGED=true
|
|
fi
|
|
|
|
# Check if frontend folder changed (excluding README.md)
|
|
if echo "$CHANGED_FILES" | grep "^sge-frontend/" | grep -qv "README.md$"; then
|
|
FRONTEND_CHANGED=true
|
|
fi
|
|
|
|
# -----------------------
|
|
# Backend section
|
|
# -----------------------
|
|
if [ "$BACKEND_CHANGED" = true ]; then
|
|
echo "⚡ Backend changes detected."
|
|
|
|
cd sge-backend
|
|
echo "Running Maven build..."
|
|
/opt/apache-maven-3.9.11/bin/mvn clean install -DskipTests
|
|
|
|
echo "🐳 Building backend Docker image..."
|
|
docker build -t "${BACKEND_IMAGE_REPO}:${VERSION}" .
|
|
|
|
echo "📤 Pushing backend image to Harbor..."
|
|
docker push "${BACKEND_IMAGE_REPO}:${VERSION}"
|
|
|
|
echo "📥 Pulling backend image from Harbor (to ensure registry is source of truth)..."
|
|
docker pull "${BACKEND_IMAGE_REPO}:${VERSION}"
|
|
|
|
cd ..
|
|
|
|
echo "🚀 Recreating backend container using Harbor image..."
|
|
VERSION="$VERSION" docker compose up -d bgreen-backend
|
|
|
|
else
|
|
echo "✅ No backend changes."
|
|
fi
|
|
|
|
# -----------------------
|
|
# Frontend section
|
|
# -----------------------
|
|
if [ "$FRONTEND_CHANGED" = true ]; then
|
|
echo "⚡ Frontend changes detected."
|
|
cd sge-frontend
|
|
|
|
# Check if package.json or package-lock.json changed
|
|
if echo "$CHANGED_FILES" | grep -qE "^sge-frontend/(package\.json|package-lock\.json)$"; then
|
|
echo "📦 package.json changed. Running 'npm install' and 'npm run build'..."
|
|
npm install
|
|
npm run build
|
|
else
|
|
echo "📦 only code changes. Running 'npm run build'..."
|
|
npm run build
|
|
fi
|
|
|
|
echo "🐳 Building frontend Docker image..."
|
|
docker build -t "${FRONTEND_IMAGE_REPO}:${VERSION}" .
|
|
|
|
echo "📤 Pushing frontend image to Harbor..."
|
|
docker push "${FRONTEND_IMAGE_REPO}:${VERSION}"
|
|
|
|
echo "📥 Pulling frontend image from Harbor (to ensure registry is source of truth)..."
|
|
docker pull "${FRONTEND_IMAGE_REPO}:${VERSION}"
|
|
|
|
cd ..
|
|
|
|
echo "🚀 Recreating frontend container using Harbor image..."
|
|
VERSION="$VERSION" docker compose up -d bgreen-frontend
|
|
|
|
else
|
|
echo "✅ No frontend changes."
|
|
fi
|
|
|
|
echo "✅ Deployment complete."
|