-
Notifications
You must be signed in to change notification settings - Fork 7
/
build
executable file
·238 lines (220 loc) · 6.89 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
#!/usr/bin/env bash
set -eu
readonly IMAGE_NAME="ghcr.io/datadog/dd-trace-java-docker-build"
readonly BASE_VARIANTS=(8 11 17 21)
readonly VARIANTS=(
7
zulu8
zulu11
oracle8
ibm8
semeru8
semeru11
semeru17
ubuntu17
graalvm17
graalvm21
)
# Use buildkit to match CI as closely as possible.
export DOCKER_BUILDKIT=1
function compute_metadata() {
# Get a TAG_PREFIX for every built image. Resulting images will be tagged as
# ${TAG_PREFIX}${variant}, where ${variant} is base, latest or a JVM name.
# ${TAG_PREFIX} is empty for the master branch, and it is a sanitized version
# of the git branch otherwise. Sanitization is a conversion to lowercase, and
# replacing all slashes with underscores, which is the same sanitization Docker
# Hub does for their own builds.
GIT_BRANCH="${GITHUB_REF_NAME:-$(git branch --show-current)}"
# Fallback to local if there is no branch (e.g. dettached development)
readonly GIT_BRANCH="${GIT_BRANCH:-local}"
if [[ ${GIT_BRANCH} = master ]]; then
TAG_PREFIX=""
else
TAG_PREFIX="${GIT_BRANCH}-"
TAG_PREFIX="${TAG_PREFIX,,}"
TAG_PREFIX="${TAG_PREFIX//\//_}"
fi
# Cache the same RFC 3339 timestamp for re-use in all images built in the same batch.
BUILD_DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
GIT_HEAD_REF="$(git show-ref --head --hash ^HEAD)"
}
# docker build wrapper with common arguments
# See https://github.com/opencontainers/image-spec/blob/main/annotations.md for common labels
# See https://docs.github.com/en/packages/learn-github-packages/connecting-a-repository-to-a-package
function docker_build() {
local target="$1"
local tag="$2"
shift
shift
docker build \
--platform linux/amd64 \
--label org.opencontainers.image.created="$BUILD_DATE" \
--label org.opencontainers.image.source=https://github.com/DataDog/dd-trace-java-docker-build \
--label org.opencontainers.image.revision="$GIT_HEAD_REF" \
--target "$target" \
--tag "$tag" \
"$@" \
.
}
function image_name() {
local variant="${1}"
echo -n "${IMAGE_NAME}:${TAG_PREFIX}${variant}"
}
function do_build() {
compute_metadata
docker_build base "$(image_name base)"
docker_build full "$(image_name latest)"
if [ -n "${GITHUB_OUTPUT+unset}" ]; then
# Make LATEST_IMAGE_TAG available to later GitHub Actions jobs
echo "LATEST_IMAGE_TAG=$(image_name latest)" >>"$GITHUB_OUTPUT"
fi
for variant in "${BASE_VARIANTS[@]}"; do
variant_lower="${variant,,}"
docker tag "$(image_name base)" "$(image_name "${variant_lower}")"
done
for variant in "${VARIANTS[@]}"; do
variant_upper="${variant^^}"
variant_lower="${variant,,}"
docker_build variant "$(image_name "$variant_lower")" \
--build-arg "VARIANT_UPPER=$variant_upper" \
--build-arg "VARIANT_LOWER=$variant_lower"
done
}
function list_layers() {
local image="${1}"
docker inspect "${image}" | jq -r '.[].RootFS.Layers[]'
}
function do_test() {
local image_base image_variant
compute_metadata
image_base="$(image_name base)"
mapfile -t base_layers < <(list_layers "${image_base}")
for variant in "${BASE_VARIANTS[@]}" "${VARIANTS[@]}"; do
image_variant="$(image_name "${variant}")"
echo "Checking shared layers for ${image_variant}..."
mapfile -t other_layers < <(list_layers "${image_variant}")
declare -A other_map
for ol in "${other_layers[@]}"; do
other_map[$ol]=true
done
for bl in "${base_layers[@]}"; do
if [[ ${other_map[$bl]:-} != true ]]; then
echo "Layer $bl from $image_base not found in $image_variant"
return 1
fi
done
done
for variant in base latest "${BASE_VARIANTS[@]}" "${VARIANTS[@]}"; do
image_variant="$(image_name "${variant}")"
echo "Running smoke tests for ${image_variant}..."
docker run \
--user "$(id -u):$(id -g)" \
--workdir /work \
--volume "$(pwd):/work" \
--rm \
"${image_variant}" \
/work/build --inner-test "${variant}"
done
}
function do_inner_test() {
local variant="${1}"
variant_lower="${variant,,}"
variant_upper="${variant^^}"
set -x
"$JAVA_HOME/bin/java" -version
"$JAVA_8_HOME/bin/java" -version
"$JAVA_11_HOME/bin/java" -version
"$JAVA_17_HOME/bin/java" -version
"$JAVA_21_HOME/bin/java" -version
if [[ $variant != base && $variant != latest ]]; then
env_lower="JAVA_${variant_lower}_HOME"
env_upper="JAVA_${variant_upper}_HOME"
"${!env_lower}/bin/java" -version
"${!env_upper}/bin/java" -version
fi
}
function do_describe() {
local image
compute_metadata
image="$(image_name latest)"
docker run \
--user "$(id -u):$(id -g)" \
--workdir /work \
--volume "$(pwd):/work" \
--rm \
"$image" \
/work/build --inner-describe
}
function do_inner_describe() {
echo "# Contents"
echo
echo "## Operating System"
echo
echo "* $(lsb_release --description --short)"
echo
echo "## Tools"
echo
echo "* bash $BASH_VERSION"
echo "* $(git --version)"
echo "* $(docker --version)"
echo "* $(docker compose version)"
echo "* $(yq --version)"
echo "* datadog-ci $(datadog-ci version)"
echo
echo "## JDKs"
echo
for variant in "${BASE_VARIANTS[@]}" "${VARIANTS[@]}"; do
variant_upper="${variant^^}"
env_upper="JAVA_${variant_upper}_HOME"
echo "* $env_upper"
echo '```'
"${!env_upper}/bin/java" -version 2>&1
echo '```'
echo
done
echo
}
function do_push() {
local tag
compute_metadata
for tag in base latest "${BASE_VARIANTS[@]}" "${VARIANTS[@]}"; do
tag="${tag,,}"
tag="$(image_name "${tag}")"
docker push "$tag"
done
}
function do_tag() {
local tag token version
TAG_PREFIX=
echo "Pulling latest images"
for tag in base latest "${BASE_VARIANTS[@]}" "${VARIANTS[@]}"; do
tag="${tag,,}"
tag="$(image_name "${tag}")"
docker pull "$tag"
done
version="$(date +%y.%m)"
echo "Tagging version $version"
for tag in base latest "${BASE_VARIANTS[@]}" "${VARIANTS[@]}"; do
tag="${tag,,}"
tag="$(image_name "${tag}")"
new_tag="${tag/:/:v$version-}"
docker tag "$tag" "$new_tag"
docker push "$new_tag"
done
}
if [[ -z ${1:-} ]]; then
do_build
elif [[ ${1} = "--test" ]]; then
do_test
elif [[ ${1} = "--inner-test" ]]; then
shift
do_inner_test "$@"
elif [[ ${1} = "--describe" ]]; then
do_describe
elif [[ ${1} = "--inner-describe" ]]; then
do_inner_describe
elif [[ ${1} = "--push" ]]; then
do_push
elif [[ ${1} = "--tag" ]]; then
do_tag
fi