-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.sh
executable file
·236 lines (210 loc) · 5.7 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env bash
# Defining some colors
RED='\033[0;31m'
RST='\033[0m'
CYA='\033[0;36m'
BLK='\033[5m'
YEL='\033[0;33m'
GRN='\033[0;32m'
IMP='\033[0;1;46;30m'
# Default variable
mode="auto"
# Checking for variable parameters
for opt in $@
do
if [[ $opt =~ ^--([a-z]+)=([a-zA-Z0-9.-]+)$ ]]
then
declare "${BASH_REMATCH[1]}"=${BASH_REMATCH[2]}
else
echo -e "\033[31mCannot parse argument \033[30;41m $opt \033[0"
fi
done
# Checking for mode variable
if [ -n "$mode" -a "$mode" != "auto" -a "$mode" != "force" -a "$mode" != "no" ]; then
echo -e "${RED}Ambigous mode. Accepted values are [auto,force,no]"
echo -e "${RST}Proceeding as normal mode."
fi
# Checking if Git has been initiated
if [ ! -d ./.git ]; then
echo -e "${RED}.git directory not found. Ensure your working directory has been initiated."
exit 1
fi
# Checkin if artisan command exists
php artisan > /dev/null
if [ $? -gt 0 ]; then
echo -e "${RED}Couldn't find artisan command. Ensure your working directory is a Laravel project."
exit 1
fi
# Begin
echo ""
echo -e "${CYA}#################################################################"
echo -e "##################### ${YEL}${BLK}LARAVEL APP DEPLOYMENT${CYA} ####################"
echo -e "#################################################################${RST}"
echo ""
# Activating maintenance mode
echo -e "${IMP}[ MAINTENANCE MODE ]${RST}"
php artisan down
echo ""
# Giving permissions to current user
echo -e "${IMP}[ PERMISSIONS ]${RST}"
sudo chown -R $USER:$USER ./
if [ $? -gt 0 ]; then
echo -e "${RED}An error has occured.${RST}"
exit 1
else
echo -e "${GRN}Permissions set to $USER.${RST}"
fi
echo ""
# git agent
echo -e "${IMP}[ GIT ]${RST}"
if [ "$(ssh-add -l)" = "The agent has no identities." ]; then
ssh-add ~/.ssh/id_ed25519
fi
# Reseting git
git reset --hard
if [ $? -gt 0 ]; then
echo -e "${RED}An error has occured."
exit 1
fi
echo ""
# Pulling from git repo
if [ -n $branch ]; then
#git fetch origin
#git checkout -t origin/$branch
git checkout $branch
fi
git pull
if [ $? -gt 0 ]; then
echo -e "${RED}An error has occured."
exit 1
else
echo -e "${GRN}Done!${RST}"
fi
echo ""
# Installing PHP dependencies
echo -e "${IMP}[ COMPOSER ]${RST}"
if [ "$mode" = "auto" -o "$mode" = "no" ]; then
echo -e "${YEL}Skipping PHP dependencies installation (Auto mode).${RST}"
echo ""
elif [ "$mode" = "force" ]; then
echo -e "${GRN}Installing PHP dependencies (Force mode).${RST}"
composer install
if [ $? -gt 0 ]; then
echo -e "${RED}An error has occured."
exit 1
fi
else
while true; do
read -p "Would you like to install PHP dependancies ? [Y/n] " ans
case $ans in
[Yy]* ) echo -e "${GRN}Installing PHP dependencies.${RST}"
composer install
if [ $? -gt 0 ]; then
echo -e "${RED}An error has occured."
exit 1
fi
echo ""
break;;
[Nn]* ) echo -e "${YEL}Skipping PHP dependencies installation.${RST}"; echo ""; break;;
* ) echo -e "Please answer ${GRN}Yes${RST} or ${RED}No${RST}."
esac
done
fi
# Installing NPM dependencies
echo -e "${IMP}[ NPM ]${RST}"
if [ "$mode" = "auto" -o "$mode" = "no" ]; then
echo -e "${YEL}Skipping Node.js dependencies installation (Auto mode).${RST}"
echo ""
elif [ "$mode" = "force" ]; then
echo -e "${GRN}Installing Node.js dependencies (Force mode).${RST}"
npm install
if [ $? -gt 0 ]; then
echo -e "${RED}An error has occured."
exit 1
fi
else
while true; do
read -p "Would you like to install Node.js dependancies ? [Y/n] " ans
case $ans in
[Yy]* ) echo -e "${GRN}Installing Node.js dependencies.${RST}"
npm install
if [ $? -gt 0 ]; then
echo -e "${RED}An error has occured."
exit 1
fi
echo ""
break;;
[Nn]* ) echo -e "${YEL}Skipping Node.js dependencies installation.${RST}"; echo ""; break;;
* ) echo -e "Please answer ${GRN}Yes${RST} or ${RED}No${RST}."
esac
done
fi
# Running production script
echo -e "${IMP}[ PRODUCTION SCRIPT ]${RST}"
if [ "$mode" = "auto" -o "$mode" = "force" ]; then
echo -e "${GRN}Running production script.${RST}"
npm run prod
if [ $? -gt 0 ]; then
echo -e "${RED}An error has occured."
exit 1
fi
elif [ "$mode" != "no" ]; then
while true; do
read -p "Would you like to run production script ? [Y/n] " ans
case $ans in
[Yy]* ) echo -e "${GRN}Running production script.${RST}"
npm run prod
if [ $? -gt 0 ]; then
echo -e "${RED}An error has occured."
exit 1
fi
break;;
[Nn]* ) echo -e "${YEL}Not running production script.${RST}"; break;;
* ) echo -e "Please answer ${GRN}Yes${RST} or ${RED}No${RST}."
esac
done
else
echo -e "${YEL}Not running production script.${RST}";
fi
echo ""
# Running Laravel migrations
echo -e "${IMP}[ MIGRATION ]${RST}"
if [ "$mode" = "auto" -o "$mode" = "force" ]; then
php artisan migrate --force
elif [ "$mode" != "no" ]; then
php artisan migrate
else
echo -e "${YEL}Not running migrations.${RST}";
fi
echo ""
# Clearing Laravel cache
echo -e "${IMP}[ CACHE ]${RST}"
php artisan cache:clear
echo ""
# Clearing Laravel configs
echo -e "${IMP}[ CONFIGS ]${RST}"
php artisan config:cache
echo ""
#Clearing Laravel routes
echo -e "${IMP}[ ROUTES ]${RST}"
php artisan route:cache
echo ""
# Restoring initial permissions
echo -e "${IMP}[ PERMISSIONS ]${RST}"
sudo chown -R www-data:www-data ./
if [ $? -gt 0 ]; then
echo -e "${RED}An error has occured.${RST}"
exit 1
else
echo -e "${GRN}Permissions restored!${RST}"
fi
echo ""
# Exiting maintenance mode
echo -e "${IMP}[ MAINTENANCE MODE ]${RST}"
php artisan up
echo ""
# End
echo -e "${CYA}#################################################################"
echo -e "####################### ${GRN}DEPLOYMENT DONE!${CYA} ########################"
echo -e "#################################################################${RST}"
echo ""