-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.sh
executable file
·176 lines (146 loc) · 3.65 KB
/
start.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
#!/bin/sh
# Main variables
START=$(date +%s)
LOG="build.log"
PROJECT=$(basename "$(pwd)")
VERSION=$(git log -n 1 --pretty=format:"%H" 2>/dev/null | head -c 10)
if [ "$VERSION" = "" ]; then
# Version will return YYYYmmdd, e.g. 20210209
VERSION=$(date '+%Y%m%d')
fi
IMAGE="$PROJECT:$VERSION"
# HTTP proxy if applicable
http_proxy="$http_proxy"
https_proxy="$https_proxy"
HTTP_PROXY="$HTTP_PROXY"
HTTPS_PROXY="$HTTPS_PROXY"
no_proxy="$no_proxy"
NO_PROXY="$NO_PROXY"
# Slack if applicable
SLACK_TOKEN=$SLACK_TOKEN
SLACK_CHANNELS="C02N3RC6S83"
BUILD_STATUS="PASS"
# Check root permissions
root() {
if [ "$(id -u)" != 0 ]; then
echo "Need root permissions" && echo
exit 1
fi
}
# Calculate finish time
finish() {
FINISH=$(date +%s)
COMPLETE=$((FINISH - START))
FINISH="took $((COMPLETE / 60)) minute(s) to finish"
}
# Result builder
result() {
ERRORS=$(grep 'ERRORS FOUND in' "$LOG" | awk '{ print $7 }' | tr '\n' ' ')
VULNS=$(grep 'Total:' "$LOG")
if [ "$(grep -i "non-zero code\|error response from daemon" "$LOG" 2>/dev/null)" != "" ]; then
BUILD_STATUS="FAIL"
fi
if [ "$VULNS" != "" ]; then
RESULT="Vulnerabilities $VULNS"
else
VULNS=""
fi
finish
RESULT="$BUILD_STATUS - $PROJECT - $VERSION - $FINISH
$ERRORS
$VULNS
"
}
# Make self-signed certificate
mkcert() {
openssl req -x509 -newkey \
rsa:4096 -nodes -keyout localhost.key \
-out localhost.pem -days 365 -sha256 -subj '/CN=localhost'
}
# Build commands
build() {
docker build \
--build-arg http_proxy="$http_proxy" \
--build-arg https_proxy="$https_proxy" \
--build-arg HTTP_PROXY="$HTTP_PROXY" \
--build-arg HTTPS_PROXY="$HTTPS_PROXY" \
--build-arg no_proxy="$no_proxy" \
--build-arg NO_PROXY="$NO_PROXY" \
--network=host -t "$IMAGE" . 2>&1 | tee "$LOG"
if [ "$(command -v docker-slim)" != "" ]; then
docker-slim build --http-probe-off \
--continue-after 5 --tag "$IMAGE" \
--target "$IMAGE" 2>&1 | tee -a "$LOG"
docker rm -f "$(docker ps | grep dockerslim | awk '{ print $1 }')"
rm -f slim.report.json
fi
# Remove ASCII colors from log
sed -ri "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2})?)?[mGK]//g" "$LOG"
}
# Run commands
run() {
if [ "$(command -v docker-init)" != "" ]; then
docker rm -f "$PROJECT" 2>/dev/null
docker run \
-d --net=host \
--init --restart=always \
--name="$PROJECT" "$IMAGE"
else
docker rm -f "$PROJECT" 2>/dev/null
docker run \
-d --net=host \
--restart=always \
--name="$PROJECT" "$IMAGE"
fi
}
# Clean commands
clean() {
docker system prune -af
}
# Upload build log file to Slack
slack() {
if [ "$SLACK_TOKEN" != "" ] && [ -f "$LOG" ]; then
result
if [ "$BUILD_STATUS" = "PASS" ]; then
EMOJI=":white_check_mark:"
else
EMOJI=":warning"
fi
curl -s \
-F channels="$SLACK_CHANNELS" \
-F initial_comment="$EMOJI $RESULT" \
-F file=@"$LOG" \
-F filename="$LOG" \
-H "Authorization: Bearer $SLACK_TOKEN" \
'https://slack.com/api/files.upload' | grep 'ok":false'
fi
}
# Check if argument is passed to script
if [ "$1" = "build" ]; then
root
build
exit
fi
if [ "$1" = "run" ]; then
root
run
exit
fi
if [ "$1" = "clean" ]; then
root
clean
exit
fi
if [ "$1" = "mkcert" ]; then
mkcert
exit
fi
# Set custom sequence below. Example:
root
build
slack
clean
result
cat <<EOF
$RESULT
EOF