-
Notifications
You must be signed in to change notification settings - Fork 147
/
build_bundle_image.sh
executable file
·384 lines (293 loc) · 12.3 KB
/
build_bundle_image.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/bin/bash
source ./_common.sh
function build_docker_image {
if [[ ${LIFERAY_DOCKER_RELEASE_FILE_URL%} == */snapshot-* ]]
then
DOCKER_IMAGE_NAME=${DOCKER_IMAGE_NAME}-snapshot
fi
if [[ ${LIFERAY_DOCKER_RELEASE_FILE_URL} == https://release-* ]] || [[ ${LIFERAY_DOCKER_RELEASE_FILE_URL} == *release.liferay.com* ]]
then
DOCKER_IMAGE_NAME=${DOCKER_IMAGE_NAME}-snapshot
fi
local release_version=${LIFERAY_DOCKER_RELEASE_FILE_URL%/*}
release_version=${release_version##*/}
if [[ ${LIFERAY_DOCKER_RELEASE_FILE_URL} == https://release-* ]] || [[ ${LIFERAY_DOCKER_RELEASE_FILE_URL} == *release.liferay.com* ]]
then
release_version=${LIFERAY_DOCKER_RELEASE_FILE_URL#*tomcat-}
release_version=${release_version%.*}
fi
if [[ ${LIFERAY_DOCKER_RELEASE_FILE_URL} == *files.liferay.com/* ]] && [[ ${LIFERAY_DOCKER_RELEASE_FILE_URL%} != */snapshot-* ]]
then
local file_name_release_version=${LIFERAY_DOCKER_RELEASE_FILE_URL#*tomcat-}
file_name_release_version=${file_name_release_version%.*}
file_name_release_version=${file_name_release_version%-*}
if [[ ${file_name_release_version} == *-slim ]]
then
file_name_release_version=${file_name_release_version%-slim}
fi
local service_pack_name=${file_name_release_version##*-}
fi
if [[ ${service_pack_name} == ga* ]] || [[ ${service_pack_name} == sp* ]]
then
release_version=${release_version}-${service_pack_name}
fi
LABEL_VERSION=${release_version}
if [[ ${LIFERAY_DOCKER_RELEASE_FILE_URL%} == */snapshot-* ]]
then
local release_branch=${LIFERAY_DOCKER_RELEASE_FILE_URL%/*}
release_branch=${release_branch%/*}
release_branch=${release_branch%-private*}
release_branch=${release_branch##*-}
local release_hash=$(cat "${TEMP_DIR}/liferay/.githash")
release_hash=${release_hash:0:7}
if [[ ${release_branch} == master ]]
then
LABEL_VERSION="Master Snapshot on ${LABEL_VERSION} at ${release_hash}"
else
LABEL_VERSION="${release_branch} Snapshot on ${LABEL_VERSION} at ${release_hash}"
fi
fi
if [ -n "${LIFERAY_DOCKER_RELEASE_VERSION}" ]
then
release_version=${LIFERAY_DOCKER_RELEASE_VERSION}
fi
DOCKER_IMAGE_TAGS=()
local default_ifs=${IFS}
IFS=","
for release_version_single in ${release_version}
do
if [[ ${LIFERAY_DOCKER_RELEASE_FILE_URL%} == */snapshot-* ]]
then
DOCKER_IMAGE_TAGS+=("${LIFERAY_DOCKER_REPOSITORY}/${DOCKER_IMAGE_NAME}:${release_branch}-${release_version_single}-${release_hash}")
DOCKER_IMAGE_TAGS+=("${LIFERAY_DOCKER_REPOSITORY}/${DOCKER_IMAGE_NAME}:${release_branch}-$(date "${CURRENT_DATE}" "+%Y%m%d")")
DOCKER_IMAGE_TAGS+=("${LIFERAY_DOCKER_REPOSITORY}/${DOCKER_IMAGE_NAME}:${release_branch}")
else
DOCKER_IMAGE_TAGS+=("${LIFERAY_DOCKER_REPOSITORY}/${DOCKER_IMAGE_NAME}:${release_version_single}-d$(./release_notes.sh get-version)-${TIMESTAMP}")
DOCKER_IMAGE_TAGS+=("${LIFERAY_DOCKER_REPOSITORY}/${DOCKER_IMAGE_NAME}:${release_version_single}")
fi
done
if [[ "${LIFERAY_DOCKER_LATEST}" = "true" ]]
then
DOCKER_IMAGE_TAGS+=("${LIFERAY_DOCKER_REPOSITORY}/${DOCKER_IMAGE_NAME}:latest")
fi
if [ -e "${TEMP_DIR}/liferay/.githash" ]
then
LIFERAY_VCS_REF=$(cat "${TEMP_DIR}/liferay/.githash")
fi
IFS=${default_ifs}
remove_temp_dockerfile_target_platform
docker build \
--build-arg LABEL_BUILD_DATE=$(date "${CURRENT_DATE}" "+%Y-%m-%dT%H:%M:%SZ") \
--build-arg LABEL_LIFERAY_PATCHING_TOOL_VERSION="${LIFERAY_DOCKER_TEST_PATCHING_TOOL_VERSION}" \
--build-arg LABEL_LIFERAY_TOMCAT_VERSION=$(get_tomcat_version "${TEMP_DIR}/liferay") \
--build-arg LABEL_LIFERAY_VCS_REF="${LIFERAY_VCS_REF}" \
--build-arg LABEL_NAME="${DOCKER_LABEL_NAME}" \
--build-arg LABEL_RELEASE_VERSION="${LIFERAY_DOCKER_RELEASE_VERSION}" \
--build-arg LABEL_VCS_REF=$(git rev-parse HEAD) \
--build-arg LABEL_VCS_URL="https://github.com/liferay/liferay-docker" \
--build-arg LABEL_VERSION="${LABEL_VERSION}" \
$(get_docker_image_tags_args "${DOCKER_IMAGE_TAGS[@]}") \
"${TEMP_DIR}" || exit 1
}
function check_release {
if [[ ${RELEASE_FILE_NAME} == *-dxp-* ]] || [[ ${RELEASE_FILE_NAME} == *-private* ]]
then
DOCKER_IMAGE_NAME="dxp"
DOCKER_LABEL_NAME="Liferay DXP"
elif [[ ${RELEASE_FILE_NAME} == *-portal-* ]]
then
DOCKER_IMAGE_NAME="portal"
DOCKER_LABEL_NAME="Liferay Portal"
else
echo "${RELEASE_FILE_NAME} is an unsupported release file name."
exit 1
fi
}
function check_usage {
if [ ! -n "${LIFERAY_DOCKER_RELEASE_FILE_URL}" ]
then
echo "Usage: ${0} --push"
echo ""
echo "The script reads the following environment variables:"
echo ""
echo " LIFERAY_DOCKER_DEVELOPER_MODE (optional): If set to \"true\", all local images will be deleted before building a new one"
echo " LIFERAY_DOCKER_FIX_PACK_URL (optional): URL to a fix pack"
echo " LIFERAY_DOCKER_HUB_TOKEN (optional): Docker Hub token to log in automatically"
echo " LIFERAY_DOCKER_HUB_USERNAME (optional): Docker Hub username to log in automatically"
echo " LIFERAY_DOCKER_IMAGE_PLATFORMS (optional): Comma separated Docker image platforms to build when the \"push\" parameter is set"
echo " LIFERAY_DOCKER_LICENSE_API_HEADER (required for DXP): API header used to generate the trial license"
echo " LIFERAY_DOCKER_LICENSE_API_URL (required for DXP): API URL to generate the trial license"
echo " LIFERAY_DOCKER_RELEASE_FILE_URL (required): URL to a Liferay bundle"
echo " LIFERAY_DOCKER_REPOSITORY (optional): Docker repository"
echo ""
echo "Example: LIFERAY_DOCKER_RELEASE_FILE_URL=files.liferay.com/private/ee/portal/7.2.10/liferay-dxp-tomcat-7.2.10-ga1-20190531140450482.7z ${0} push"
echo ""
echo "Set \"push\" as a parameter to automatically push the image to Docker Hub."
exit 1
fi
check_utils 7z curl docker java unzip
}
function download_trial_dxp_license {
if [[ ${DOCKER_IMAGE_NAME} == "dxp" ]]
then
rm -fr "${TEMP_DIR}/liferay/data/license"
if (! ./download_trial_dxp_license.sh "${TEMP_DIR}/liferay" $(date "${CURRENT_DATE}" "+%s000"))
then
exit 4
fi
fi
}
function install_fix_pack {
if [ -n "${LIFERAY_DOCKER_FIX_PACK_URL}" ]
then
local fix_pack_url=${LIFERAY_DOCKER_FIX_PACK_URL}
FIX_PACK_FILE_NAME=${fix_pack_url##*/}
download downloads/fix-packs/"${FIX_PACK_FILE_NAME}" "${fix_pack_url}"
cp "downloads/fix-packs/${FIX_PACK_FILE_NAME}" "${TEMP_DIR}/liferay/patching-tool/patches"
"${TEMP_DIR}/liferay/patching-tool/patching-tool.sh" install
rm -fr "${TEMP_DIR}/liferay/data/hypersonic/"*
rm -fr "${TEMP_DIR}/liferay/osgi/state/"*
fi
}
function main {
if [[ " ${@} " =~ " --test " ]]
then
return
fi
check_usage "${@}"
make_temp_directory templates/bundle
set_parent_image
prepare_temp_directory "${@}"
check_release "${@}"
update_patching_tool
install_fix_pack "${@}"
prepare_tomcat
download_trial_dxp_license
build_docker_image
test_docker_image
log_in_to_docker_hub
push_docker_image "${1}"
clean_up_temp_directory
}
function prepare_temp_directory {
RELEASE_FILE_NAME=${LIFERAY_DOCKER_RELEASE_FILE_URL##*/}
local download_dir=${LIFERAY_DOCKER_RELEASE_FILE_URL%/*}
download_dir=${download_dir#*com/}
download_dir=${download_dir#*com/}
download_dir=${download_dir#*liferay-release-tool/}
download_dir=${download_dir#*private/ee/}
download_dir=downloads/${download_dir}
download "${download_dir}/${RELEASE_FILE_NAME}" "${LIFERAY_DOCKER_RELEASE_FILE_URL}"
if [[ ${RELEASE_FILE_NAME} == *.7z ]]
then
7z x -O"${TEMP_DIR}" "${download_dir}/${RELEASE_FILE_NAME}" || exit 3
else
unzip -d "${TEMP_DIR}" -q "${download_dir}/${RELEASE_FILE_NAME}" || exit 3
fi
mv "${TEMP_DIR}/liferay-"* "${TEMP_DIR}/liferay"
}
function push_docker_image {
if [ "${1}" == "push" ]
then
check_docker_buildx
sed -i '1s/FROM /FROM --platform=${TARGETPLATFORM} /g' "${TEMP_DIR}"/Dockerfile
docker buildx build \
--build-arg LABEL_BUILD_DATE=$(date "${CURRENT_DATE}" "+%Y-%m-%dT%H:%M:%SZ") \
--build-arg LABEL_LIFERAY_PATCHING_TOOL_VERSION="${LIFERAY_DOCKER_TEST_PATCHING_TOOL_VERSION}" \
--build-arg LABEL_LIFERAY_TOMCAT_VERSION=$(get_tomcat_version "${TEMP_DIR}/liferay") \
--build-arg LABEL_LIFERAY_VCS_REF="${LIFERAY_VCS_REF}" \
--build-arg LABEL_NAME="${DOCKER_LABEL_NAME}" \
--build-arg LABEL_RELEASE_VERSION="${LIFERAY_DOCKER_RELEASE_VERSION}" \
--build-arg LABEL_VCS_REF=$(git rev-parse HEAD) \
--build-arg LABEL_VCS_URL="https://github.com/liferay/liferay-docker" \
--build-arg LABEL_VERSION="${LABEL_VERSION}" \
--builder "liferay-buildkit" \
--platform "${LIFERAY_DOCKER_IMAGE_PLATFORMS}" \
--push \
$(get_docker_image_tags_args "${DOCKER_IMAGE_TAGS[@]}") \
"${TEMP_DIR}" || exit 1
fi
}
function set_parent_image {
if (echo "${LIFERAY_DOCKER_RELEASE_VERSION}" | grep -q "q")
then
if [[ "$(echo "${LIFERAY_DOCKER_RELEASE_VERSION}" | cut -d '.' -f 1)" -gt 2024 ]]
then
return
fi
if [[ "$(echo "${LIFERAY_DOCKER_RELEASE_VERSION}" | cut -d '.' -f 1)" -eq 2024 ]] &&
[[ "$(echo "${LIFERAY_DOCKER_RELEASE_VERSION}" | cut -d '.' -f 2 | tr -d q)" -ge 3 ]]
then
return
fi
sed -i 's/liferay\/jdk21:latest AS liferay-jdk21/liferay\/jdk11:latest AS liferay-jdk11/g' "${TEMP_DIR}"/Dockerfile
sed -i 's/FROM liferay-jdk21/FROM liferay-jdk11/g' "${TEMP_DIR}"/Dockerfile
elif [[ "$(echo "${LIFERAY_DOCKER_RELEASE_VERSION}" | cut -d '.' -f 1,2)" == 7.4 ]]
then
if [[ "$(echo "${LIFERAY_DOCKER_RELEASE_VERSION}" | cut -d '.' -f 1,2,3 | cut -d '-' -f 1)" == 7.4.13 ]] &&
[[ "$(echo "${LIFERAY_DOCKER_RELEASE_VERSION}" | cut -d '-' -f 2 | tr -d u)" -ge 125 ]]
then
return
fi
if [[ "$(echo "${LIFERAY_DOCKER_RELEASE_VERSION}" | cut -d '.' -f 1,2,3)" == 7.4.3 ]] &&
[[ "$(echo "${LIFERAY_DOCKER_RELEASE_VERSION}" | cut -d '-' -f 2 | sed 's/ga//g')" -ge 125 ]]
then
return
fi
sed -i 's/liferay\/jdk21:latest AS liferay-jdk21/liferay\/jdk11:latest AS liferay-jdk11/g' "${TEMP_DIR}"/Dockerfile
sed -i 's/FROM liferay-jdk21/FROM liferay-jdk11/g' "${TEMP_DIR}"/Dockerfile
elif [[ "$(echo "${LIFERAY_DOCKER_RELEASE_VERSION}" | cut -d '.' -f 1,2 | tr -d .)" -le 73 ]]
then
sed -i 's/liferay\/jdk21:latest AS liferay-jdk21/liferay\/jdk11-jdk8:latest AS liferay-jdk11-jdk8/g' "${TEMP_DIR}"/Dockerfile
sed -i 's/FROM liferay-jdk21/FROM liferay-jdk11/g' "${TEMP_DIR}"/Dockerfile
fi
}
function update_patching_tool {
if [ -e "${TEMP_DIR}/liferay/tomcat" ]
then
sed -i "s@tomcat-[0-9]*.[0-9]*.[0-9]*/@tomcat/@g" "${TEMP_DIR}/liferay/patching-tool/default.properties"
fi
if [ -e "${TEMP_DIR}/liferay/patching-tool" ]
then
local patching_tool_minor_version=$("${TEMP_DIR}"/liferay/patching-tool/patching-tool.sh version | grep "Patching-tool version")
if [ ! -n "${patching_tool_minor_version}" ]
then
patching_tool_minor_version="2.0.0"
else
patching_tool_minor_version=${patching_tool_minor_version##*Patching-tool version: }
fi
patching_tool_minor_version=${patching_tool_minor_version%.*}
if (! echo ${patching_tool_minor_version} | grep -e '[0-9]*[.][0-9]*' >/dev/null)
then
echo "Patching Tool update is skipped as it's not a 1.0+ version or the bundle did not include a properly configured Patching Tool."
return
fi
mv "${TEMP_DIR}/liferay/patching-tool/patches" "${TEMP_DIR}/liferay/patching-tool-upgrade-patches"
rm -fr "${TEMP_DIR}/liferay/patching-tool"
local latest_patching_tool_version
#
# Set the latest patching tool version in a separate line to get the
# proper exit code.
#
latest_patching_tool_version=$(./patching_tool_version.sh ${patching_tool_minor_version})
local exit_code=$?
if [ ${exit_code} -gt 0 ]
then
echo "./patching_tool_version.sh returned with an error: ${latest_patching_tool_version}"
exit ${exit_code}
fi
echo ""
echo "Updating Patching Tool to version ${latest_patching_tool_version}."
echo ""
download "downloads/patching-tool/patching-tool-${latest_patching_tool_version}.zip" "releases-cdn.liferay.com/tools/patching-tool/patching-tool-${latest_patching_tool_version}.zip"
unzip -d "${TEMP_DIR}/liferay" -q "downloads/patching-tool/patching-tool-${latest_patching_tool_version}.zip"
"${TEMP_DIR}/liferay/patching-tool/patching-tool.sh" auto-discovery
rm -fr "${TEMP_DIR}/liferay/patching-tool/patches"
mv "${TEMP_DIR}/liferay/patching-tool-upgrade-patches" "${TEMP_DIR}/liferay/patching-tool/patches"
if [ ! -n "${LIFERAY_DOCKER_TEST_PATCHING_TOOL_VERSION}" ]
then
export LIFERAY_DOCKER_TEST_PATCHING_TOOL_VERSION="${latest_patching_tool_version}"
fi
fi
}
main "${@}"