-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild
executable file
·612 lines (517 loc) · 14.5 KB
/
build
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#!/bin/bash
# docker run --rm --privileged docker/binfmt:a7996909642ee92942dcd6cff44b9b95f08dad64
# ll /proc/sys/fs/binfmt_misc/qemu-*
# buildx create --name builder --driver docker-container --driver-opt image=moby/buildkit,network=host --use
# docker buildx inspect --bootstrap
# ==============================================================================
abspath() {
# macOS下没有 realpath
# REF: https://stackoverflow.com/questions/3572030/bash-script-absolute-path-with-os-x/3572105#3572105
which realpath 1>/dev/null 2>&1 && realpath $1 || {
[[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
}
}
# ==============================================================================
SCRIPT_DIR=$(abspath $(dirname $0))
# 默认构建镜像优先顺序
: ${DOCKER_BUILD_PRIORITY:="alpine ubuntu ssh python go node php php/fpm flask httpd nginx git"}
# 默认构架镜像的CUP架构
: ${DOCKER_BUILD_ARCHS:="amd64 arm64 arm/v7 arm/v6"}
echo_red() {
echo -e "\033[31m$@\033[0m"
}
echo_green() {
echo -e "\033[32m$@\033[0m"
}
echo_split() {
local start=
local splitline="================================================="
if [[ -n "$@" ]]; then
echo "$splitline"
echo "\n$@"
start="\n"
fi
echo "$start$splitline"
}
echo_red_split() {
echo_red $(echo_split $@)
}
echo_green_split() {
echo_green $(echo_split $@)
}
echo_err() {
echo_red $@ >&2
}
exit_err() {
[[ -n "$@" ]] && echo_err "$@\n"
usage >&2
exit 1
}
exit_ok() {
[[ -n "$@" ]] && echo_green $@
exit 0
}
log_error() {
echo "$1" >>$ERR_LOG_FILE
echo_red "$1"
}
is_true() {
[[ "$1" == "true" || "$1" == "yes" ]] && return 0 || return 1
}
# is_verbose [LEVEL]
is_verbose() {
local level=${1:-1}
[ "$CMD_VERBOSE" -ge $level ]
}
is_fail() {
[[ $? -ne 0 ]]
}
is_ignored() {
is_true $CMD_FORCE && return 1
local type=$1
local image=$2
local suite=$3
cat $BASE_DIR/.buildignore 2>/dev/null | grep -q -e "^$type,$image\$" -e "^$type,$image:$suite\$"
}
is_build_ignored() {
is_ignored B $1 $2
}
is_push_ignored() {
is_ignored P $1 $2
}
get_local_tag() {
local image=$1
local suite=$2
local tag=$image
[[ -n "$suite" && "$suite" != "latest" ]] && tag="$image:$suite"
[[ -n "$USER" ]] && tag="$USER/$tag"
echo $tag
}
get_local_arch() {
local arch=$(uname -m)
case "$arch" in
i386 )
echo "386"
;;
x86_64 )
echo "amd64"
;;
arm|armv7* )
echo "arm"
;;
aarch64*|armv8* )
echo "arm64"
;;
*)
echo "unknown"
;;
esac
}
get_local_platform() {
echo "linux/$(get_local_arch)"
}
# 查找包含Dockerfile或Dockerfile.$ARCH的目录
# $1 要查找的路径 默认: .
# $2 查找最大层 默认: 3
find_dirs() {
local path=$1
local depth=$2
# find "${path:=.}" -maxdepth ${depth:=2} ! -ipath '.\/[~\._]*' \( -name "Dockerfile" -o -name "Dockerfile.*" -o -name "docker-bake.hcl" \) | sort
IFS=$'\n'
local dirs=$(
find "${path:=.}" -maxdepth ${depth:=3} \
! -ipath '.\/[~\._]*' \
\( -name "Dockerfile" -o -name "Dockerfile.*" -o -name "docker-bake.hcl" \) | \
sed 's|\.\/||' | \
xargs -n1 dirname 2>/dev/null | \
sort -u
)
unset IFS
echo $dirs
}
run_cmd() {
local cmd=$@
[ -z "$cmd" ] && cmd=$LAST_CMD_RUNNED
LAST_CMD_RUNNED=$cmd
is_true $CMD_DRY_RUN && echo "$cmd" && return 0
is_verbose && echo_green "RUN CMD: $cmd"
$cmd
}
retry_if_failed() {
is_fail || return 0
local n=1
until [ $n -gt $FAILED_RETRY_MAX ]; do
echo
echo_red_split "Failed, retry #$n/$FAILED_RETRY_MAX after 3 seconds..."
echo
sleep 3
run_cmd
is_fail || return 0
n=$[$n+1]
done
return 1
}
# ==========
build() {
local f="$1"
local buildfile=
local dirpath=
(
[[ -d "$f" ]] && optfile="$f/options" || optfile="$(dirname $f)/options"
source "$optfile" 2>/dev/null
if [[ -d "$f" ]]; then
[[ -f "$f/docker-bake.hcl" ]] && f="$f/docker-bake.hcl" || f="$f/${OPT_BUILD_FILE:=Dockerfile}"
fi
[[ ! -f "$f" ]] && {
log_error "FAIL: $1 build file missing."
return 1
}
echo_green "Building with context $(dirname $f)[$(basename $f)]"
echo_green_split
if [[ "${f##*.}" == "hcl" ]]; then
build_bake "$f"
else
build_dockerfile "$f"
fi
echo_green_split
echo
)
}
get_common_build_options() {
local opts="$CMD_BUILD_OPTS"
is_true $CMD_BUILD_FORCE && opts="$opts --no-cache"
is_true $CMD_PUSH && opts="$opts --push"
is_true $CMD_LOAD && opts="$opts --load"
is_verbose 2 && opts="$opts --progress plain"
echo $opts
}
# 平台
# 优先级: 默认 < options < 命令指定 < --load时本地系统平台
get_build_platform() {
is_true $CMD_LOAD && {
get_local_platform
return
}
# 指定平台
[[ -n "$OPT_BUILD_PLATFORM" ]] && \
OPT_BUILD_PLATFORM=$(echo $OPT_BUILD_PLATFORM | tr -s ' ' ',')
local plat=${OPT_BUILD_PLATFORM:-$DEF_BUILD_PLATFORMS}
# 排除平台
[[ -n "$OPT_BUILD_EXCLUDE_PLATFORM" ]] && {
arr=(${OPT_BUILD_EXCLUDE_PLATFORM//,/ })
for _e in ${arr[@]}; do
plat=${plat//$_e/}
done
# 删除重复`,`
plat=$(echo $plat | tr -s ',' ',')
# 删除开头头或末尾的`,`
plat=${plat##,}
plat=${plat%%,}
}
echo ${CMD_PLATFORM:-$plat}
}
build_bake() {
local f=$1
local bakefile=$(basename $f)
local build_dir=$(dirname $f)
if is_verbose; then
echo_green "filepath: $f"
echo_green "build_dir: $build_dir"
echo_green "bakefile: $bakefile"
echo_green "target: $CMD_BUILD_TARGET"
echo_green_split
fi
(
export BUILD_USER=${BUILD_USER:=$USER}
export BUILD_CONTEXT=$build_dir
export BUILD_NAME=${build_dir%%\/*}
cd $build_dir # 跳出 () 后会自动退出
export BUILD_PLATFORM=$(get_build_platform)
build_opts=$(get_common_build_options)
# build args
build_args="$OPT_BUILD_ARGS $CMD_BUILD_ARGS"
[[ -n "$build_args" ]] && {
for a in $build_args; do
build_opts="$build_opts --set=*.args.$a"
done
}
cmd="docker buildx bake $build_opts -f $SCRIPT_DIR/basic.hcl -f $bakefile"
# echo
if is_true $CMD_DRY_RUN; then
for e in ${!BUILD_*}; do
echo_green "$(eval printf \"%-16s%s\" \"$e:\" \"\$$e\")"
done
echo_green_split
echo
$cmd --print $CMD_BUILD_TARGET
fi
run_cmd $cmd $CMD_BUILD_TARGET
retry_if_failed
is_fail && {
echo
log_error "Build $f fail."
}
)
}
# 构建镜像
# $1 目录或Dockerfile路径
# $2 options file
build_dockerfile() {
local f=$1
local of=$2
local dockerfile=$(basename $f)
local build_dir=$(dirname $f)
local image=${build_dir%%\/*}
suite=(${build_dir##*\/})
[[ "$suite" == "$build_dir" ]] && suite=("latest")
local tag=$(get_local_tag $image $suite)
if is_verbose; then
echo_green "filepath: $f"
echo_green "build_dir: $build_dir"
echo_green "dockerfile: $dockerfile"
echo_green "image: $image"
echo_green "suite: ${suite[@]:-latest}"
echo_green "tags: $(get_local_tag $image $suite)"
echo_green_split
fi
local new_suite=()
for s in ${suite[@]}; do
is_build_ignored $image $s && \
log_error "IGNORED: $(get_local_tag $image $s)" || \
new_suite=(${new_suite[@]} $s)
done
if [[ -z "${new_suite[@]}" ]]; then
return 0
fi
suite=(${new_suite[@]})
local build_opts=$(get_common_build_options)
build_args="$OPT_BUILD_ARGS $CMD_BUILD_ARGS"
[[ -n "$build_args" ]] && {
for a in $build_args; do
build_opts="$build_opts --build-arg $a"
done
}
for s in ${suite[@]}; do
build_opts="$build_opts -t $(get_local_tag $image $s)"
done
for t in $CMD_BUILD_TARGET; do
build_opts="$build_opts --target $t"
done
run_cmd docker buildx build --platform=$(get_build_platform) $build_opts -f $f $build_dir
# run_cmd docker build $build_opts -f $f $build_dir
retry_if_failed
is_fail && {
echo
log_error "Build $tag with context $image fail."
}
}
build_all() {
local dirs=$(find_dirs)
# 排序
dirs=($DEF_BUILD_PRIORITY ${dirs[@]})
# 去重
dirs=($(awk -v RS=' ' '!a[$1]++' <<< ${dirs[@]}))
echo ${dirs[@]}
for d in ${dirs[@]}; do
build "$d"
done
}
_build_all() {
local dirs=$(find_dirs "." 2)
# 排序
dirs=($DEF_BUILD_PRIORITY ${dirs[@]})
# 去重
dirs=($(awk -v RS=' ' '!a[$1]++' <<< ${dirs[@]}))
# echo $dirs
for d in ${dirs[@]}; do
build "$d"
done
}
_build_single() {
local single=$1
[[ ! -e "$single" ]] && {
log_error "$single missing"
return 1
}
if [[ -f "$single" ]] || [[ -f "$single/docker-bake.hcl" ]]; then
build "$single"
else
dirs=$(find_dirs ${single%/} 2)
# echo ${dirs[@]}
for d in ${dirs[@]}; do build "$d"; done
fi
}
build_single() {
local single=$1
[[ ! -e "$single" ]] && {
log_error "$single missing"
return 1
}
local files=
[[ -f "$single" ]] && files=($single) || {
single=${single%/}
files=$(find_dirs $single 2)
}
# [[ -z "$files" ]] && {
# log_error "$single Dockerfile missing"
# return 1
# }
# echo ${files[@]}
for f in ${files[@]}; do
build "$f"
done
}
usage() {
cat <<EOF
USAGE: $0 [OPTIONS] [DIR|DOCKERFILE ...]
基于 buildx(https://github.com/docker/buildx)
Options:
-h, --help 帮助
-u, --user USER 用户名,用于生成镜像名,当值为『-』时则意味着用户名将被置空
-p, --push 构造镜像后推送至镜像库
-l, --load
-f, --force 忽略.buildignore
-d, --dry-run 仅输出运行过程
-v, --verbose 详细输出
以build开始的参数都作用于docker buildx命令:
--build-arg ARG 自定义构建参数,即docker build --build-arg ARG
--build-opt OPT docker build参数,已有默认: $DEFAULT_BUILD_OPTS
--build-force 强制重建,不使用缓存,相当于:
--build-opt "--no-cache"
EOF
}
# ==============================================================================
BASE_DIR=$(abspath $(dirname $0))
# 默认docker user
USER=${DOCKER_USER:-}
[[ -z "$USER" ]] && USER=$(docker info 2>/dev/null | grep Username | awk '{print $2}')
DEF_BUILD_PRIORITY=${DOCKER_BUILD_PRIORITY}
DEF_BUILD_ARCHS=${DOCKER_BUILD_ARCHS}
# 默认构建架构平台
# REF: https://github.com/containerd/containerd/blob/v1.2.6/platforms/platforms.go#L63
DEF_BUILD_PLATFORMS=$(for _A in $DEF_BUILD_ARCHS; do echo -n "linux/$_A,"; done)
DEF_BUILD_PLATFORMS=${DEF_BUILD_PLATFORMS%,}
LAST_CMD_RUNNED=
FAILED_RETRY_MAX=${FAILED_RETRY_MAX:-5}
# ==============================================================================
CMD_VERBOSE=0
CMD_DRY_RUN=false
CMD_PLATFORM=
CMD_PUSH=false
CMD_LOAD=false
CMD_PULL=false
CMD_FORCE=false
CMD_BUILD_ARGS=
CMD_BUILD_OPTS=
CMD_BUILD_TARGET=
# ==============================================================================
cd "$BASE_DIR" >/dev/null || exit 1
# trap "echo aaa" EXIT
file_dirs=()
while (( ${#} )); do
case "$1" in
--user|-u )
shift 1
USER=${1:-}
[[ "$USER" == "-" ]] && USER=""
;;
--platform )
shift 1
CMD_PLATFORM=${1:-}
;;
-output|-o )
shift 1
CMD_OUTPUT=${1:-}
;;
--push|-p )
CMD_PUSH=true
;;
--load|-l )
CMD_LOAD=true
;;
--pull )
CMD_PULL=true
;;
--build-target|-t )
shift 1
CMD_BUILD_TARGET="$CMD_BUILD_TARGET $1"
;;
--build-arg|--ba )
shift 1
arg=${1:-}
[[ -n "$arg" ]] && CMD_BUILD_ARGS="$CMD_BUILD_ARGS $arg"
;;
--build-opt|--bo )
shift 1
arg=${1:-}
[[ -n "$arg" ]] && CMD_BUILD_OPTS="$CMD_BUILD_OPTS $arg"
;;
--build-force|--bf )
CMD_BUILD_OPTS="$CMD_BUILD_OPTS --no-cache"
;;
--force|-f )
CMD_FORCE=true
;;
--dry-run|-d)
CMD_DRY_RUN=true
is_verbose || CMD_VERBOSE=1
;;
--verbose|-v|-vv )
level=$(echo "$1" | grep -o 'v' | wc -l)
CMD_VERBOSE=$(($CMD_VERBOSE + $level))
;;
--help|-h )
usage
exit_ok
;;
-* )
exit_err "错误的选项: $1"
;;
* )
# 参数结束
file_dirs=(${file_dirs[@]} "$1")
# shift
# break
;;
esac
shift 1
done
# 检查
ERR_LOG_FILE=$(mktemp)
is_verbose && {
echo_green "Main Options:"
echo_green_split
echo_green "USER: $USER"
echo_green "LOCAL ARCH: $(get_local_arch)"
echo_green "PLATFORM: ${CMD_PLATFORM:-$DEF_BUILD_PLATFORMS}"
echo_green "PUSH: $CMD_PUSH"
echo_green "LOAD: $CMD_LOAD"
echo_green "PULL: $CMD_PULL"
echo_green "FORCE: $CMD_FORCE"
echo_green "DRY_RUN: $CMD_DRY_RUN"
echo_green "BUILD_OPTS: $CMD_BUILD_OPTS"
echo_green "BUILD_ARGS: $CMD_BUILD_ARGS"
echo_green "BUILD_TARGET: $CMD_BUILD_TARGET"
echo_green "FILE/DIR: ${file_dirs[@]}"
echo_green_split
echo
}
if [[ ${#file_dirs[@]} -eq 0 ]]; then
_build_all
else
for i in ${file_dirs[@]}; do
_build_single $i
done
# while [[ $# -gt 0 ]]; do
# _build_single $1
# shift $(( $# > 0 ? 1 : 0 ))
# done
fi
[[ -n "$(cat $ERR_LOG_FILE)" ]] && {
echo
echo_red_split
echo_red "ERRORS:"
echo_red "$(cat $ERR_LOG_FILE)"
echo_red_split
}
exit_ok