-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.sh
185 lines (152 loc) · 3.65 KB
/
update.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
#!/bin/bash
REMOTE_NAME=origin
REMOTE_BRANCH=$(git rev-parse --abbrev-ref HEAD)
LOCAL_BRANCH=$(git rev-parse --abbrev-ref HEAD)
SLACK_WEBHOOK=YOUR_SLACK_WEBHOOK
SLACK_TITLE="Docker Deployment"
# Set the slack alert level be DEBUG | SUCCESS | ERROR | NONE
SLACK_ALERT_LEVEL="SUCCESS"
#############################################
# LOGGER
#############################################
LOG_LEVEL_DECIMAL=0
case "$SLACK_ALERT_LEVEL" in
DEBUG)
LOG_LEVEL_DECIMAL=3
;;
SUCCESS)
LOG_LEVEL_DECIMAL=2
;;
ERROR)
LOG_LEVEL_DECIMAL=1
;;
esac
_log() {
local date_time msg level slack_color
date_time=$(date +"%Y/%m/%d %H:%M:%S")
msg="$1"
level="${2-${FUNCNAME[1]}}"
echo "[$date_time][$level] $msg"
}
_log_and_notify() {
local date_time msg level slack_color
date_time=$(date +"%Y/%m/%d %H:%M:%S")
msg="$1"
level="${2-${FUNCNAME[1]}}"
case "$level" in
SUCCESS)
slack_color="#84cc16"
;;
DEBUG)
slack_color="#0ea5e9"
;;
*)
slack_color="#ef4444"
;;
esac
echo "[$date_time][$level] $msg"
local PAYLOAD="{ \"attachments\": [{ \"color\": \"$slack_color\", \"fields\": [{ \"title\": \"$SLACK_TITLE\", \"value\": \"$msg\" }]}] }"
curl -X POST --silent \
-H 'Content-type: application/json; charset=utf-8' \
--data "$PAYLOAD" \
"$SLACK_WEBHOOK" 1>/dev/null
}
function DEBUG() {
if [[ "$LOG_LEVEL_DECIMAL" -ge "3" ]]
then
_log_and_notify "$1"
else
_log "$1"
fi
}
function SUCCESS() {
if [[ "$LOG_LEVEL_DECIMAL" -ge "2" ]]
then
_log_and_notify "$1"
else
_log "$1"
fi
}
function ERROR() {
if [[ "$LOG_LEVEL_DECIMAL" -ge "1" ]]
then
_log_and_notify "$1"
else
_log "$1"
fi
}
#############################################
# UTILS
#############################################
function checkResult() {
local status=$?
local message=$1
if [ "$status" == "0" ]; then
DEBUG "$message"
else
ERROR "$message"
exit 1
fi
}
function filterDockerComposeFiles() {
local arr=()
for changedFile in $1; do
if [[ $changedFile =~ docker-compose\.ya?ml$ ]]; then
arr+=("$changedFile")
fi
done
echo arr
}
function getStatus() {
echo "$1" | cut -f 1
}
function getFileName() {
echo "$1" | cut -f 2
}
git remote update > /dev/null
checkResult "Update Remote"
if [ "$(git rev-parse HEAD)" == "$(git rev-parse @{u})" ]; then
DEBUG "No changes"
exit 0
fi
DEBUG "Changes detected"
changedFiles=$(git diff --name-status $LOCAL_BRANCH $REMOTE_NAME/$REMOTE_BRANCH)
checkResult "Update Remote"
delettedFiles=()
updatedFiles=()
while IFS= read -r changedFile; do
# Check if docker-compose file
status=$(getStatus "$changedFile")
fileName=$(getFileName "$changedFile")
if [[ $fileName =~ docker-compose\.ya?ml$ ]]; then
if [ "$status" == "D" ]; then
# File must be deleted
delettedFiles+=("$fileName")
else
updatedFiles+=("$fileName")
fi
fi
done <<< "$changedFiles"
DEBUG "Deleted files"
DEBUG "${delettedFiles[@]}"
DEBUG "Updated files"
DEBUG "${updatedFiles[@]}"
# Docker compose down files
for delettedFile in "${delettedFiles[@]}"; do
docker-compose -f "$delettedFile" down
checkResult "DOWN $delettedFile"
SUCCESS "Successfully deletted $delettedFile !"
done
git pull > /dev/null
checkResult "Pull repository"
# Docker compose up files
for updatedFile in "${updatedFiles[@]}"; do
dir=$(dirname "$updatedFile")
if [ -f "./$dir/.env.example" ] && [ ! -f "./$dir/.env" ]; then
ERROR ".env file needed for $dir, deploy stoped"
else
docker compose -f "$updatedFile" up -d
checkResult "UP $updatedFile"
SUCCESS "Successfully deploy $updatedFile !"
fi
done