-
Notifications
You must be signed in to change notification settings - Fork 1
/
build
executable file
·166 lines (144 loc) · 4.1 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
#!/bin/bash
set -e
DOCKER_REGISTRY=docker.io/phpbrew
PUSH_ONLY=0
DRY_RUN=0
PUSH_IMAGE=0
ARGS=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--dry-run)
DRY_RUN=1
shift
;;
--push)
PUSH_IMAGE=1
shift
;;
-p|--push-only)
PUSH_ONLY=1
shift
;;
*) # unknown option
ARGS+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
success()
{
echo -e "\033[1;32m"$*"\033[0m"
}
build()
{
local dir=$1
local default_tag=""
if [[ $(dirname $dir) == "." ]] ; then
name=$(basename $dir)
else
name=$(dirname $dir)
default_tag=$(basename $dir)
fi
local image_repository=$DOCKER_REGISTRY/$name
echo "==> Building $name in $dir with default tag $default_tag"
# export $name
if [[ ! -e $dir/Dockerfile ]] ; then
return
fi
declare -a tags=()
declare -a docker_args=()
# image tags
local git_tag=$(git name-rev --name-only --tags HEAD 2> /dev/null)
if [[ $git_tag != "undefined" ]] ; then
export GIT_TAG=$git_tag
fi
if [[ -e $dir/tags.sh ]] ; then
tags+=($(bash $dir/tags.sh))
elif [[ -x $dir/tags ]] ; then
tags+=($($dir/tags))
elif [[ -f $dir/tags.list ]] ; then
tags+=($(cat $dir/tags.list))
elif [[ -n $GIT_TAG ]] ; then
tags+=($git_tag)
elif [[ -n $default_tag ]] ; then
tags+=("$default_tag")
else
tags+=("latest")
fi
if [[ $PUSH_ONLY == 1 ]] ; then
for tag in ${tags[@]} ; do
(set -x ; docker push $image_repository:$tag)
done
return
fi
if [[ -e $dir/buildargs.sh ]] ; then
echo Found $dir/buildargs.sh
for build_arg in $(bash $dir/buildargs.sh); do
docker_args+=(--build-arg "$build_arg")
done
fi
for tag in ${tags[@]} ; do
docker_args+=("--tag" "$image_repository:$tag")
done
# docker build does not solve the symlink in the context directory
# we need to read the symlink and build up the --file option.
if [[ -L $dir/Dockerfile ]] ; then
local dockerfile=$dir/$(readlink $dir/Dockerfile)
docker_args+=(--file "$dockerfile")
fi
if [[ $DRY_RUN == 1 ]] ; then
echo "+ docker build ${DOCKER_BUILD_ARGS[@]} ${docker_args[@]} $dir"
else
# use sub-shell to show the command
(set -x ; docker build "${DOCKER_BUILD_ARGS[@]}" \
"${docker_args[@]}" \
$dir)
fi
if [[ $PUSH_IMAGE == 1 ]] ; then
if [[ $DRY_RUN == 1 ]] ; then
for tag in ${tags[@]} ; do
echo "+ docker push $image_repository:$tag"
done
else
for tag in ${tags[@]} ; do
(set -x ; docker push $image_repository:$tag)
done
fi
fi
success "[SUCCESS] $name ($dir) image are successfully built and tagged."
if [[ -f $dir/test.yaml ]] ; then
container-structure-test test --image "$image_repository:$tag" --config $dir/test.yaml
success "[SUCCESS] $name ($dir) image are successfully validated."
fi
}
build_image_recursively()
{
local image_dir=$1
# strip the image dir from the path
local dockerfiles=$(find $image_dir -name "Dockerfile" | sort)
for dockerfile in ${dockerfiles[@]} ; do
echo "==> Found Dockerfile $dockerfile"
local image_subdir=$(dirname $dockerfile)
build "$image_subdir"
done
}
# bash3 does not support associative array
declare -a DOCKER_BUILD_ARGS=()
for name in * ; do
if [[ -e $name/Dockerfile ]] ; then
image_varname=$(echo ${name}_IMAGE | tr a-z- A-Z_)
image_repository=$DOCKER_REGISTRY/$name
echo "$image_varname=$image_repository:latest"
DOCKER_BUILD_ARGS+=("--build-arg" "$image_varname=$image_repository:latest")
fi
done
if [[ ${#ARGS[*]} > 0 ]] ; then
for arg in ${ARGS[*]} ; do
build_image_recursively "$arg"
done
else
# always build the base image first
build_image_recursively "phpbrew"
fi