-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·106 lines (92 loc) · 3.34 KB
/
run.sh
File metadata and controls
executable file
·106 lines (92 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
# ============================================================
# Spring Boot Application Deployment Script
# Usage: ./run.sh [profile]
# profile: local (default) | prod
# ============================================================
APP_NAME="demo"
JAR_FILE="build/libs/${APP_NAME}.jar"
PROFILE="${1:-local}"
LOG_DIR="./logs"
PID_FILE="${LOG_DIR}/${APP_NAME}.pid"
LOG_FILE="${LOG_DIR}/${APP_NAME}.log"
JVM_OPTS="-Xms512m -Xmx1024m -XX:+UseG1GC -Dfile.encoding=UTF-8"
# Color output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
info() { echo -e "${GREEN}[INFO]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; }
step() { echo -e "${BLUE}[STEP]${NC} $1"; }
echo ""
echo "================================================"
echo " ${APP_NAME} Deployment (profile: ${PROFILE})"
echo "================================================"
# ── 1. Create log directory ───────────────────────
mkdir -p "${LOG_DIR}"
# ── 2. Check Java ─────────────────────────────────
if ! command -v java &> /dev/null; then
error "Java is not installed or not in PATH"
exit 1
fi
JAVA_MAJOR=$(java -version 2>&1 | head -1 | sed 's/.*version "\([0-9]*\).*/\1/')
if [ "${JAVA_MAJOR}" -lt 17 ] 2>/dev/null; then
error "Java 17+ required. Current: ${JAVA_MAJOR}"
exit 1
fi
info "Java ${JAVA_MAJOR} detected."
# ── 3. Build ──────────────────────────────────────
step "Building ${APP_NAME}..."
./gradlew clean bootJar -x test --quiet
if [ $? -ne 0 ]; then
error "Build failed!"
exit 1
fi
info "Build successful → ${JAR_FILE}"
# ── 4. Stop existing process ──────────────────────
if [ -f "${PID_FILE}" ]; then
OLD_PID=$(cat "${PID_FILE}")
if ps -p "${OLD_PID}" > /dev/null 2>&1; then
warn "Stopping existing process (PID: ${OLD_PID})..."
kill "${OLD_PID}"
for i in {1..15}; do
if ! ps -p "${OLD_PID}" > /dev/null 2>&1; then
break
fi
sleep 1
done
if ps -p "${OLD_PID}" > /dev/null 2>&1; then
warn "Force killing (PID: ${OLD_PID})..."
kill -9 "${OLD_PID}"
fi
info "Process stopped."
fi
rm -f "${PID_FILE}"
fi
# ── 5. Start application ──────────────────────────
step "Starting ${APP_NAME} (profile: ${PROFILE})..."
nohup java ${JVM_OPTS} \
-jar "${JAR_FILE}" \
--spring.profiles.active="${PROFILE}" \
>> "${LOG_FILE}" 2>&1 &
PID=$!
echo "${PID}" > "${PID_FILE}"
# ── 6. Verify startup ─────────────────────────────
sleep 3
if ps -p "${PID}" > /dev/null 2>&1; then
echo ""
info "✓ ${APP_NAME} started successfully"
info " PID : ${PID}"
info " Profile : ${PROFILE}"
info " Log : ${LOG_FILE}"
echo ""
else
error "Failed to start ${APP_NAME}. Last 20 lines of log:"
echo "----------------------------------------------------"
tail -n 20 "${LOG_FILE}"
echo "----------------------------------------------------"
exit 1
fi