-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeploy.sh
More file actions
133 lines (111 loc) · 3.82 KB
/
deploy.sh
File metadata and controls
133 lines (111 loc) · 3.82 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
#!/bin/bash
# ============================================================
# CourseHub Deployment Script
# Usage:
# ./deploy.sh — deploy both frontend & backend
# ./deploy.sh frontend — deploy frontend only
# ./deploy.sh backend — deploy backend only
# ============================================================
set -euo pipefail
REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CLIENT_DIR="$REPO_DIR/client"
SERVER_DIR="$REPO_DIR/server"
<<<<<<< HEAD
NGINX_WEB_ROOT="/var/www/coursehub"
=======
NGINX_WEB_ROOT="/var/www/coursehub/client"
>>>>>>> f385ff68fcab0ff89d8769e012a17c4ac23973fe
PM2_APP_NAME="coursehub-backend"
# ── Colours ─────────────────────────────────────────────────
GREEN="\033[0;32m"
YELLOW="\033[1;33m"
RED="\033[0;31m"
CYAN="\033[0;36m"
BOLD="\033[1m"
RESET="\033[0m"
log() { echo -e "${CYAN}[•]${RESET} $*"; }
success() { echo -e "${GREEN}[✔]${RESET} $*"; }
warn() { echo -e "${YELLOW}[!]${RESET} $*"; }
error() { echo -e "${RED}[✘]${RESET} $*" >&2; }
header() { echo -e "\n${BOLD}${CYAN}══ $* ══${RESET}\n"; }
# ── Argument parsing ─────────────────────────────────────────
TARGET="${1:-all}" # all | frontend | backend
deploy_frontend() {
header "Deploying Frontend"
log "Pulling latest changes..."
git -C "$REPO_DIR" pull
log "Installing client dependencies..."
cd "$CLIENT_DIR"
npm install
log "Building frontend..."
npm run build
log "Replacing files in $NGINX_WEB_ROOT ..."
# Clear old build and copy new one
sudo rm -rf "${NGINX_WEB_ROOT:?}"/*
sudo cp -r "$CLIENT_DIR/dist/." "$NGINX_WEB_ROOT/"
<<<<<<< HEAD
=======
log "Reloading nginx..."
sudo systemctl reload nginx
sudo nginx -s reload
>>>>>>> f385ff68fcab0ff89d8769e012a17c4ac23973fe
success "Frontend deployed successfully!"
}
deploy_backend() {
header "Deploying Backend"
log "Pulling latest changes..."
git -C "$REPO_DIR" pull
log "Installing server dependencies..."
cd "$SERVER_DIR"
npm install
log "Restarting PM2 app: $PM2_APP_NAME ..."
pm2 restart "$PM2_APP_NAME"
success "Backend deployed successfully!"
}
# ── Main ─────────────────────────────────────────────────────
echo -e "\n${BOLD}CourseHub Deployment${RESET} — target: ${YELLOW}${TARGET}${RESET}"
START=$(date +%s)
case "$TARGET" in
frontend)
deploy_frontend
;;
backend)
deploy_backend
;;
all)
# Pull once, deploy both
header "Full Deployment"
log "Pulling latest changes..."
git -C "$REPO_DIR" pull
# Frontend
log "Installing client dependencies..."
cd "$CLIENT_DIR"
npm install
log "Building frontend..."
npm run build
log "Replacing files in $NGINX_WEB_ROOT ..."
sudo rm -rf "${NGINX_WEB_ROOT:?}"/*
sudo cp -r "$CLIENT_DIR/dist/." "$NGINX_WEB_ROOT/"
<<<<<<< HEAD
=======
log "Reloading nginx..."
sudo systemctl reload nginx
sudo nginx -s reload
>>>>>>> f385ff68fcab0ff89d8769e012a17c4ac23973fe
success "Frontend deployed!"
# Backend
log "Installing server dependencies..."
cd "$SERVER_DIR"
npm install
log "Restarting PM2 app: $PM2_APP_NAME ..."
pm2 restart "$PM2_APP_NAME"
success "Backend deployed!"
;;
*)
error "Unknown target: '$TARGET'"
echo "Usage: $0 [all|frontend|backend]"
exit 1
;;
esac
END=$(date +%s)
echo -e "\n${GREEN}${BOLD}✔ Deployment complete${RESET} in $(( END - START ))s\n"