-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.sh
executable file
·151 lines (117 loc) · 3.84 KB
/
publish.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
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
#!/usr/bin/env bash
# set -x
exdir=$(dirname $(readlink -f "$BASH_SOURCE"))
FORCE="0"
BACKEND_SRCDIR="$exdir"/src/backend
FRONTEND_SRCDIR="$exdir"/src/frontend
APP_SSH_HOST=""
APP_SEVERNAME=""
APP_ID=""
printHelp() {
echo "$0 argmuments:"
echo " -h <sshhost> ssh host where to publish ( ie. main-test )"
echo " -sn <servername> nginx app servername ( ie. mytest.searchathing.local )"
echo " -id <appid> app identifier ( ie. mytest )"
echo " -f force overwrite existing"
}
mandatoryArg() {
if [ "$1" == "" ]; then
printHelp
exit 1
fi
}
header() {
echo "========================================================================="
echo " $1"
echo "========================================================================="
}
while [ "$1" != "" ]; do
if [ "$1" == "--help" ]; then
printHelp
exit 1
fi
if [ "$1" == "-f" ]; then
FORCE="1"
shift
continue
fi
if [ "$1" == "-h" ]; then
shift
mandatoryArg "$1"
APP_SSH_HOST=$1
shift
continue
fi
if [ "$1" == "-sn" ]; then
shift
mandatoryArg "$1"
APP_SERVERNAME=$1
shift
continue
fi
if [ "$1" == "-id" ]; then
shift
mandatoryArg "$1"
APP_ID=$1
shift
continue
fi
if [ "$1" != "" ]; then
echo "Unknown arg $1"
printHelp
exit 1
fi
done
if [ "$APP_SSH_HOST" == "" ] || [ "$APP_SERVERNAME" == "" ] || [ "$APP_ID" == "" ]; then
mandatoryArg
fi
cd "$exdir"
ssh $APP_SSH_HOST exit 100
testssh=$?
if [ "$testssh" != "100" ]; then
echo "unable to connect ssh host $APP_SSH_HOST"
exit 2
fi
# ======================================================================
header "PROD ENV"
# ======================================================================
GITCOMMIT="$(git rev-parse --short HEAD)"
GITCOMMITDATE="$(git show -s --format="%ci" $GITCOMMIT)"
# backend appsettings
PRODFILE="$BACKEND_SRCDIR"/appsettings.Production.json
PRODFILE_TMP="$BACKEND_SRCDIR"/appsettings.Production.json.tmp
cat "$PRODFILE" |
jq ".GitCommit |= \"$GITCOMMIT\"" |
jq ".GitCommitDate |= \"$GITCOMMITDATE\"" |
jq ".AppServerName |= \"$APP_SERVERNAME\"" \
>"$PRODFILE_TMP"
mv -f "$PRODFILE_TMP" "$PRODFILE"
# frontend vite env
sed -i "s/VITE_SERVERNAME=.*/VITE_SERVERNAME=$APP_SERVERNAME/g" "$FRONTEND_SRCDIR"/.env.production
sed -i "s/VITE_GITCOMMIT=.*/VITE_GITCOMMIT=$GITCOMMIT/g" "$FRONTEND_SRCDIR"/.env.production
sed -i "s/VITE_GITCOMMITDATE=.*/VITE_GITCOMMITDATE=\"$GITCOMMITDATE\"/g" "$FRONTEND_SRCDIR"/.env.production
# ======================================================================
header "PREPARE $APP_ID"
# ======================================================================
REMOTE_DEPLOY=/root/deploy/$APP_ID
ssh $APP_SSH_HOST mkdir -p "$REMOTE_DEPLOY"
rsync -arvx --delete deploy/ $APP_SSH_HOST:$REMOTE_DEPLOY/
forceargs=""
if [ "$FORCE" == "1" ]; then forceargs="-f"; fi
ssh $APP_SSH_HOST "$REMOTE_DEPLOY/scripts/prepare.sh" -sn "$APP_SERVERNAME" -id "$APP_ID" "$forceargs"
excode="$?"
if [ "$excode" == "10" ]; then
echo "some prerequisites missing"
exit 10
fi
# ======================================================================
header "BUILDING $APP_ID SERVER"
# ======================================================================
dotnet publish -c Release --runtime linux-x64 --sc
PREBUILT="$BACKEND_SRCDIR/bin/Release/net8.0/linux-x64/publish"
ssh $APP_SSH_HOST mkdir -p /srv/app/$APP_ID
rsync -arx --delete $PREBUILT/ $APP_SSH_HOST:/srv/app/$APP_ID/bin/
# ======================================================================
header "RESTARTING $APP_ID SERVER"
# ======================================================================
ssh $APP_SSH_HOST "service $APP_ID-webapp restart"