-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
Β·428 lines (373 loc) Β· 11.9 KB
/
build.sh
File metadata and controls
executable file
Β·428 lines (373 loc) Β· 11.9 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/bin/bash
# --- Input Validation ---
if [ "$#" -lt 2 ] || [ "$#" -gt 5 ]; then
echo "Usage: $0 <IMAGE_NAME> <REGISTRY> [FINAL_TAG] [--latest] [--build-app]"
echo "Example: $0 myorg/myapp my-registry.com"
echo " $0 myorg/myapp my-registry.com 1.0.0"
echo " $0 myorg/myapp my-registry.com 1.0.0 --latest"
echo " $0 myorg/myapp my-registry.com --build-app"
echo " $0 myorg/myapp my-registry.com 1.0.0 --latest --build-app"
echo ""
echo "If FINAL_TAG is omitted, version from package.json will be used."
exit 1
fi
# --- Configuration Variables from CLI ---
IMAGE_NAME="$1"
REGISTRY="$2"
FINAL_TAG=""
TAG_LATEST=false
BUILD_APP=false
# Parse remaining arguments
shift 2
while [ "$#" -gt 0 ]; do
case "$1" in
--latest)
TAG_LATEST=true
shift
;;
--build-app)
BUILD_APP=true
shift
;;
*)
# If it doesn't start with --, assume it's the FINAL_TAG
if [[ "$1" != --* ]] && [ -z "$FINAL_TAG" ]; then
FINAL_TAG="$1"
shift
else
echo "Unknown option: $1"
exit 1
fi
;;
esac
done
# --- Prerequisites Check ---
echo "--- Prerequisites Check π ---"
# Check if podman is available
printf "%-80s" "Checking for podman... "
if ! command -v podman &> /dev/null; then
echo "β"
echo "Error: podman is not installed or not in PATH"
echo "Please install Podman before running this script"
echo ""
echo "Install instructions:"
echo " - Ubuntu/Debian: sudo apt install podman"
echo " - RHEL/Fedora: sudo dnf install podman"
echo " - macOS: brew install podman"
echo " - Or visit: https://podman.io/getting-started/installation"
exit 1
fi
echo "β
"
# Check if npm is available (only if --build-app flag is set)
if [ "$BUILD_APP" = true ]; then
printf "%-80s" "Checking for npm... "
if ! command -v npm &> /dev/null; then
echo "β"
echo "Error: npm is not installed or not in PATH"
echo "Please install Node.js and npm before running with --build-app"
echo ""
echo "Install instructions:"
echo " - Ubuntu/Debian: sudo apt install nodejs npm"
echo " - RHEL/Fedora: sudo dnf install nodejs npm"
echo " - macOS: brew install node"
echo " - Or visit: https://nodejs.org/"
exit 1
fi
echo "β
"
# Check if package.json exists
printf "%-80s" "Checking for package.json... "
if [ ! -f "package.json" ]; then
echo "β"
echo "Error: package.json not found in current directory"
exit 1
fi
echo "β
"
fi
# Check if Dockerfile exists
printf "%-80s" "Checking for Dockerfile... "
if [ ! -f "Dockerfile" ]; then
echo "β"
echo "Error: Dockerfile not found in current directory"
exit 1
fi
echo "β
"
echo ""
# If FINAL_TAG is not provided, read from package.json
if [ -z "$FINAL_TAG" ]; then
if [ ! -f "package.json" ]; then
echo "β Error: package.json not found and no FINAL_TAG provided."
exit 1
fi
# Extract version from package.json using grep and sed
FINAL_TAG=$(grep -m 1 '"version"' package.json | sed 's/.*"version": "\(.*\)".*/\1/')
if [ -z "$FINAL_TAG" ]; then
echo "β Error: Could not extract version from package.json"
exit 1
fi
echo "π¦ Using version from package.json: ${FINAL_TAG}"
fi
# Derived tags for individual images
ARCH_TAG="${FINAL_TAG}"
# --- Fully Qualified Names (FQN) ---
# FQNs for the individual architectural images
AMD64_FQN="${REGISTRY}/${IMAGE_NAME}:amd64-${ARCH_TAG}"
ARM64_FQN="${REGISTRY}/${IMAGE_NAME}:arm64-${ARCH_TAG}"
# FQN for the final manifest list
MANIFEST_FQN="${REGISTRY}/${IMAGE_NAME}:${FINAL_TAG}"
# FQNs for "latest" tags
AMD64_LATEST_FQN="${REGISTRY}/${IMAGE_NAME}:amd64-latest"
ARM64_LATEST_FQN="${REGISTRY}/${IMAGE_NAME}:arm64-latest"
MANIFEST_LATEST_FQN="${REGISTRY}/${IMAGE_NAME}:latest"
echo "--- Configuration Summary ---"
echo "Image Name: ${IMAGE_NAME}"
echo "Registry: ${REGISTRY}"
echo "Final Tag: ${FINAL_TAG}"
echo "Architecture: ${ARCH_TAG}"
echo "Tag as Latest: ${TAG_LATEST}"
echo "Build App: ${BUILD_APP}"
echo "-----------------------------"
# --- Script Logic ---
## 0. Building Application (if requested)
if [ "$BUILD_APP" = true ]; then
echo "--- 0. Building Application ποΈ ---"
# Delete dist directory if it exists
if [ -d "dist" ]; then
printf "%-80s" "Removing 'dist' directory... "
if rm -rf dist 2>/dev/null; then
echo "β
"
else
echo "β Failed to remove dist directory"
exit 1
fi
fi
# Run npm build
printf "%-80s" "Running 'npm run build'... "
if npm run build > /tmp/neo-ui-framework-build.log 2>&1; then
echo "β
"
else
echo "β npm build failed"
echo "Error log:"
tail -20 /tmp/neo-ui-framework-build.log
exit 1
fi
echo ""
fi
## 1. Authenticating to Registry
echo "--- 1. Authenticating to Registry π ---"
printf "%-80s" "Checking authentication... "
if LOGGED_IN_USER=$(podman login --get-login "${REGISTRY}" 2>/dev/null); then
echo "β
(${LOGGED_IN_USER})"
else
echo ""
printf "%-80s" "Attempting login... "
echo ""
if podman login "${REGISTRY}"; then
echo "β
"
else
echo "β Authentication failed"
exit 1
fi
fi
echo ""
## 2. Building Images
echo "--- 2. Building Architecture-Specific Images ποΈ ---"
# Build AMD64 image locally
printf "%-80s" "Building AMD64 image... "
if podman build --platform linux/amd64 -t "${IMAGE_NAME}:amd64-local" . > /tmp/neo-ui-framework-build.log 2>&1; then
echo "β
"
else
echo "β AMD64 build failed"
echo "Error log:"
tail -20 /tmp/neo-ui-framework-build.log
exit 1
fi
# Build ARM64 image locally
printf "%-80s" "Building ARM64 image... "
if podman build --platform linux/arm64 -t "${IMAGE_NAME}:arm64-local" . > /tmp/neo-ui-framework-build.log 2>&1; then
echo "β
"
else
echo "β ARM64 build failed"
echo "Error log:"
tail -20 /tmp/neo-ui-framework-build.log
exit 1
fi
echo ""
## 3. Tagging and Pushing Images
echo "--- 3. Tagging and Pushing Images π€ ---"
# Tag and Push AMD64
printf "%-80s" "Tagging AMD64... "
if podman tag "${IMAGE_NAME}:amd64-local" "${AMD64_FQN}" 2>/dev/null; then
echo "β
"
else
echo "β"
exit 1
fi
printf "%-80s" "Pushing ${AMD64_FQN}... "
if podman push "${AMD64_FQN}" > /tmp/neo-ui-framework-build.log 2>&1; then
echo "β
"
else
echo "β Push failed"
tail -10 /tmp/neo-ui-framework-build.log
exit 1
fi
# Tag and Push ARM64
printf "%-80s" "Tagging ARM64... "
if podman tag "${IMAGE_NAME}:arm64-local" "${ARM64_FQN}" 2>/dev/null; then
echo "β
"
else
echo "β"
exit 1
fi
printf "%-80s" "Pushing ${ARM64_FQN}... "
if podman push "${ARM64_FQN}" > /tmp/neo-ui-framework-build.log 2>&1; then
echo "β
"
else
echo "β Push failed"
tail -10 /tmp/neo-ui-framework-build.log
exit 1
fi
echo ""
# If --latest flag is set, also push with "latest" tags
if [ "$TAG_LATEST" = true ]; then
echo "--- Tagging and Pushing as 'latest' π·οΈ ---"
# Tag and Push AMD64 as latest
printf "%-80s" "Tagging AMD64 as latest... "
if podman tag "${IMAGE_NAME}:amd64-local" "${AMD64_LATEST_FQN}" 2>/dev/null; then
echo "β
"
else
echo "β"
exit 1
fi
printf "%-80s" "Pushing ${AMD64_LATEST_FQN}... "
if podman push "${AMD64_LATEST_FQN}" > /tmp/neo-ui-framework-build.log 2>&1; then
echo "β
"
else
echo "β Push failed"
tail -10 /tmp/neo-ui-framework-build.log
exit 1
fi
# Tag and Push ARM64 as latest
printf "%-80s" "Tagging ARM64 as latest... "
if podman tag "${IMAGE_NAME}:arm64-local" "${ARM64_LATEST_FQN}" 2>/dev/null; then
echo "β
"
else
echo "β"
exit 1
fi
printf "%-80s" "Pushing ${ARM64_LATEST_FQN}... "
if podman push "${ARM64_LATEST_FQN}" > /tmp/neo-ui-framework-build.log 2>&1; then
echo "β
"
else
echo "β Push failed"
tail -10 /tmp/neo-ui-framework-build.log
exit 1
fi
fi
echo ""
## 4. Creating and Pushing Manifest List
echo "--- 4. Creating and Pushing Manifest List π ---"
# Remove existing manifest if it exists
if podman manifest exists "${MANIFEST_FQN}" 2>/dev/null; then
printf "%-80s" "Removing existing manifest... "
if podman manifest rm "${MANIFEST_FQN}" > /dev/null 2>&1; then
echo "β
"
else
echo "β οΈ (continuing)"
fi
fi
printf "%-80s" "Creating manifest ${MANIFEST_FQN}... "
if podman manifest create "${MANIFEST_FQN}" > /dev/null 2>&1; then
echo "β
"
else
echo "β Manifest creation failed"
exit 1
fi
printf "%-80s" "Adding AMD64 to manifest... "
if podman manifest add "${MANIFEST_FQN}" "docker://${AMD64_FQN}" > /dev/null 2>&1; then
echo "β
"
else
echo "β"
exit 1
fi
printf "%-80s" "Adding ARM64 to manifest... "
if podman manifest add "${MANIFEST_FQN}" "docker://${ARM64_FQN}" > /dev/null 2>&1; then
echo "β
"
else
echo "β"
exit 1
fi
printf "%-80s" "Pushing manifest ${MANIFEST_FQN}... "
if podman manifest push "${MANIFEST_FQN}" "docker://${MANIFEST_FQN}" > /tmp/neo-ui-framework-build.log 2>&1; then
echo "β
"
else
echo "β Manifest push failed"
tail -10 /tmp/neo-ui-framework-build.log
exit 1
fi
echo ""
## 5. Creating and Pushing "latest" Manifest (if requested)
if [ "$TAG_LATEST" = true ]; then
echo "--- 5. Creating and Pushing 'latest' Manifest π ---"
# Remove existing latest manifest if it exists
if podman manifest exists "${MANIFEST_LATEST_FQN}" 2>/dev/null; then
printf "%-80s" "Removing existing latest manifest... "
if podman manifest rm "${MANIFEST_LATEST_FQN}" > /dev/null 2>&1; then
echo "β
"
else
echo "β οΈ (continuing)"
fi
fi
# Also check for any image with the same name
if podman image exists "${MANIFEST_LATEST_FQN}" 2>/dev/null; then
printf "%-80s" "Removing existing latest image... "
if podman rmi "${MANIFEST_LATEST_FQN}" > /dev/null 2>&1; then
echo "β
"
else
echo "β οΈ (continuing)"
fi
fi
printf "%-80s" "Creating latest manifest... "
if podman manifest create "${MANIFEST_LATEST_FQN}" > /dev/null 2>&1; then
echo "β
"
else
echo "β Latest manifest creation failed"
exit 1
fi
printf "%-80s" "Adding AMD64 to latest manifest... "
if podman manifest add "${MANIFEST_LATEST_FQN}" "docker://${AMD64_LATEST_FQN}" > /dev/null 2>&1; then
echo "β
"
else
echo "β"
exit 1
fi
printf "%-80s" "Adding ARM64 to latest manifest... "
if podman manifest add "${MANIFEST_LATEST_FQN}" "docker://${ARM64_LATEST_FQN}" > /dev/null 2>&1; then
echo "β
"
else
echo "β"
exit 1
fi
printf "%-80s" "Pushing latest manifest... "
if podman manifest push "${MANIFEST_LATEST_FQN}" "docker://${MANIFEST_LATEST_FQN}" > /tmp/neo-ui-framework-build.log 2>&1; then
echo "β
"
else
echo "β Latest manifest push failed"
tail -10 /tmp/neo-ui-framework-build.log
exit 1
fi
echo ""
fi
echo "-----------------------------"
echo "β¨ Multi-arch build and push complete! β¨"
echo "Manifest: ${MANIFEST_FQN}"
echo "Consumers can pull: ${MANIFEST_FQN}"
echo "-----------------------------"
if [ "$TAG_LATEST" = true ]; then
echo "Latest Manifest: ${MANIFEST_LATEST_FQN}"
echo "Consumers can also pull: ${MANIFEST_LATEST_FQN}"
fi
echo "-----------------------------"
# Cleanup temporary log file
printf "%-80s" "Cleaning up temporary log file... "
rm -f /tmp/neo-ui-framework-build.log
echo "β
"