forked from jtreminio/php-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build
executable file
·263 lines (227 loc) · 6.58 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
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BUILD_TYPE="all"
RUN_TESTS="no"
function show_help() {
cat << EOF
Usage: build [OPTION]
Builds CLI, FPM, Apache and Nginx images for PHP
-v full version, 7.4.13 or 7.3.25 or 7.2.34, etc
if -b=env this flag is not accepted
-b build type, "all", "env", "cli", "fpm", "apache", "nginx"
defaults to "all"
-p push image(s) to docker hub
-t run tests after build
defaults to "no"
-h display this help and exit
Example: build -v 8.1.0 -b all -p yes
Builds these images - jtreminio:php-cli
- jtreminio:php
- jtreminio:php-apache
- jtreminio:php-nginx
EOF
exit 0
}
while getopts ":v:b:p:t:h" opt; do
case $opt in
v) PHP_VERSION="$OPTARG"
;;
b) BUILD_TYPE="$OPTARG"
;;
p) PUSH_TO_HUB="$OPTARG"
;;
t) RUN_TESTS="$OPTARG"
;;
h) show_help
;;
\?) echo "Invalid option -$OPTARG" >&2
;;
esac
done
if [[ "${BUILD_TYPE}" != "all" &&
"${BUILD_TYPE}" != "env" &&
"${BUILD_TYPE}" != "cli" &&
"${BUILD_TYPE}" != "fpm" &&
"${BUILD_TYPE}" != "apache" &&
"${BUILD_TYPE}" != "nginx"
]]; then
printf "invalid build (-b) value: ${BUILD_TYPE}\n"
show_help
exit 1
fi
if [[ "${PUSH_TO_HUB}" != "yes" &&
"${PUSH_TO_HUB}" != "no"
]]; then
printf "invalid push to hub (-p) value: ${PUSH_TO_HUB}\n"
show_help
exit 1
fi
if [[ "${RUN_TESTS}" != "yes" &&
"${RUN_TESTS}" != "no"
]]; then
printf "invalid run tests (-t) value: ${RUN_TESTS}\n"
show_help
exit 1
fi
if [[ ${BUILD_TYPE} == "env" ]] && ! [[ -z ${PHP_VERSION} ]]; then
printf "version (-v) flag not accepted when -b=env\n"
show_help
exit 1
fi
if [[ ${BUILD_TYPE} != "env" ]] && [[ -z ${PHP_VERSION} ]]; then
printf "invalid version (-v) value: ${PHP_VERSION}\n"
show_help
exit 1
fi
if [[ ${BUILD_TYPE} != "env" ]]; then
# 7.4.13 -> 7.4
PHP_MAJOR=$(echo ${PHP_VERSION} | cut -d . -f -2)
if [[ "${PHP_VERSION// }" == "${PHP_MAJOR// }" ]]; then
printf "PHP_VERSION must be in 7.4.0 format"
exit 1
fi
REGEXP="([0-9]{1}\.)+([0-9]{1}\.)+([0-9]{1,2})"
if ! [[ ${PHP_VERSION} =~ ${REGEXP} ]]; then
printf "PHP_VERSION must be in 7.4.0 format"
exit 1
fi
fi
function main() {
if [[ ${BUILD_TYPE} == "env" ]]; then
build_env_image
if [[ ${PUSH_TO_HUB} == "yes" ]]; then
docker image push jtreminio/phpenv:latest
fi
exit 0
fi
case ${BUILD_TYPE} in
cli)
build_php_cli_image ${PHP_MAJOR} ${PHP_VERSION}
;;
fpm)
build_php_fpm_image ${PHP_MAJOR} ${PHP_VERSION}
;;
apache)
build_apache_image ${PHP_MAJOR} ${PHP_VERSION}
;;
nginx)
build_nginx_image ${PHP_MAJOR} ${PHP_VERSION}
;;
*)
build_php_cli_image ${PHP_MAJOR} ${PHP_VERSION}
build_php_fpm_image ${PHP_MAJOR} ${PHP_VERSION}
build_apache_image ${PHP_MAJOR} ${PHP_VERSION}
build_nginx_image ${PHP_MAJOR} ${PHP_VERSION}
;;
esac
if [[ ${RUN_TESTS} == "yes" ]]; then
bash "${DIR}/test" ${PHP_VERSION}
fi
if [[ $? -ne 0 ]]; then
exit 1
fi
if [[ "${PUSH_TO_HUB}" == "yes" ]]; then
case ${BUILD_TYPE} in
cli)
docker image push jtreminio/php-cli:${PHP_MAJOR}
docker image push jtreminio/php-cli:${PHP_VERSION}
;;
fpm)
docker image push jtreminio/php:${PHP_MAJOR}
docker image push jtreminio/php:${PHP_VERSION}
;;
apache)
docker image push jtreminio/php-apache:${PHP_MAJOR}
docker image push jtreminio/php-apache:${PHP_VERSION}
;;
nginx)
docker image push jtreminio/php-nginx:${PHP_MAJOR}
docker image push jtreminio/php-nginx:${PHP_VERSION}
;;
*)
docker image push jtreminio/php-cli:${PHP_MAJOR}
docker image push jtreminio/php-cli:${PHP_VERSION}
docker image push jtreminio/php:${PHP_MAJOR}
docker image push jtreminio/php:${PHP_VERSION}
docker image push jtreminio/php-apache:${PHP_MAJOR}
docker image push jtreminio/php-apache:${PHP_VERSION}
docker image push jtreminio/php-nginx:${PHP_MAJOR}
docker image push jtreminio/php-nginx:${PHP_VERSION}
;;
esac
fi
}
function build_env_image() {
echo "Building ENV"
docker image build \
-t jtreminio/phpenv:latest \
-f Dockerfile-env \
.
if [[ $? -ne 0 ]]; then
echo "Error encountered"
exit 1
fi
}
function build_php_cli_image() {
PHP_MAJOR="${1}"
PHP_MINOR="${2}"
echo "Building PHP-CLI ${PHP_MINOR}"
docker image build \
--build-arg PHP_VER=${PHP_MAJOR} \
--build-arg PHP_VER_DOT=${PHP_MINOR} \
-t jtreminio/php-cli:${PHP_MAJOR} \
-t jtreminio/php-cli:${PHP_MINOR} \
-f Dockerfile-php-cli \
.
if [[ $? -ne 0 ]]; then
echo "Error encountered"
exit 1
fi
}
function build_php_fpm_image() {
PHP_MAJOR="${1}"
PHP_MINOR="${2}"
echo "Building PHP-CLI + PHP-FPM ${PHP_MINOR}"
docker image build \
--build-arg PHP_VER=${PHP_MAJOR} \
--build-arg PHP_VER_DOT=${PHP_MINOR} \
-t jtreminio/php:${PHP_MAJOR} \
-t jtreminio/php:${PHP_MINOR} \
-f Dockerfile-php-fpm \
.
if [[ $? -ne 0 ]]; then
echo "Error encountered"
exit 1
fi
}
function build_apache_image() {
PHP_MAJOR="${1}"
PHP_MINOR="${2}"
echo "Building PHP ${PHP_MAJOR} + Apache"
docker image build \
--build-arg PHP_VER_DOT=${PHP_MINOR} \
-t jtreminio/php-apache:${PHP_MAJOR} \
-t jtreminio/php-apache:${PHP_MINOR} \
-f Dockerfile-apache \
.
if [[ $? -ne 0 ]]; then
echo "Error encountered"
exit 1
fi
}
function build_nginx_image() {
PHP_MAJOR="${1}"
PHP_MINOR="${2}"
echo "Building PHP ${PHP_MAJOR} + Nginx"
docker image build \
--build-arg PHP_VER_DOT=${PHP_MINOR} \
-t jtreminio/php-nginx:${PHP_MAJOR} \
-t jtreminio/php-nginx:${PHP_MINOR} \
-f Dockerfile-nginx \
.
if [[ $? -ne 0 ]]; then
echo "Error encountered"
exit 1
fi
}
main