-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·166 lines (140 loc) · 4.2 KB
/
deploy.sh
File metadata and controls
executable file
·166 lines (140 loc) · 4.2 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env bash
# Production deployment script for Bloom (non-Docker)
# Deploys application to target directory and manages the running service
# Usage: ./deploy.sh [--prod] [--migration] [--restart-only]
# made w claude
REQUIRED_USER="www-data"
if [ "$(id -un)" != "$REQUIRED_USER" ]; then
echo "ERROR: This deploy script must be run as $REQUIRED_USER"
echo "Run it with: sudo -u $REQUIRED_USER $0 $*"
exit 1
fi
set -euo pipefail
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PUBLISH_DIR="$(ls -d "/var/www/bloom-build/net9.0/publish" 2>/dev/null | head -n 1)"
TARGET_DIR=""
RUN_MIGRATIONS=false
RESTART_ONLY=false
APP_PORT=5000
# --- Parse arguments ---
for arg in "$@"; do
case $arg in
--prod)
TARGET_DIR="/var/www/bloom"
;;
--migration)
RUN_MIGRATIONS=true
;;
--restart-only)
RESTART_ONLY=true
;;
*)
;;
esac
done
for tool in dotnet curl; do
command -v "$tool" &>/dev/null || {
echo "Error: $tool is required but not installed."
exit 1
}
done
# Default to dev target if none provided
if [ -z "$TARGET_DIR" ]; then
TARGET_DIR="/var/www/bloom-dev"
fi
echo "================================"
echo "Bloom Application Deployment"
echo "================================"
echo "Target directory: $TARGET_DIR"
echo ""
# --- Check if restart-only mode ---
if [ "$RESTART_ONLY" = true ]; then
echo "Restarting application in $TARGET_DIR..."
if [ ! -f "$TARGET_DIR/app.pid" ]; then
echo "No running application found (app.pid not found)"
exit 1
fi
OLD_PID=$(cat "$TARGET_DIR/app.pid")
if kill -0 "$OLD_PID" 2>/dev/null; then
echo "Stopping previous application (PID: $OLD_PID)..."
kill "$OLD_PID" || true
sleep 2
fi
rm -f "$TARGET_DIR/app.pid"
# Start the application
cd "$TARGET_DIR"
nohup dotnet Bloom.dll > "$TARGET_DIR/logs/app.log" 2>&1 &
NEW_PID=$!
echo $NEW_PID > "$TARGET_DIR/app.pid"
echo "Application started (PID: $NEW_PID)"
exit 0
fi
# --- Ensure build output exists ---
if [ -z "$PUBLISH_DIR" ]; then
echo "Error: No publish directory found!"
echo "Please run ./build.sh first to build the application."
exit 1
fi
echo "Using publish directory: $PUBLISH_DIR"
echo "Preparing deployment..."
# --- Stop existing application ---
if [ -f "$TARGET_DIR/app.pid" ]; then
OLD_PID=$(cat "$TARGET_DIR/app.pid")
if kill -0 "$OLD_PID" 2>/dev/null; then
echo "Stopping previous application (PID: $OLD_PID)..."
kill "$OLD_PID" || true
sleep 2
fi
fi
# --- Create target directory structure ---
echo "Setting up target directory: $TARGET_DIR"
mkdir -p "$TARGET_DIR"
find "$TARGET_DIR" -mindepth 1 -maxdepth 1 ! -name logs ! -name backups -exec rm -rf {} +
mkdir -p "$TARGET_DIR"
mkdir -p "$TARGET_DIR/logs"
mkdir -p "$TARGET_DIR/backups"
# --- Deploy application files ---
echo "Deploying application files..."
cp -r "$PUBLISH_DIR/"* "$TARGET_DIR/"
# --- Run migrations if requested ---
if [ "$RUN_MIGRATIONS" = true ]; then
echo "Running database migrations..."
cd "$TARGET_DIR"
if dotnet Bloom.dll --run-migrations; then
echo "Migrations completed successfully."
else
echo "Warning: Migrations may have failed. Check logs."
fi
fi
# --- Start the application ---
echo "Starting Bloom application..."
cd "$TARGET_DIR"
nohup dotnet bloom.dll > "$TARGET_DIR/logs/app.log" 2>&1 &
APP_PID=$!
echo $APP_PID > "$TARGET_DIR/app.pid"
echo "Application started with PID: $APP_PID"
# --- Wait for application to be ready ---
echo "Waiting for application to become ready..."
ATTEMPTS=0
until curl -s http://localhost:$APP_PORT/health &> /dev/null; do
sleep 2
((ATTEMPTS++))
if [ "$ATTEMPTS" -gt 30 ]; then
echo "Warning: Application may not have started properly."
echo "Check logs at: $TARGET_DIR/logs/app.log"
break
fi
done
# --- Deployment summary ---
echo ""
echo "================================"
echo "Deployment Complete"
echo "================================"
echo "Target directory: $TARGET_DIR"
echo "Application PID: $APP_PID"
echo "Logs: $TARGET_DIR/logs/app.log"
echo ""
echo "To restart: ./deploy.sh $([ "$TARGET_DIR" = "/var/www/bloom" ] && echo "--prod" || echo "") --restart-only"
echo "To stop: kill $APP_PID"
echo "================================"