-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathrebuild-images.sh
More file actions
executable file
·276 lines (237 loc) · 10.4 KB
/
Copy pathrebuild-images.sh
File metadata and controls
executable file
·276 lines (237 loc) · 10.4 KB
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
#!/bin/bash
# rebuild-images.sh - Build and push CI images, then create a GitHub PR with updated references
#
# Usage: REBUILD_IMAGES="x64,arm64" ./scripts/rebuild-images.sh
# REBUILD_IMAGES="" ./scripts/rebuild-images.sh # builds all images
#
# Required environment (auto-set in GitLab CI):
# CI_PIPELINE_ID - Pipeline ID used as image tag prefix
# CI_JOB_URL - URL of the CI job (included in PR comment)
#
# Optional environment:
# REBUILD_IMAGES - Comma/space-separated short names; empty = all
# DRY_RUN - Set to 'true' to print plan and exit without building
# REGISTRY - Override registry (default: registry.ddbuild.io)
#
# Base image variables come from .gitlab/build-deploy/images.yml which is
# included in the root pipeline and available as CI variables:
# OPENJDK_BASE_IMAGE, OPENJDK_BASE_IMAGE_ARM64, OPENJDK_BASE_IMAGE_MUSL,
# OPENJDK_BASE_IMAGE_ARM64_MUSL, BASE_IMAGE_LIBC_2_17, BASE_BENCHMARK_IMAGE_NAME,
# DOCKER_IMAGE
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
REGISTRY="${REGISTRY:-registry.ddbuild.io}"
# Colors for stderr
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
log_info() { echo -e "${GREEN}[INFO]${NC} $*" >&2; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $*" >&2; }
log_error() { echo -e "${RED}[ERROR]${NC} $*" >&2; }
usage() {
echo "Usage: REBUILD_IMAGES=\"<names>\" $0" >&2
echo "" >&2
echo "Valid short names:" >&2
echo " x64 glibc x86_64 build image" >&2
echo " x64-2.17 glibc 2.17 (centos7) x86_64 build image" >&2
echo " x64-musl musl x86_64 build image" >&2
echo " arm64 glibc arm64 build image" >&2
echo " arm64-musl musl arm64 build image" >&2
echo " datadog-ci datadog-ci utility image" >&2
echo " benchmarks-amd64 benchmark runner image for amd64" >&2
echo " benchmarks-arm64 benchmark runner image for arm64" >&2
echo "" >&2
echo "Leave REBUILD_IMAGES empty to rebuild all images." >&2
}
# IMAGE_DEFS format (pipe-delimited):
# short_name | VAR_NAME | yaml_file | tag_suffix | dockerfile | platform | registry_path | base_image_var
#
# base_image_var is the name of the env var holding the base image (empty if none needed).
IMAGE_DEFS=(
"x64|BUILD_IMAGE_X64|.gitlab/build-deploy/.gitlab-ci.yml|x64-base|.gitlab/base/Dockerfile|linux/amd64|async-profiler-build|OPENJDK_BASE_IMAGE"
"x64-2.17|BUILD_IMAGE_X64_2_17|.gitlab/build-deploy/.gitlab-ci.yml|x64-2.17-base|.gitlab/base/centos7/Dockerfile|linux/amd64|async-profiler-build|BASE_IMAGE_LIBC_2_17"
"x64-musl|BUILD_IMAGE_X64_MUSL|.gitlab/build-deploy/.gitlab-ci.yml|x64-musl-base|.gitlab/base/Dockerfile.musl|linux/amd64|async-profiler-build|OPENJDK_BASE_IMAGE_MUSL"
"arm64|BUILD_IMAGE_ARM64|.gitlab/build-deploy/.gitlab-ci.yml|arm64-base|.gitlab/base/Dockerfile|linux/arm64|async-profiler-build|OPENJDK_BASE_IMAGE_ARM64"
"arm64-musl|BUILD_IMAGE_ARM64_MUSL|.gitlab/build-deploy/.gitlab-ci.yml|arm64-musl-base|.gitlab/base/Dockerfile.musl|linux/arm64|async-profiler-build|OPENJDK_BASE_IMAGE_ARM64_MUSL"
"datadog-ci|DATADOG_CI_IMAGE|.gitlab/build-deploy/.gitlab-ci.yml|datadog-ci|.gitlab/Dockerfile.datadog-ci|linux/amd64|async-profiler-build|"
"benchmarks-amd64|BENCHMARK_IMAGE_AMD64|.gitlab/benchmarks/images.yml|amd64-benchmarks|.gitlab/benchmarks/docker/Dockerfile|linux/amd64|async-profiler-build-amd64|BASE_BENCHMARK_IMAGE_NAME"
"benchmarks-arm64|BENCHMARK_IMAGE_ARM64|.gitlab/benchmarks/images.yml|arm64-benchmarks|.gitlab/benchmarks/docker/Dockerfile|linux/arm64|async-profiler-build-arm64|BASE_BENCHMARK_IMAGE_NAME"
)
# Extract current image reference from YAML file (reused from check-image-updates.sh)
get_current_ref() {
local var_name="$1"
local yaml_file="$2"
local full_path="${PROJECT_ROOT}/${yaml_file}"
if [[ ! -f "$full_path" ]]; then
log_error "YAML file not found: $full_path"
return 1
fi
grep -E "^\s*${var_name}:" "$full_path" | \
sed "s/^[[:space:]]*${var_name}:[[:space:]]*//" | \
tr -d ' "'"'" | \
head -1
}
# Extract tag from full image reference (reused from check-image-updates.sh)
extract_tag() {
local ref="$1"
echo "$ref" | sed 's/@sha256:.*//' | rev | cut -d':' -f1 | rev
}
# Build one image and return its digest.
# Arguments: short_name tag dockerfile platform registry_path base_image
# Prints the sha256 digest to stdout on success.
build_image() {
local short_name="$1"
local tag="$2"
local dockerfile="$3"
local platform="$4"
local registry_path="$5"
local base_image="$6"
local full_tag="${REGISTRY}/ci/${registry_path}:${tag}"
local meta_file
meta_file=$(mktemp)
local build_args=()
[[ -n "$base_image" ]] && build_args+=(--build-arg "BASE_IMAGE=${base_image}")
[[ -n "${CI_JOB_TOKEN:-}" ]] && build_args+=(--build-arg "CI_JOB_TOKEN=${CI_JOB_TOKEN}")
# benchmarks images change into the docker sub-directory before building
local build_context="."
local dockerfile_flag="-f ${PROJECT_ROOT}/${dockerfile}"
if [[ "$short_name" == benchmarks-* ]]; then
build_context="${PROJECT_ROOT}/.gitlab/benchmarks/docker"
dockerfile_flag="-f ${PROJECT_ROOT}/${dockerfile}"
elif [[ "$dockerfile" == */ ]]; then
# dockerfile is a directory (build context)
build_context="${PROJECT_ROOT}/${dockerfile}"
dockerfile_flag=""
fi
log_info " Running: docker buildx build --platform=${platform} --tag=${full_tag} ... --push"
# shellcheck disable=SC2086
docker buildx build \
--platform "${platform}" \
--tag "${full_tag}" \
"${build_args[@]}" \
--push \
--metadata-file "${meta_file}" \
${dockerfile_flag} \
"${build_context}"
ddsign sign "${full_tag}" --docker-metadata-file "${meta_file}" >&2
# Get manifest digest from registry: more reliable than --metadata-file
# which ddsign corrupts for some image types.
docker buildx imagetools inspect "${full_tag}" 2>/dev/null \
| awk '/Digest:/{print $NF; exit}'
}
find_def() {
local target_name="$1"
for def in "${IMAGE_DEFS[@]}"; do
local short_name
short_name=$(cut -d'|' -f1 <<< "$def")
if [[ "$short_name" == "$target_name" ]]; then
echo "$def"
return 0
fi
done
return 1
}
main() {
cd "$PROJECT_ROOT"
# Build list of all valid short names
local all_names=()
for def in "${IMAGE_DEFS[@]}"; do
all_names+=("$(cut -d'|' -f1 <<< "$def")")
done
# Parse REBUILD_IMAGES (split on comma and/or whitespace)
local selected=()
if [[ -n "${REBUILD_IMAGES:-}" ]]; then
IFS=', ' read -r -a selected <<< "${REBUILD_IMAGES}"
# Validate
for name in "${selected[@]}"; do
if ! find_def "$name" > /dev/null 2>&1; then
log_error "Unknown image name: '${name}'"
usage
exit 1
fi
done
else
selected=("${all_names[@]}")
fi
log_info "Images to build: ${selected[*]}"
if [[ "${DRY_RUN:-}" == "true" ]]; then
log_info "DRY RUN: would build the following images:"
for name in "${selected[@]}"; do
local def
def=$(find_def "$name")
IFS='|' read -r s_name var_name yaml_file tag_suffix dockerfile platform registry_path base_image_var <<< "$def"
local tag="v${CI_PIPELINE_ID:-DRY_RUN}-${tag_suffix}"
log_info " ${name}: ${REGISTRY}/ci/${registry_path}:${tag} (${platform})"
done
exit 0
fi
local updates="[]"
local failed=0
for name in "${selected[@]}"; do
local def
def=$(find_def "$name")
IFS='|' read -r s_name var_name yaml_file tag_suffix dockerfile platform registry_path base_image_var <<< "$def"
local base_image=""
if [[ -n "$base_image_var" ]]; then
base_image="${!base_image_var:-}"
if [[ -z "$base_image" ]]; then
log_warn "Base image variable ${base_image_var} is not set for ${name}, building without BASE_IMAGE arg"
fi
fi
local new_tag="v${CI_PIPELINE_ID}-${tag_suffix}"
log_info "Building ${name} (${new_tag})..."
local digest
if ! digest=$(build_image "$name" "$new_tag" "$dockerfile" "$platform" "$registry_path" "$base_image"); then
log_error "Build failed for: ${name}"
(( failed++ )) || true
continue
fi
if [[ -z "$digest" || "$digest" == "null" ]]; then
log_error "Build succeeded but digest is empty for ${name} (ddsign may have corrupted metadata)"
(( failed++ )) || true
continue
fi
local new_full_ref="${REGISTRY}/ci/${registry_path}:${new_tag}@${digest}"
local current_ref
current_ref=$(get_current_ref "$var_name" "$yaml_file" || echo "")
local current_tag=""
[[ -n "$current_ref" ]] && current_tag=$(extract_tag "$current_ref")
local current_digest
current_digest=$(echo "$current_ref" | grep -oE 'sha256:[a-f0-9]+' || echo "")
local job_url="${CI_JOB_URL:-}"
updates=$(echo "$updates" | jq \
--arg var_name "$var_name" \
--arg yaml_file "$yaml_file" \
--arg current_tag "$current_tag" \
--arg current_digest "$current_digest" \
--arg new_tag "$new_tag" \
--arg new_digest "$digest" \
--arg new_full_ref "$new_full_ref" \
--arg job_url "$job_url" \
--arg job_name "rebuild-images" \
'. + [{
var_name: $var_name,
yaml_file: $yaml_file,
current_tag: $current_tag,
current_digest: $current_digest,
new_tag: $new_tag,
new_digest: $new_digest,
new_full_ref: $new_full_ref,
job_url: $job_url,
job_name: $job_name
}]')
done
local update_count
update_count=$(echo "$updates" | jq 'length')
# Always write updates.json before exiting so the PR job always has valid JSON
echo "$updates" | jq . > "${PROJECT_ROOT}/updates.json"
log_info "Wrote ${update_count} update(s) to updates.json"
if [[ "$update_count" -eq 0 ]]; then
log_error "No successful builds; nothing to create a PR for"
exit 1
fi
exit $failed
}
main "$@"