|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Docker build script for redis-benchmark-go |
| 4 | +# This script builds the Docker image with proper Git information |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +# Default values |
| 9 | +IMAGE_NAME="redis/redis-benchmark-go" |
| 10 | +TAG="latest" |
| 11 | +PLATFORM="" |
| 12 | +PUSH=false |
| 13 | + |
| 14 | +# Colors for output |
| 15 | +RED='\033[0;31m' |
| 16 | +GREEN='\033[0;32m' |
| 17 | +YELLOW='\033[1;33m' |
| 18 | +NC='\033[0m' # No Color |
| 19 | + |
| 20 | +# Function to print colored output |
| 21 | +print_info() { |
| 22 | + echo -e "${GREEN}[INFO]${NC} $1" |
| 23 | +} |
| 24 | + |
| 25 | +print_warning() { |
| 26 | + echo -e "${YELLOW}[WARNING]${NC} $1" |
| 27 | +} |
| 28 | + |
| 29 | +print_error() { |
| 30 | + echo -e "${RED}[ERROR]${NC} $1" |
| 31 | +} |
| 32 | + |
| 33 | +# Function to show usage |
| 34 | +usage() { |
| 35 | + echo "Usage: $0 [OPTIONS]" |
| 36 | + echo "" |
| 37 | + echo "Options:" |
| 38 | + echo " -n, --name NAME Docker image name (default: redis/redis-benchmark-go)" |
| 39 | + echo " -t, --tag TAG Docker image tag (default: latest)" |
| 40 | + echo " -p, --platform PLATFORM Target platform (e.g., linux/amd64,linux/arm64)" |
| 41 | + echo " --push Push image to Docker Hub after building" |
| 42 | + echo " -h, --help Show this help message" |
| 43 | + echo "" |
| 44 | + echo "Examples:" |
| 45 | + echo " $0 # Build with defaults (latest tag)" |
| 46 | + echo " $0 -t v1.0.0 --push # Build and push version tag" |
| 47 | + echo " $0 -p linux/amd64,linux/arm64 --push # Multi-platform build and push" |
| 48 | + echo "" |
| 49 | + echo "Docker Hub Repository: redis/redis-benchmark-go" |
| 50 | +} |
| 51 | + |
| 52 | +# Parse command line arguments |
| 53 | +while [[ $# -gt 0 ]]; do |
| 54 | + case $1 in |
| 55 | + -n|--name) |
| 56 | + IMAGE_NAME="$2" |
| 57 | + shift 2 |
| 58 | + ;; |
| 59 | + -t|--tag) |
| 60 | + TAG="$2" |
| 61 | + shift 2 |
| 62 | + ;; |
| 63 | + -p|--platform) |
| 64 | + PLATFORM="$2" |
| 65 | + shift 2 |
| 66 | + ;; |
| 67 | + --push) |
| 68 | + PUSH=true |
| 69 | + shift |
| 70 | + ;; |
| 71 | + -h|--help) |
| 72 | + usage |
| 73 | + exit 0 |
| 74 | + ;; |
| 75 | + *) |
| 76 | + print_error "Unknown option: $1" |
| 77 | + usage |
| 78 | + exit 1 |
| 79 | + ;; |
| 80 | + esac |
| 81 | +done |
| 82 | + |
| 83 | +# Prepare for build |
| 84 | +print_info "Preparing Docker build..." |
| 85 | + |
| 86 | +# Build Docker image |
| 87 | +FULL_IMAGE_NAME="${IMAGE_NAME}:${TAG}" |
| 88 | +print_info "Building Docker image: $FULL_IMAGE_NAME" |
| 89 | + |
| 90 | +# Prepare build command |
| 91 | +if [[ -n "$PLATFORM" ]]; then |
| 92 | + # Multi-platform build requires buildx |
| 93 | + print_info "Target platform(s): $PLATFORM" |
| 94 | + print_info "Setting up Docker Buildx for multi-platform build..." |
| 95 | + |
| 96 | + # Create buildx builder if it doesn't exist |
| 97 | + if ! docker buildx ls | grep -q "multiplatform"; then |
| 98 | + print_info "Creating multiplatform builder..." |
| 99 | + docker buildx create --name multiplatform --use --bootstrap |
| 100 | + else |
| 101 | + print_info "Using existing multiplatform builder..." |
| 102 | + docker buildx use multiplatform |
| 103 | + fi |
| 104 | + |
| 105 | + BUILD_CMD="docker buildx build --platform $PLATFORM" |
| 106 | + if [[ "$PUSH" == "true" ]]; then |
| 107 | + BUILD_CMD="$BUILD_CMD --push" |
| 108 | + else |
| 109 | + BUILD_CMD="$BUILD_CMD --load" |
| 110 | + print_warning "Multi-platform builds without --push will only load the native platform image" |
| 111 | + fi |
| 112 | +else |
| 113 | + # Single platform build uses regular docker build |
| 114 | + BUILD_CMD="docker build" |
| 115 | +fi |
| 116 | + |
| 117 | +# Get Git information for build args |
| 118 | +GIT_SHA=$(git rev-parse HEAD 2>/dev/null || echo "unknown") |
| 119 | +GIT_DIRTY=$(git diff --no-ext-diff 2>/dev/null | wc -l || echo "0") |
| 120 | + |
| 121 | +print_info "Git SHA: $GIT_SHA" |
| 122 | +print_info "Git dirty files: $GIT_DIRTY" |
| 123 | + |
| 124 | +# Add build args and tags |
| 125 | +BUILD_CMD="$BUILD_CMD --build-arg GIT_SHA=$GIT_SHA --build-arg GIT_DIRTY=$GIT_DIRTY -t $FULL_IMAGE_NAME ." |
| 126 | + |
| 127 | +print_info "Executing: $BUILD_CMD" |
| 128 | + |
| 129 | +# Execute build |
| 130 | +if eval $BUILD_CMD; then |
| 131 | + print_info "✅ Docker image built successfully: $FULL_IMAGE_NAME" |
| 132 | + |
| 133 | + # Show image size (only for single platform builds or when image is loaded locally) |
| 134 | + if [[ -z "$PLATFORM" ]] || [[ "$PUSH" != "true" ]]; then |
| 135 | + IMAGE_SIZE=$(docker images --format "table {{.Size}}" $FULL_IMAGE_NAME 2>/dev/null | tail -n 1) |
| 136 | + if [[ -n "$IMAGE_SIZE" && "$IMAGE_SIZE" != "SIZE" ]]; then |
| 137 | + print_info "Image size: $IMAGE_SIZE" |
| 138 | + fi |
| 139 | + fi |
| 140 | + |
| 141 | + # Handle push for single platform builds (multi-platform builds push automatically with buildx) |
| 142 | + if [[ "$PUSH" == "true" && -z "$PLATFORM" ]]; then |
| 143 | + print_info "🚀 Pushing image to Docker Hub..." |
| 144 | + |
| 145 | + # Check if logged in to Docker Hub |
| 146 | + if ! docker info | grep -q "Username:"; then |
| 147 | + print_warning "Not logged in to Docker Hub. Please run: docker login" |
| 148 | + print_info "Or set DOCKER_USERNAME and DOCKER_PASSWORD environment variables" |
| 149 | + |
| 150 | + if [[ -n "$DOCKER_USERNAME" && -n "$DOCKER_PASSWORD" ]]; then |
| 151 | + print_info "Using environment variables for Docker login..." |
| 152 | + echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin |
| 153 | + else |
| 154 | + print_error "❌ Docker Hub authentication required" |
| 155 | + exit 1 |
| 156 | + fi |
| 157 | + fi |
| 158 | + |
| 159 | + # Push the image |
| 160 | + if docker push $FULL_IMAGE_NAME; then |
| 161 | + print_info "✅ Image pushed successfully to Docker Hub: $FULL_IMAGE_NAME" |
| 162 | + else |
| 163 | + print_error "❌ Failed to push image to Docker Hub" |
| 164 | + exit 1 |
| 165 | + fi |
| 166 | + elif [[ "$PUSH" == "true" && -n "$PLATFORM" ]]; then |
| 167 | + print_info "✅ Multi-platform image pushed successfully to Docker Hub: $FULL_IMAGE_NAME" |
| 168 | + fi |
| 169 | + |
| 170 | + echo "" |
| 171 | + print_info "To run the container:" |
| 172 | + echo " docker run --rm $FULL_IMAGE_NAME --help" |
| 173 | + echo "" |
| 174 | + print_info "To run with Redis connection:" |
| 175 | + echo " docker run --rm --network=host $FULL_IMAGE_NAME -h localhost -c 50 -n 100000" |
| 176 | + echo "" |
| 177 | + print_info "To run with custom Redis server:" |
| 178 | + echo " docker run --rm $FULL_IMAGE_NAME -h redis-server -p 6379 -c 100 -n 1000000" |
| 179 | + echo "" |
| 180 | + if [[ "$PUSH" == "true" ]]; then |
| 181 | + print_info "Image available on Docker Hub: https://hub.docker.com/r/redis/redis-benchmark-go" |
| 182 | + fi |
| 183 | +else |
| 184 | + print_error "❌ Docker build failed" |
| 185 | + exit 1 |
| 186 | +fi |
0 commit comments