-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathdeploy-current.sh
114 lines (102 loc) · 1.95 KB
/
deploy-current.sh
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
#!/usr/bin/env bash
# Initialize variables
host_dir=""
git_branch=""
pm2_id=""
# Parse arguments
while getopts "d:b:i:" opt; do
case "$opt" in
d)
host_dir="$OPTARG"
;;
b)
git_branch="$OPTARG"
;;
i)
pm2_id="$OPTARG"
if ! [[ "$pm2_id" =~ ^[0-9]+$ ]]; then
echo "Error: -i must be a number(PM2 ID)"
exit 1
fi
;;
\?)
echo "Usage: $0 -d <HOST_DIR> -b <GIT_BRANCH> -i <PM2_ID>"
exit 1
;;
esac
done
# Check if both flags are provided
if [[ -z "$host_dir" || -z "$git_branch" || -z "$pm2_id" ]]; then
echo "Error: Follwing flags are required: -d -i -b"
echo
echo "Usage: $0 -d <HOST_DIR> -b <GIT_BRANCH> -i <PM2_ID>"
exit 1
fi
echo "Deployment started 🚀"
echo
# Step 1: Pull latest changes
echo "Pulling git branch $git_branch"
echo
git pull origin $git_branch
if [ $? -ne 0 ]; then
echo "failed: git pull origin $git_branch"
echo
exit 1
fi
# Step 2: Install dependencies
echo "Installing dependencies"
echo
bun i
if [ $? -ne 0 ]; then
echo "failed: bun i"
echo
exit 1
fi
# Step 3: Build project
echo "Building project"
echo
bun run build
if [ $? -ne 0 ]; then
echo "failed: bun run build"
echo
exit 1
fi
# Step 4: Stop PM2 process
echo "Stopping PM2 process with ID $pm2_id"
echo
pm2 stop $pm2_id
if [ $? -ne 0 ]; then
echo "failed: pm2 stop $pm2_id"
echo
exit 1
fi
# Step 5: Remove host_dir directory
echo "Removing $host_dir directory"
echo
rm -rf $host_dir
if [ $? -ne 0 ]; then
echo "failed: rm -rf $host_dir"
echo
exit 1
fi
# Step 6: Move build to $host_dir
echo "Moving build to $host_dir"
echo
mv build $host_dir
if [ $? -ne 0 ]; then
echo "failed: mv build $host_dir"
echo
exit 1
fi
# Step 7: Restart PM2 process
echo "Restarting PM2 process with ID $pm2_id"
echo
pm2 restart $pm2_id
if [ $? -ne 0 ]; then
echo "failed: pm2 restart $pm2_id"
echo
exit 1
fi
echo "Run follwing command to check logs: pm2 logs --lines 500 $pm2_id"
echo
echo "Deployment successful 🎉"