|
| 1 | +#!/bin/bash |
| 2 | +# Build all CCB base images for Docker layer caching. |
| 3 | +# Run this ONCE before a benchmark batch. Subsequent task builds |
| 4 | +# will reuse these cached layers instead of re-cloning repos. |
| 5 | +# |
| 6 | +# Usage: ./base_images/build.sh [--parallel] |
| 7 | + |
| 8 | +set -euo pipefail |
| 9 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 10 | + |
| 11 | +# Enable BuildKit |
| 12 | +export DOCKER_BUILDKIT=1 |
| 13 | + |
| 14 | +PARALLEL=false |
| 15 | +if [ "${1:-}" = "--parallel" ]; then |
| 16 | + PARALLEL=true |
| 17 | +fi |
| 18 | + |
| 19 | +build_image() { |
| 20 | + local dockerfile="$1" |
| 21 | + local tag="$2" |
| 22 | + |
| 23 | + # Skip if image already exists and is recent (< 7 days old) |
| 24 | + local image_age |
| 25 | + image_age=$(docker inspect --format='{{.Created}}' "$tag" 2>/dev/null || echo "") |
| 26 | + if [ -n "$image_age" ]; then |
| 27 | + local age_seconds |
| 28 | + age_seconds=$(( $(date +%s) - $(date -d "$image_age" +%s 2>/dev/null || echo 0) )) |
| 29 | + if [ "$age_seconds" -lt 604800 ]; then |
| 30 | + echo "SKIP $tag (exists, ${age_seconds}s old)" |
| 31 | + return 0 |
| 32 | + fi |
| 33 | + fi |
| 34 | + |
| 35 | + echo "BUILD $tag ..." |
| 36 | + local start_time=$SECONDS |
| 37 | + docker build -f "$dockerfile" -t "$tag" "$SCRIPT_DIR" 2>&1 | tail -5 |
| 38 | + local elapsed=$(( SECONDS - start_time )) |
| 39 | + echo "DONE $tag (${elapsed}s)" |
| 40 | +} |
| 41 | + |
| 42 | +echo "=== Building CCB base images ===" |
| 43 | +echo "" |
| 44 | + |
| 45 | +IMAGES=( |
| 46 | + "Dockerfile.django-674eda1c ccb-repo-django-674eda1c" |
| 47 | + "Dockerfile.flipt-3d5a345f ccb-repo-flipt-3d5a345f" |
| 48 | + "Dockerfile.k8s-11602f08 ccb-repo-k8s-11602f08" |
| 49 | + "Dockerfile.k8s-8c9c67c0 ccb-repo-k8s-8c9c67c0" |
| 50 | + "Dockerfile.kafka-0753c489 ccb-repo-kafka-0753c489" |
| 51 | + "Dockerfile.kafka-e678b4b ccb-repo-kafka-e678b4b" |
| 52 | + "Dockerfile.flink-0cc95fcc ccb-repo-flink-0cc95fcc" |
| 53 | +) |
| 54 | + |
| 55 | +TOTAL_START=$SECONDS |
| 56 | + |
| 57 | +if $PARALLEL; then |
| 58 | + echo "Building ${#IMAGES[@]} images in parallel (max 4 concurrent)..." |
| 59 | + for entry in "${IMAGES[@]}"; do |
| 60 | + read -r dockerfile tag <<< "$entry" |
| 61 | + (build_image "$SCRIPT_DIR/$dockerfile" "$tag") & |
| 62 | + # Limit to 4 concurrent builds |
| 63 | + while [ "$(jobs -rp | wc -l)" -ge 4 ]; do |
| 64 | + wait -n 2>/dev/null || true |
| 65 | + done |
| 66 | + done |
| 67 | + wait |
| 68 | +else |
| 69 | + echo "Building ${#IMAGES[@]} images sequentially..." |
| 70 | + for entry in "${IMAGES[@]}"; do |
| 71 | + read -r dockerfile tag <<< "$entry" |
| 72 | + build_image "$SCRIPT_DIR/$dockerfile" "$tag" |
| 73 | + done |
| 74 | +fi |
| 75 | + |
| 76 | +TOTAL_ELAPSED=$(( SECONDS - TOTAL_START )) |
| 77 | +echo "" |
| 78 | +echo "=== All base images built in ${TOTAL_ELAPSED}s ===" |
| 79 | +echo "" |
| 80 | +echo "Base images available:" |
| 81 | +docker images --format " {{.Repository}}:{{.Tag}} {{.Size}}" | grep ccb-repo || true |
0 commit comments