-
Notifications
You must be signed in to change notification settings - Fork 14
/
deploy.sh
executable file
·111 lines (85 loc) · 2.54 KB
/
deploy.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
#!/bin/bash
ask_continue() {
echo "Continue? (y/n)"
read CONTINUE
if [[ "$CONTINUE" != "y" ]]; then
exit 1
fi
}
echo_usage() {
echo "deploy.sh production | staging"
}
if [[ "$2" != "" ]]; then
echo_usage
exit 1
fi
if [[ "$1" = "production" ]]; then
TARGET="poap.vote"
FRONTEND_BUILD_SCRIPT=build-prod
elif [[ "$1" = "staging" ]]; then
TARGET="staging.poap.vote"
FRONTEND_BUILD_SCRIPT=build-staging
else
echo_usage
exit 1
fi
echo "Will deploy to $TARGET"
ask_continue
UNIXTIME=$(date +%s)
DIRNAME="frontend_$UNIXTIME"
## BUILD FRONTEND
echo "Installing frontend depdencies"
cd frontend
npm install
cd ..
ask_continue
echo "Building frontend"
cd frontend
npm run $FRONTEND_BUILD_SCRIPT
cd ..
ask_continue
## BUILD BACKEND
echo "Installing backend dependencies"
cd backend
npm install --production
cd ..
ask_continue
## PUT UP MAINTENANCE PAGE
echo "Updating the maintenance page"
rsync -aP frontend/maintenance/* deploy@$TARGET:~/frontend_down
echo "The next action is destructive. It will put the site in maintenance mode."
echo "ARE YOU SURE?"
ask_continue
echo "** PUTTING THE SITE IN MAINTENANCE MODE **"
ssh deploy@$TARGET "rm -rfv ~/frontend_last && mv -v ~/frontend_live ~/frontend_last && mv -v ~/frontend_down ~/frontend_live"
ask_continue
## DEPLOY BACKEND
echo "Archiving current backend"
ssh deploy@$TARGET "rsync -aP ~/backend_live/* ~/backend_last"
ssh deploy@$TARGET "rsync -aP ~/backend_live/.babelrc ~/backend_last"
ssh deploy@$TARGET "rsync -aP ~/backend_live/.sequelizerc ~/backend_last"
ask_continue
echo "Pushing new backend to server"
rsync -aP backend/* --exclude=.env* deploy@$TARGET:~/backend_live
rsync -aP backend/.babelrc deploy@$TARGET:~/backend_live
rsync -aP backend/.sequelizerc deploy@$TARGET:~/backend_live
ask_continue
echo "Stopping the backend server"
ssh deploy@$TARGET "cd ~/backend_live && pwd && npm run stop-prod"
ask_continue
echo "Running database migrations"
ssh deploy@$TARGET "cd ~/backend_live && pwd && npm run migrate"
ask_continue
echo "Restarting the backend server"
ssh deploy@$TARGET "cd ~/backend_live && pwd && npm run start-prod"
ask_continue
## DEPLOY FRONTEND
echo "Copying frontend to server"
rsync -rv frontend/dist/spa/* deploy@$TARGET:~/frontend_archive/$DIRNAME
ask_continue
echo "Copying new frontend"
ssh deploy@$TARGET "cp -rv ~/frontend_archive/$DIRNAME ~/frontend_new"
ask_continue
echo "Swapping in new frontend"
ssh deploy@$TARGET "mv -v ~/frontend_live ~/frontend_down && mv -v ~/frontend_new ~/frontend_live"
echo "** DEPLOYMENT COMPLETE **"