-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdocker_builder.bash
executable file
·185 lines (159 loc) · 4.42 KB
/
docker_builder.bash
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
#!/usr/bin/env bash
#
# author : Jeong Han Lee
# email : jeonghan.lee@gmail.com
# version : 0.0.1
declare -g SC_SCRIPT;
declare -g SC_TOP;
SC_SCRIPT="$(realpath "$0")";
SC_TOP="${SC_SCRIPT%/*}"
function pushd { builtin pushd "$@" > /dev/null || exit; }
function popd { builtin popd > /dev/null || exit; }
declare -g TARGET_NAME=""
declare -g DOCKER_ID=""
declare -g BUILD_ARGS=""
declare -g DOCKER_BUILD_OPTS=""
declare -g DOCKER_FILENAME=""
options=":o:i:n:t:a:hd"
DRYRUN="NO"
#BUILDARG="NO"
docker_build_options=""
docker_file_path=""
docker_id=""
target_name=""
build_args=""
function usage
{
{
echo "";
echo "Usage : $0 ${options} "
echo "";
echo " possbile options";
echo "";
echo " -o : docker build options ${DOCKER_BUILD_OPTS}";
echo " -i : docker id ${DOCKER_ID}";
echo " -n : target name ${TARGET_NAME}";
echo " -t : dockerpath Dockerfile"
echo " -a : array of build-arg translate from -a \"a=1 b=2 c=3\""
echo " to \"--build-arg a=1 --build-arg b=2 --build-arg b=3\""
echo " -d : dry run";
echo " -h : this screen";
echo "";
echo " bash $0 -d "
echo ""
} 1>&2;
exit 1;
}
while getopts "${options}" opt; do
case "${opt}" in
o)
docker_build_options="${OPTARG}";
;;
i)
docker_id="${OPTARG}";
;;
n)
target_name="${OPTARG}";
;;
t)
docker_file_path="${OPTARG}";
;;
a)
build_args="${OPTARG}";
;;
d)
DRYRUN="YES";
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
h)
usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
*)
usage
;;
esac
done
#if [ $OPTIND -eq 1 ]; then usage ; fi
shift $((OPTIND-1))
if [ -z "${docker_file_path}" ]; then
usage;
else
DOCKER_FILENAME=${docker_file_path}/Dockerfile;
fi
set -a
# shellcheck disable=SC1091
# shellcheck source=env.conf
. "$SC_TOP/${docker_file_path}/env.conf"
if [ -r "${SC_TOP}/${docker_file_path}"/env.local ]; then
printf ">>> We've found the local configuration file.\\n";
printf " The original DOCKER_ID = %s\\n" "${DOCKER_ID}"
printf " TARGET_NAME = %s\\n" "${TARGET_NAME}"
printf " BUILD_ARGS = %s\\n" "${BUILD_ARGS}"
printf " DOCKER_BUILD_OPTS = %s\\n" "${DOCKER_BUILD_OPTS}"
# shellcheck disable=SC1091
# shellcheck source=docker_target_name.local
. "$SC_TOP/target_name.local"
printf " will be overridden with \\n";
printf " DOCKER_ID = %s\\n" "${DOCKER_ID}"
printf " TARGET_NAME = %s\\n" "${TARGET_NAME}"
printf " BUILD_ARGS = %s\\n" "${BUILD_ARGS}"
printf " DOCKER_BUILD_OPTS = %s\\n" "${DOCKER_BUILD_OPTS}"
fi
set +a
# BUILD OPTIONS
if [ -z "${docker_build_options}" ]; then
printf ">>> We will use the predefined docker build options : %s\\n" "${DOCKER_BUILD_OPTS}"
else
DOCKER_BUILD_OPTS="${docker_build_options}"
printf ">>> We will use the input docker build options : %s\\n" "${DOCKER_BUILD_OPTS}"
fi
# ID
if [ -z "${docker_id}" ]; then
printf ">>> We will use the predefined docker id : %s\\n" "${DOCKER_ID}"
else
DOCKER_ID="${docker_id}"
printf ">>> We will use the input docker id: %s\\n" "${DOCKER_ID}"
fi
# Target Name
if [ -z "${target_name}" ]; then
printf ">>> We will use the predefined target name: %s\\n" "${TARGET_NAME}"
else
TARGET_NAME=${target_name};
printf ">>> We will use the input target name: %s\\n" "${TARGET_NAME}"
fi
# Build Args
if [ -z "${build_args}" ]; then
printf ">>> We will use the predefined build args : %s\\n" "${BUILD_ARGS}"
else
BUILD_ARGS="${build_args}"
printf ">>> We will use the input build args : %s\\n" "${BUILD_ARGS}"
fi
target_image="${DOCKER_ID}/${TARGET_NAME}"
docker_build_arg="";
if [ -z "${BUILD_ARGS}" ]; then
docker_build_arg="";
else
for arg in "${BUILD_ARGS[@]}"; do
docker_build_arg+="--build-arg";
docker_build_arg+=" ";
docker_build_arg+="\"${arg}\"";
docker_build_arg+=" ";
done
fi
command="docker build ${DOCKER_BUILD_OPTS} --file ${DOCKER_FILENAME} -t ${target_image} ${docker_build_arg} ."
SRC_TOP=${SC_TOP}
pushd "${SRC_TOP}" || exit
if [ "$DRYRUN" == "YES" ]; then
echo "${command}"
else
echo "${command}"
eval "${command}"
fi
popd || exit