-
Notifications
You must be signed in to change notification settings - Fork 1
/
build-images.sh
97 lines (87 loc) · 2.57 KB
/
build-images.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
#!/bin/bash
function build_images() {
local path=$1
local tag=$2
local log_file=$4
local pattern=$3
local services=`ls ${path} | egrep "${pattern}"`
for service in $services; do
echo "start build image ${service}:${tag}"
build_image "${path}" "${service}" "${tag}" "${log_file}"
RESULT=$?
if [ ${RESULT} -ne 0 ]; then
echo "${RED}build image ${service}:${tag} failure, please check log file [${log_file}] for details"
else
echo "build image ${service}:${tag} succeed"
fi
done
}
function build_image() {
local path=$1
local image_name=$2
local image_tag=$3
local log_file=$4
docker build -t "${image_name}:${image_tag}" -f "${path}/${image_name}/target/Dockerfile" "${path}" >> "${log_file}" 2>&1
return $?
}
function push_images() {
echo "${RED}Before push image, you should login remote repository, are you already login? y/n"
read confirm
if [ "${confirm}" != "y" ]; then
echo "bye"
exit 0
fi
local path=$1
local tag=$2
local remote_repo=$3
local pattern=$4
local log_file=$5
local services=`ls ${path} | egrep "${pattern}"`
for service in ${services}; do
echo "start push image ${service}:${tag}"
image_id=`docker images -q "${service}:${tag}"`
if [ -z "${image_id}" ]; then
echo "image ${service}:${tag} does not exist"
continue
fi
echo "tag image ${service}:${tag} with tag ${remote_repo}/${service}:${tag}"
docker tag "${image_id}" "${remote_repo}/${service}:${tag}"
if [ $? -ne 0 ]; then
echo "${RED}tag image ${service}:${tag} [with id ${image_id}] failure"
continue
fi
echo "push image ${remote_repo}/${service}:${tag}"
docker push "${remote_repo}/${service}:${tag}" >> ${log_file} 2>&1
if [ $? -eq 0 ]; then
echo "push image ${remote_repo}/${service}:${tag} succeed"
else
echo "${RED}push image ${remote_repo}/${service}:${tag} failure"
fi
done
}
if [ $# -lt 2 ]; then
echo "usage: $0 <build|push> <tag> [service pattern] [remote repo]"
exit 1
fi
RED='\033[0;31m'
NC='\033[0m'
CMD=$1
TAG=$2
SERVICES_PATTERN=$3
SERVICES_PATTERN="${SERVICES_PATTERN:-"*-service|*-website|loadtestclient|tx-coordinator|init-db"}"
REMOTE_REPO=$4
REMOTE_REPO="${REMOTE_REPO:-"100.125.0.198:20202/maoxuepeng6459"}"
RELATIVE_PATH="`dirname $0`"
cd "${RELATIVE_PATH}"
ABSOLUTELY_PATH=`pwd`
LOG_FILE="${ABSOLUTELY_PATH}/build-images.log"
if [ "${CMD}" = "build" ]; then
build_images "${ABSOLUTELY_PATH}" "${TAG}" "${SERVICES_PATTERN}" "${LOG_FILE}"
exit $?
elif [ "${CMD}" = "push" ]; then
push_images "${ABSOLUTELY_PATH}" "${TAG}" "${REMOTE_REPO}" "${SERVICES_PATTERN}" "${LOG_FILE}"
exit $?
else
echo "Unknown command ${CMD}"
exit 1
fi