-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·293 lines (250 loc) · 9.05 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·293 lines (250 loc) · 9.05 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/bin/bash
#===============================================================================
# VisuaLex Deploy Script
#
# Usage: ./deploy.sh [OPTIONS]
#
# Options:
# --major Increment major version (1.0.0 -> 2.0.0)
# --minor Increment minor version (1.0.0 -> 1.1.0)
# --patch Increment patch version (1.0.0 -> 1.0.1)
# --no-pull Skip git pull
# --no-restart Skip service restart
# -h, --help Show this help message
#
# Example:
# ./deploy.sh --patch # Build and bump patch version
# ./deploy.sh --minor --no-pull # Build without pull, bump minor
#===============================================================================
set -e # Exit on error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
VERSION_FILE="$SCRIPT_DIR/version.txt"
# Default options
DO_PULL=true
DO_RESTART=true
VERSION_BUMP=""
#===============================================================================
# Functions
#===============================================================================
print_header() {
echo -e "${BLUE}"
echo "╔═══════════════════════════════════════════════════════════╗"
echo "║ VisuaLex Deploy Script ║"
echo "╚═══════════════════════════════════════════════════════════╝"
echo -e "${NC}"
}
print_step() {
echo -e "${BLUE}▶${NC} $1"
}
print_success() {
echo -e "${GREEN}✓${NC} $1"
}
print_warning() {
echo -e "${YELLOW}⚠${NC} $1"
}
print_error() {
echo -e "${RED}✗${NC} $1"
}
show_help() {
cat << 'EOF'
Usage: ./deploy.sh [OPTIONS]
Options:
--major Increment major version (1.0.0 -> 2.0.0)
--minor Increment minor version (1.0.0 -> 1.1.0)
--patch Increment patch version (1.0.0 -> 1.0.1)
--no-pull Skip git pull
--no-restart Skip service restart
-h, --help Show this help message
Examples:
./deploy.sh --patch # Build and bump patch version
./deploy.sh --minor --no-pull # Build without pull, bump minor
./deploy.sh # Build only, no version bump
EOF
exit 0
}
get_current_version() {
if [[ -f "$VERSION_FILE" ]]; then
cat "$VERSION_FILE" | tr -d '[:space:]'
else
echo "1.0.0"
fi
}
bump_version() {
local version="$1"
local bump_type="$2"
# Parse version
IFS='.' read -r major minor patch <<< "$version"
case "$bump_type" in
major)
major=$((major + 1))
minor=0
patch=0
;;
minor)
minor=$((minor + 1))
patch=0
;;
patch)
patch=$((patch + 1))
;;
esac
echo "${major}.${minor}.${patch}"
}
update_version_file() {
local new_version="$1"
echo "$new_version" > "$VERSION_FILE"
}
#===============================================================================
# Parse Arguments
#===============================================================================
while [[ $# -gt 0 ]]; do
case $1 in
--major)
VERSION_BUMP="major"
shift
;;
--minor)
VERSION_BUMP="minor"
shift
;;
--patch)
VERSION_BUMP="patch"
shift
;;
--no-pull)
DO_PULL=false
shift
;;
--no-restart)
DO_RESTART=false
shift
;;
-h|--help)
show_help
;;
*)
print_error "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
#===============================================================================
# Main Script
#===============================================================================
print_header
CURRENT_VERSION=$(get_current_version)
echo -e "Current version: ${YELLOW}${CURRENT_VERSION}${NC}"
if [[ -n "$VERSION_BUMP" ]]; then
NEW_VERSION=$(bump_version "$CURRENT_VERSION" "$VERSION_BUMP")
echo -e "New version: ${GREEN}${NEW_VERSION}${NC} (${VERSION_BUMP})"
fi
echo ""
# Step 1: Git Pull
if [[ "$DO_PULL" == true ]]; then
print_step "Pulling latest changes..."
cd "$SCRIPT_DIR"
git pull -r origin "$(git branch --show-current)"
print_success "Git pull completed"
else
print_warning "Skipping git pull (--no-pull)"
fi
# Step 2: Python API dependencies + Playwright browser
print_step "Installing Python API dependencies..."
VENV_PIP="$SCRIPT_DIR/.venv/bin/pip"
VENV_PLAYWRIGHT="$SCRIPT_DIR/.venv/bin/playwright"
if [[ -x "$VENV_PIP" ]]; then
"$VENV_PIP" install --quiet -r "$SCRIPT_DIR/requirements.txt"
print_success "Python API dependencies installed"
else
print_error "Python venv not found at $SCRIPT_DIR/.venv — create it with 'python -m venv .venv && .venv/bin/pip install -r requirements.txt'"
exit 1
fi
# Ensure Playwright Chromium browser matches the installed package version
# (pip install does not download browser binaries; PDF export and date
# completion require chromium to be present in the Playwright cache).
if [[ -x "$VENV_PLAYWRIGHT" ]]; then
print_step "Syncing Playwright Chromium browser..."
"$VENV_PLAYWRIGHT" install chromium > /dev/null 2>&1 \
&& print_success "Playwright Chromium ready" \
|| print_warning "Playwright install failed — PDF export and date completion may break. Run '.venv/bin/playwright install chromium' manually."
fi
# Step 3: Backend dependencies
print_step "Installing backend dependencies..."
cd "$SCRIPT_DIR/backend"
npm install --silent
print_success "Backend dependencies installed"
# Step 3b: Regenerate Prisma client to match the current schema.
# Without this, schema changes pulled from git leave node_modules/@prisma/client
# stale and `tsc --noEmit` fails on missing models/fields.
print_step "Regenerating Prisma client..."
npx prisma generate > /dev/null
print_success "Prisma client regenerated"
# Step 3c: Apply pending migrations to the production database.
# Idempotent — no-op if there are no pending migrations. Without this, a
# schema change ships to prod with no matching DB column/table and the API
# fails at runtime on the first query.
print_step "Applying database migrations..."
npx prisma migrate deploy
print_success "Database migrations applied"
# Step 4: Frontend dependencies
print_step "Installing frontend dependencies..."
cd "$SCRIPT_DIR/frontend"
npm install --silent
print_success "Frontend dependencies installed"
# Step 5: Frontend build
print_step "Building frontend..."
npm run build
print_success "Frontend build completed"
# Step 6: Compile backend TypeScript to dist/.
# pm2 launches `node dist/index.js` (see backend/package.json `start` script),
# so without this step the service runs against a stale dist on every deploy.
# `tsc` performs the type-check too — it fails on any error before emitting.
print_step "Building backend..."
cd "$SCRIPT_DIR/backend"
npm run build
print_success "Backend build completed"
# Step 7: Update version (only if build succeeded and bump requested)
if [[ -n "$VERSION_BUMP" ]]; then
print_step "Updating version to ${NEW_VERSION}..."
update_version_file "$NEW_VERSION"
print_success "Version updated to ${NEW_VERSION}"
# Commit version change
cd "$SCRIPT_DIR"
git add version.txt
git commit -m "chore: bump version to ${NEW_VERSION}
🤖 Generated with [Claude Code](https://claude.com/claude-code)" || true
print_success "Version commit created"
fi
# Step 8: Restart services
if [[ "$DO_RESTART" == true ]]; then
print_step "Restarting services..."
# Try pm2 first
if command -v pm2 &> /dev/null; then
pm2 restart all 2>/dev/null || print_warning "pm2 restart failed or no processes"
print_success "Services restarted (pm2)"
# Try systemctl
elif command -v systemctl &> /dev/null; then
sudo systemctl restart visualex-backend 2>/dev/null || print_warning "systemctl restart failed"
print_success "Services restarted (systemctl)"
else
print_warning "No service manager found (pm2/systemctl). Please restart manually."
fi
else
print_warning "Skipping service restart (--no-restart)"
fi
# Done
echo ""
echo -e "${GREEN}╔═══════════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ Deploy completed! ║${NC}"
echo -e "${GREEN}╚═══════════════════════════════════════════════════════════╝${NC}"
FINAL_VERSION=$(get_current_version)
echo -e "Version: ${GREEN}${FINAL_VERSION}${NC}"
echo ""