-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-images.sh
executable file
·127 lines (116 loc) · 3.86 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
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
#!/bin/bash
#
# Copyright 2024 Dmitriy Barbul
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
set -e
if [ "$1" = "--help" -o "$1" = "-h" ]; then
echo "Just execute the script"
echo "Environment variables:"
echo " NOBUILD - empty to build docker images, non-empty to skip build"
echo " PUSH - non-empty to push docker images to docker registry"
echo " empty to skip push"
exit 1;
fi
function expand_versions
{
declare -a result
current="$1"
while [[ "$current" =~ \. ]]; do
result+=($current);
current=${current%.*}
done
echo "${result[@]}"
}
if [ -z "${PROJECT_VERSION}" ]; then
echo "Calculating project version"
PROJECT_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
fi
IMAGE_NAME=dimabarbul/wiremock-openapi-validation
IMAGE_VERSION=$(tr '[:upper:]' '[:lower:]' <<<${PROJECT_VERSION})
if [ -z "${PUSH}" ]; then
PUSH=false;
else
PUSH=true;
fi
if [ -z "${NOBUILD}" ]; then
BUILD=true;
else
BUILD=false;
fi
if [[ "${PROJECT_VERSION}" =~ -SNAPSHOT$ ]]; then
IS_SNAPSHOT_VERSION=true
else
IS_SNAPSHOT_VERSION=false
fi
if [ "$IS_SNAPSHOT_VERSION" = "false" ]; then
IMAGE_VERSIONS=( $(expand_versions $IMAGE_VERSION) )
else
IMAGE_VERSIONS=($IMAGE_VERSION)
fi
declare -a IMAGE_VERSIONS_ALPINE;
for v in "${IMAGE_VERSIONS[@]}"; do
IMAGE_VERSIONS_ALPINE+=("${v}-alpine")
done
echo
echo "Project version: ${PROJECT_VERSION}"
echo "Image name: ${IMAGE_NAME}"
echo "Image version: ${IMAGE_VERSIONS[@]}"
echo "Image alpine version: ${IMAGE_VERSIONS_ALPINE[@]}"
echo "Push: ${PUSH}"
echo
if [ ! -f target/wiremock-openapi-validation-extension-${PROJECT_VERSION}.jar ]; then
echo 'Run `mvn clean package` before building images.'
exit 1;
fi
if [ "${BUILD}" = "true" ]; then
echo "Building ${IMAGE_VERSIONS[0]}"
docker build -t ${IMAGE_NAME}:${IMAGE_VERSIONS[0]} . 2>&1
for v in "${IMAGE_VERSIONS[@]:1}"; do
echo "Tagging ${IMAGE_VERSIONS[0]} as ${v}"
docker tag ${IMAGE_NAME}:${IMAGE_VERSIONS[0]} ${IMAGE_NAME}:${v}
done
echo "Building ${IMAGE_VERSIONS_ALPINE[0]}"
docker build -t ${IMAGE_NAME}:${IMAGE_VERSIONS_ALPINE[0]} -f Dockerfile-alpine . 2>&1
for v in "${IMAGE_VERSIONS_ALPINE[@]:1}"; do
echo "Tagging ${IMAGE_VERSIONS_ALPINE[0]} as ${v}"
docker tag ${IMAGE_NAME}:${IMAGE_VERSIONS_ALPINE[0]} ${IMAGE_NAME}:${v}
done
if [ "${IS_SNAPSHOT_VERSION}" = "false" ]; then
echo "Adding tag latest"
docker tag ${IMAGE_NAME}:${IMAGE_VERSIONS[0]} ${IMAGE_NAME}:latest
echo "Adding tag alpine"
docker tag ${IMAGE_NAME}:${IMAGE_VERSIONS_ALPINE[0]} ${IMAGE_NAME}:alpine
fi
fi
if [ "${PUSH}" = "true" ]; then
echo "Pushing ${IMAGE_VERSIONS[0]}"
docker push ${IMAGE_NAME}:${IMAGE_VERSIONS[0]}
for v in "${IMAGE_VERSIONS[@]:1}"; do
echo "Pushing ${v}"
docker push ${IMAGE_NAME}:${v}
done
echo "Pushing ${IMAGE_VERSIONS_ALPINE[0]}"
docker push ${IMAGE_NAME}:${IMAGE_VERSIONS_ALPINE[0]}
for v in "${IMAGE_VERSIONS_ALPINE[@]:1}"; do
echo "Pushing ${v}"
docker push ${IMAGE_NAME}:${v}
done
if [ "${IS_SNAPSHOT_VERSION}" = "false" ]; then
echo "Pushing latest"
docker push ${IMAGE_NAME}:latest
echo "Pushing alpine"
docker push ${IMAGE_NAME}:alpine
fi
fi