-
Notifications
You must be signed in to change notification settings - Fork 192
/
build.sh
executable file
·183 lines (159 loc) · 4.35 KB
/
build.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
#!/bin/bash
#
# Build container images and run tests.
set -euo pipefail
print_usage()
{
echo "Usage: $0 [<VERSION>...]"
echo "Build Container images and run tests."
echo ""
echo "Arguments:"
echo " --base-os <os> Choose between different Dockerfiles"
echo " --continue-on-error Don't stop executing tests when an error occurs"
echo " --no-build Skip building the image"
echo " --no-test Skip running the tests"
echo " --test-port <port> Local TCP port used for tests"
echo " --debug Print the bash script commands before executing them"
echo " --dotnet-tarball <url> Tarball containing a .NET SDK to be used instead of distro packages"
echo " --ci Indicates the build is a CI/dev version."
}
RUN_TESTS=true
DOTNET_TARBALL=
CI=false
DEBUG=
BASE_OS=
VERSIONS=""
TEST_PORT=8080
RUN_BUILD=true
STOP_ON_ERROR=true
while [ $# -ne 0 ]
do
name="$1"
case "$name" in
--base-os)
shift
BASE_OS="$1"
;;
--no-build)
RUN_BUILD=false
;;
--no-test)
RUN_TESTS=false
;;
--test-port)
shift
TEST_PORT="$1"
;;
--dotnet-tarball)
shift
DOTNET_TARBALL="$1"
;;
--debug)
set -x
DEBUG=true
;;
--continue-on-error)
STOP_ON_ERROR=false
;;
--ci)
CI=true
;;
--help)
print_usage
exit 0
break
;;
*)
VERSIONS="$VERSIONS $1"
;;
esac
shift
done
VERSIONS=$(echo "$VERSIONS" | xargs)
# default to building the currently supported versions.
if [ -z "$VERSIONS" ]; then
VERSIONS="6.0 8.0"
fi
# Use podman instead of docker when available.
if command -v podman >/dev/null; then
docker() {
podman "$@"
}
fi
image_exists() {
docker inspect $1 &>/dev/null
}
default_base_os_for_version() {
local version=$1
echo "rhel8"
}
os_image_prefix_for() {
local version=$1
local base_os=$2
if [ "$base_os" == "rhel8" ]; then
echo "ubi8"
else
echo "$base_os"
fi
}
check_result_msg() {
local retval=$1
local msg=$2
if [ ${retval} -ne 0 ]; then
echo 1>&2 "${msg}"
exit 1
fi
}
build_image() {
local path=$1
local docker_filename=$2
local name=$3
if [ "$RUN_BUILD" == "false" ]; then
return
fi
echo "Building Docker image ${name} ..."
if [ ! -d "${path}" ]; then
echo "No directory found at given location '${path}'. Skipping this image."
return
fi
pushd ${path} &>/dev/null
docker build --no-cache \
--build-arg=DOTNET_TARBALL=$DOTNET_TARBALL \
--build-arg=IS_CI=$CI \
-f ${docker_filename} -t ${name} .
check_result_msg $? "Building Docker image ${name} FAILED!"
popd &>/dev/null
}
test_images() {
local path=$1
local base_os=$2
local test_image=$3
local runtime_image=$4
if [ "$RUN_TESTS" == "false" ]; then
return
fi
local image_os=$(echo "$base_os" | tr '[:lower:]' '[:upper:]')
echo "Running tests..."
STOP_ON_ERROR=$STOP_ON_ERROR DEBUG=$DEBUG IMAGE_OS=${image_os} SKIP_VERSION_CHECK=$CI IMAGE_NAME=${test_image} RUNTIME_IMAGE_NAME=${runtime_image} ${path}/run
check_result_msg $? "Tests FAILED!"
}
for VERSION in ${VERSIONS}; do
# use the specified BASE_OS, or default to the prefered os when unset.
base_os_for_version="$BASE_OS"
if [ -z "$base_os_for_version" ]; then
base_os_for_version=$(default_base_os_for_version $VERSION)
fi
docker_filename="Dockerfile.$base_os_for_version"
image_prefix=$(os_image_prefix_for $VERSION $base_os_for_version)
version_no_dot=$(echo ${VERSION} | sed 's/\.//g')
build_name="localhost/${image_prefix}/dotnet-$version_no_dot"
runtime_name="localhost/${image_prefix}/dotnet-$version_no_dot-runtime"
# Build the runtime image
build_image "$VERSION/runtime" "${docker_filename}" "${runtime_name}"
test_images "$VERSION/runtime/test" "$base_os_for_version" "${runtime_name}" "${runtime_name}"
# Build the build image
build_image "$VERSION/build" "${docker_filename}" "${build_name}"
test_images "$VERSION/build/test" "$base_os_for_version" "${build_name}" "${runtime_name}"
done
echo "ALL builds and tests were successful!"
exit 0