-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·117 lines (98 loc) · 2.08 KB
/
build.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
#!/usr/bin/env sh
# shellcheck shell=sh disable=SC2039,SC3043
_print_usage() {
local program="$1"
echo "$program
Builds a Docker image with extended metadata
USAGE:
$program [FLAGS] [--] <NAME> [<TAG> ..]
FLAGS:
-h, --help Prints help information
-v, --verbose Prints verbose output
ARGS:
<NAME> Name for the Docker image
<TAG> Tag for the Docker image [default: latest]
" | sed 's/^ \{1,4\}//g'
}
main() {
set -eu
if [ -n "${DEBUG:-}" ]; then set -v; fi
if [ -n "${TRACE:-}" ]; then set -xv; fi
local program
program="$(basename "$0")"
OPTIND=1
while getopts "h-:" arg; do
case "$arg" in
h)
_print_usage "$program"
return 0
;;
-)
case "$OPTARG" in
help)
_print_usage "$program"
return 0
;;
'')
# "--" terminates argument processing
break
;;
*)
_print_usage "$program" >&2
fail "invalid argument --$OPTARG"
;;
esac
;;
\?)
_print_usage_main "$program" >&2
fail "invalid argument; arg=-$OPTARG"
;;
esac
done
shift "$((OPTIND - 1))"
if [ -z "${1:-}" ]; then
_print_usage "$program" >&2
fail "missing <NAME> argument; arg=-$OPTARG"
fi
local name="$1"
shift
if [ -z "${VERBOSE:-}" ]; then
VERBOSE=""
fi
if [ -n "$*" ]; then
build "$name" "$@"
else
build "$name" "latest"
fi
}
build() {
local name="$1"
shift
local tag="$1"
shift
local revision created t
revision="$(git show -s --format=%H)"
created="$(date -u +%FT%TZ)"
if [ -n "$VERBOSE" ]; then
set -x
fi
docker build \
--pull \
--force-rm \
--build-arg "NAME=$name" \
--build-arg "VERSION=$tag" \
--build-arg "REVISION=$revision" \
--build-arg "CREATED=$created" \
--tag "$name:$tag" \
.
for t in "$@"; do
docker tag "$name:$tag" "$name:$t"
done
}
fail() {
echo "" >&2
echo "xxx $1" >&2
echo "" >&2
return 1
}
main "$@"