-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path.make-support
309 lines (248 loc) · 7.68 KB
/
.make-support
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/bin/bash
log() {
local severity=${2:-}
local prefix=
local color=
local exitCode=${3:-}
# detect severity based on input string, this is not an accurate way to
# determine the real severity of the input message but can be useful when
# viewing the output on the terminal due to color highlighting
if [[ -z "$severity" ]] && [[ ! -z "${1:-}" ]]; then
if echo "$1" \
| grep -E '^[^#;]' \
| grep -qEi "fatal|crit|emerg|error|fail|invalid|illegal|can(no|')?t|impossible|abort"; then
severity='ERROR'
elif echo "$1" \
| grep -E '^[^#;]' \
| grep -qEi 'warn'; then
severity='WARN'
elif echo "$1" \
| grep -E '^[^#;]' \
| grep -qEi 'success|complete|(add|creat|updat|delet|sav|install|fetch|remove|purg)(ing|ed)|exec'; then
severity='INFO'
fi
fi
case "$severity" in
INFO)
prefix='INFO:'
color='\033[34m'
;;
WARN)
prefix='WARN:'
color='\033[33m'
;;
ERROR)
prefix='ERROR:'
color='\033[31m'
;;
FATAL)
prefix='FATAL:'
color='\033[41;39m'
if [[ -z "${exitCode:-}" ]] || [[ "${exitCode:-}" == "0" ]]; then
exitCode=255
fi
;;
DEBUG | *)
prefix='DEBUG:'
color=''
esac
if [ -z "${HOSTNAME:-}" ]; then
HOSTNAME=$(hostname 2>/dev/null || echo 'local')
fi
# print from argument
if [ ! -z "${1:-}" ]; then
# set terminal color
echo -ne "$color" >&2
# print the message
date=$(date +'%Y-%d-%m %H:%M:%S')
message="[$date] $HOSTNAME.$prefix $(echo "${1:-}" | sed -E 's/\033[[^m]+m//g')"
echo "$message" | tee -a build.log >&2
# reset terminal color
echo -ne '\033[0m' >&2
fi
# print from stdin
if [ ! -t 0 ]; then
while read -r line; do
log "$line" "${2:-}" "${3:-}"
done
fi
# exit if exit code is set
if [ "${exitCode:-}" != "" ]; then
exit "$exitCode"
fi
}
debug() {
log "${1:-}" 'DEBUG' "${2:-}"
}
info() {
log "${1:-}" 'INFO' "${2:-}"
}
warn() {
log "${1:-}" 'WARN' "${2:-}"
}
error() {
log "${1:-}" 'ERROR' "${2:-}"
}
fatal() {
log "${1:-}" 'FATAL' "${2:-}"
}
dockerBuild() {
versions=$(ls -1 build*.properties 2>/dev/null | sort -V)
if [ -z "$versions" ]; then
createBuildProperties
versions=$(ls -1 build*.properties 2>/dev/null | sort -V)
if [ -z "$versions" ]; then
fatal 'no build.properties file(s)'
fi
else
debug 'using existing build.properties file(s)'
fi
setEnvironment
local latest=$(echo "$versions" | tail -n1)
local isLatest
for properties in $versions; do
if [ "$properties" = "$latest" ]; then
isLatest=1
else
isLatest=0
fi
setEnvironment "$properties"
local args=$(getBuildArgs "$properties")
local tags=$(getBuildTags "$isLatest")
local cacheFrom="${IMAGE}:${LABEL_VERSION}"
# ignore errors on pull, the image may not have been pushed if it's new
if ! docker pull "$cacheFrom" 2>&1 | debug; then
cacheFrom="${IMAGE}:latest"
if ! docker pull "$cacheFrom" 2>&1 | debug; then
cacheFrom=
fi
fi
if [ ! -z "$cacheFrom" ]; then
cacheFrom="--cache-from $cacheFrom"
fi
command="$(cat <<EOF
docker build \
$cacheFrom \
$args \
--build-arg "LABEL_NAME=$NAME" \
--build-arg "LABEL_USAGE=https://$VCS_HOST/$VENDOR/$VCS_NAME/tree/master/$NAME" \
--build-arg "LABEL_VENDOR=$VENDOR" \
--build-arg "LABEL_VCS_URL=$VCS_URL" \
--build-arg "LABEL_VCS_REF=$VCS_REF" \
$tags \
"$DOCKER_BUILD_CONTEXT" \
-f "$DOCKER_FILE_PATH"
EOF
)"
debug "Build Command: $command"
eval $command 2>&1 | log
exitCode=${PIPESTATUS[0]}
if [ ${exitCode} -ne 0 ]; then
fatal 'build error' ${exitCode}
fi
done
}
dockerPush() {
local versions=$(ls -1 build*.properties | sort -V)
local latest=$(echo "$versions" | tail -n1)
local version
for properties in $versions; do
setEnvironment "$properties"
docker push "$IMAGE:$LABEL_VERSION"
if [ "${TAG_MAJOR:-false}" = "true" ]; then
docker push "$IMAGE:$(echo "$LABEL_VERSION" | sed -E 's/^([^.]+).*/\1/')"
fi
if [ "${TAG_MINOR:-false}" = "true" ]; then
docker push "$IMAGE:$(echo "$LABEL_VERSION" | sed -E 's/^([^.]+\.[^.]+).*/\1/')"
fi
if [ "$properties" = "$latest" ]; then
docker push "$IMAGE:latest"
fi
done
updateDescription
}
updateDescription() {
if [ ! -f README.md ]; then
debug 'skipping description update no README.md'
return
fi
debug 'updating README.md'
if [ -z "${DOCKER_HUB_TOKEN:-}" ]; then
error 'DOCKER_HUB_TOKEN is not set'
return
fi
setEnvironment
# send the api request to update the description
echo -n '{"full_description": '"$(jq -Rs . README.md)"'}' \
| curl -sSL "https://hub.docker.com/v2/repositories/$VENDOR/$NAME/" \
-d @- -X PATCH \
-H "Content-Type: application/json" \
-H "Authorization: JWT ${DOCKER_HUB_TOKEN}" 2>&1 \
| sed -E 's/JWT .+/JWT **redacted**/' \
| debug
}
setEnvironment() {
# registry defaults
REGISTRY_HOST=${REGISTRY_HOST:-docker.io}
VENDOR=${VENDOR:-$(whoami)}
# image defaults
NAME=${NAME:-$(basename "$(pwd)")}
IMAGE=${IMAGE:-$REGISTRY_HOST/$VENDOR/$NAME}
# dockerfile defaults
DOCKER_BUILD_CONTEXT=${DOCKER_BUILD_CONTEXT:-.}
DOCKER_FILE_PATH=${DOCKER_FILE_PATH:-Dockerfile}
# vcs defaults
VCS_NAME=${VCS_NAME:-$(basename "$(dirname "${BASH_SOURCE[0]}")")}
VCS_HOST=${VCS_HOST:-github.com}
VCS_URL=${VCS_URL:-https://$VCS_HOST/$VENDOR/$VCS_NAME}
VCS_REF=${VCS_REF:-$(getVcsRef)}
setVersion "${1:-}"
}
setVersion() {
local properties=${1:-build.properties}
if [ -f "$properties" ]; then
LABEL_VERSION=$(sed -nE 's/^LABEL_VERSION=(.+)/\1/p' "$properties" | sed -E 's/"//g' | sed -E "s/'//g")
fi
}
getBuildArgs() {
local properties=${1:-build.properties} && . "$properties"
if [ -f "$properties" ]; then
grep . "$properties" | sed -E 's/(.+)/--build-arg \1/' | tr $'\n' ' '
fi
}
getBuildTags() {
echo " -t ${IMAGE}:${LABEL_VERSION}"
if [ "${TAG_MAJOR:-false}" = "true" ]; then
local version=$(echo "$LABEL_VERSION" | sed -E 's/^([^.]+).*/\1/')
echo " -t ${IMAGE}:${version}"
fi
if [ "${TAG_MINOR:-false}" = "true" ]; then
local version=$(echo "$LABEL_VERSION" | sed -E 's/^([^.]+\.[^.]+).*/\1/')
echo " -t ${IMAGE}:${version}"
fi
if [ "${1:-0}" = "1" ]; then
echo " -t ${IMAGE}:latest"
fi
}
getVcsRef() {
local exclude=
debug 'determining git ref ...'
if [ -f .dockerignore ]; then
exclude=$(cat .dockerignore | sed -E 's/(.+)/":(exclude)\1"/' | tr $'\n' ' ')
exclude="$exclude "'":(exclude).dockerignore"'
debug "git log exclude: $exclude"
else
debug 'skipping exclusions: no .dockerignore'
fi
local ref=$(eval git log --format=%h "${exclude[*]}" . | head -n1)
debug "git ref: $ref"
echo "$ref"
}
createBuildProperties() {
if [ -f configure ]; then
debug 'running ./configure ...'
./configure 2>&1 | log
else
fatal 'file not found: ./configure'
fi
}