-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeployment.sh
131 lines (114 loc) · 3.02 KB
/
deployment.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
#!/bin/bash
# Auto-Deployment for recapp
# Konfiguration
if [ $# -eq 0 ]; then
REPO_PATH="/home/cloud/recapp"
LOG_FILE="/home/cloud/recapp/deploy.log"
else
REPO_PATH="$1"
LOG_FILE="$1/deploy.log"
fi
PM2_PROCESS_NAME="backend"
# Funktion zum Loggen
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
echo "$1"
}
# Funktion zum Prüfen von Änderungen im Remote
check_remote_changes() {
git fetch origin
local_branch=$(git rev-parse --abbrev-ref HEAD)
remote_branch="origin/$local_branch"
if git diff --quiet "$local_branch" "$remote_branch"; then
log "No remote changes for current branch ($local_branch)."
return 1
else
log "New commits detected for current branch ($local_branch)."
return 0
fi
}
# Funktion zum Pullen von Änderungen
pull_changes() {
if git pull origin "$(git rev-parse --abbrev-ref HEAD)"; then
log "Pulling changes was successful."
return 0
else
log "Error on pulling changes."
return 1
fi
}
# Funktion zum Bauen der Projekte
build_projects() {
log "Starting rebuild"
# Installiere Abhängigkeiten
if npm ci; then
log "Installed dependencies successfully."
else
log "Error on dependent package installation."
return 1
fi
# Führe Lerna build aus
if npx lerna run build; then
log "Build all packages."
return 0
else
log "Error on building packages."
return 1
fi
}
# Funktion zum Neustarten des PM2-Prozesses
restart_pm2() {
if pm2 restart "$PM2_PROCESS_NAME"; then
log "Restartet backend."
return 0
else
log "Error on restarting the backend."
return 1
fi
}
# Funktion zum Kopieren der Frontend-Dateien und Setzen der Rechte
change_frontend_permissions() {
if chmod -R o+r ./packages/frontend/dist/*; then
log "Made frontend build accessible by webserver."
return 0
else
log "Could not change frontend access rights for webserver"
return 1
fi
}
# Funktion zum Zurücksetzen auf den letzten funktionierenden Stand
rollback() {
log "Errors occured. Rolling back."
git reset --hard HEAD~1
build_projects
restart_pm2
copy_frontend_files
log "Rollback finished."
}
# Hauptfunktion
main() {
cd "$REPO_PATH" || { log "Fehler: Konnte nicht in das Repository-Verzeichnis wechseln."; exit 1; }
if [ "$1" = "force-build" ]; then
log "Forced deployment."
if build_projects; then
log "New version was deployed"
else
log "An error occured"
exit 1
fi
return
fi
if check_remote_changes; then
if pull_changes && build_projects && restart_pm2 && change_frontend_permissions; then
log "New version was deployed"
else
log "An error occured"
rollback
exit 1
fi
else
log "No action neccessary."
fi
}
# Ausführung der Hauptfunktion
main $2