-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·95 lines (76 loc) · 2.37 KB
/
build.sh
File metadata and controls
executable file
·95 lines (76 loc) · 2.37 KB
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
#!/bin/bash
set -eo pipefail
SCRIPT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
BUILD_PREFIX="${BUILD_PREFIX:-ellneal}"
function buildandpushimage() {
local f=${1#./}
local image=${f%Dockerfile}
local base=${image%%\/*}
local build_dir=$(dirname $f)
local tag=${build_dir##*\/}
if [[ -z "$tag" ]] || [[ "$tag" == "$base" ]]; then
tag=latest
fi
buildimage "$@" || return 1
# on successful build, push the image
echo " --- "
echo "Successfully built ${base}:${tag} with context ${build_dir}"
echo " --- "
# try push a few times because notary server sometimes returns 401 for
# absolutely no reason
n=0
until [ $n -ge 5 ]; do
docker push "$BUILD_PREFIX/$base:$tag" && break
echo "Try #$n failed... sleeping for 15 seconds"
n=$[$n+1]
sleep 15
done
}
function buildimage() {
local f=${1#./}
local image=${f%Dockerfile}
local base=${image%%\/*}
local build_dir=$(dirname $f)
local tag=${build_dir##*\/}
if [[ -z "$tag" ]] || [[ "$tag" == "$base" ]]; then
tag=latest
fi
echo "Building $BUILD_PREFIX/$base:$tag in $build_dir"
docker build --rm --force-rm -t "$BUILD_PREFIX/$base:$tag" $build_dir || return 1
}
function removeimage() {
local f=${1#./}
local image=${f%Dockerfile}
local base=${image%%\/*}
local build_dir=$(dirname $f)
local tag=${build_dir##*\/}
if [[ -z "$tag" ]] || [[ "$tag" == "$base" ]]; then
tag=latest
fi
echo "Removing $BUILD_PREFIX/$base:$tag"
docker rmi "$BUILD_PREFIX/$base:$tag" || echo
}
function buildall() {
find . -iname '*Dockerfile' -print0 | sort -z | xargs -0 -I file $SCRIPT buildandpushimage file
}
function removeall() {
find . -iname '*Dockerfile' -print0 | sort -z | xargs -0 -I file $SCRIPT removeimage file
}
function main() {
f=$1
if [[ "$f" == "" ]]; then
echo "Pass 'all' to build all images" && exit 1
elif [[ "$f" == "all" ]]; then
buildall
elif [[ "$f" == "clean" ]]; then
removeall
elif [[ -f "$f" ]]; then
buildimage "$@"
elif [[ -d "$f" ]]; then
shift
buildimage "$(basename "$f")/Dockerfile" "$@"
else
$@
fi
}
main "$@"