-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployService.sh
51 lines (44 loc) · 1.37 KB
/
deployService.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
while getopts k:h:s: flag
do
case "${flag}" in
k) key=${OPTARG};;
h) hostname=${OPTARG};;
s) service=${OPTARG};;
esac
done
if [[ -z "$key" || -z "$hostname" || -z "$service" ]]; then
printf "\nMissing required parameter.\n"
printf " syntax: deployService.sh -k <pem key file> -h <hostname> -s <service>\n\n"
exit 1
fi
printf "\n----> Deploying React bundle $service to $hostname with $key\n"
# Step 1
printf "\n----> Build the distribution package\n"
rm -rf build
mkdir build
npm install # make sure vite is installed so that we can bundle
npm run build # build the React front end
cp -rf dist build/public # move the React front end to the target distribution
cp service/*.js build # move the back end service to the target distribution
cp service/*.json build
# Step 2
printf "\n----> Clearing out previous distribution on the target\n"
ssh -i "$key" ubuntu@$hostname << ENDSSH
rm -rf services/${service}
mkdir -p services/${service}
ENDSSH
# Step 3
printf "\n----> Copy the distribution package to the target\n"
scp -r -i "$key" build/* ubuntu@$hostname:services/$service
# Step 4
printf "\n----> Deploy the service on the target\n"
ssh -i "$key" ubuntu@$hostname << ENDSSH
bash -i
cd services/${service}
npm install
pm2 restart ${service}
ENDSSH
# Step 5
printf "\n----> Removing local copy of the distribution package\n"
rm -rf build
rm -rf dist