#!/bin/bash # Script: validate_input.sh # Description: Validates prerequisites and input parameters for building and pushing a Docker image. # This script ensures that Docker, Docker Buildx, and input variables (architectures, tags, image name, registry URL) # meet the required criteria. If any check fails, the script provides informative error messages and exits. # Exit immediately if a command exits with a non-zero status. set -e echo "============================================" echo "Starting Validation Script" echo "============================================" # ---------------------------- Docker Installation --------------------------- # echo "🔍 Checking Docker installation..." if docker --version >/dev/null 2>&1; then DOCKER_VERSION=$(docker --version) echo "✅ Docker is installed: $DOCKER_VERSION" else echo "❌ Docker is not installed." echo "â„šī¸ Please install Docker from: https://docs.docker.com/get-docker/" exit 1 fi # ---------------------------- Buildx Installation --------------------------- # echo "🔍 Checking Docker Buildx installation..." if docker buildx version >/dev/null 2>&1; then BUILDX_VERSION=$(docker buildx version) echo "✅ Docker Buildx is installed: $BUILDX_VERSION" else echo "❌ Docker Buildx is not installed." echo "â„šī¸ Please install Docker Buildx. See: https://docs.docker.com/buildx/working-with-buildx/" exit 1 fi # ---------------------------- Validate Architectures --------------------------- # echo "🔍 Validating architectures..." for arch in $ARCHITECTURES; do echo "→ Checking architecture: $arch" if [[ $VALID_ARCHITECTURES != *"$arch"* ]]; then echo "❌ Invalid architecture detected: $arch" echo "â„šī¸ Allowed architectures are: $VALID_ARCHITECTURES" exit 1 fi done echo "✅ All specified architectures are valid." # ---------------------------- Validate Tags --------------------------- # echo "🔍 Validating tags..." # Ensure tags are lowercase. if sh ./ensure_lowercase.sh "Tag" $TAGS; then echo "✅ All tags are lowercase." else echo "❌ One or more tags are not lowercase. Please update the tags." exit 1 fi # Ensure tags do not contain spaces. if sh ./ensure_no_spaces.sh "Tag" $TAGS; then echo "✅ Tags do not contain spaces." else echo "❌ One or more tags contain spaces. Please update the tags." exit 1 fi # ---------------------------- Validate Image Name --------------------------- # if [ "$NAME" != "auto" ]; then echo "🔍 Validating image name..." # Ensure image name is lowercase. if sh ./ensure_lowercase.sh "Name" $NAME; then echo "✅ Image name is lowercase." else echo "❌ Image name is not lowercase. Please update the name." exit 1 fi # Ensure image name does not contain spaces. if sh ./ensure_no_spaces.sh "Name" $NAME; then echo "✅ Image name does not contain spaces." else echo "❌ Image name contains spaces. Please update the name." exit 1 fi else echo "â„šī¸ Image name is set to auto-detect." fi # ---------------------------- Validate Registry URL --------------------------- # echo "🔍 Validating Registry URL..." # Ensure Registry URL does not contain spaces. if sh ./ensure_no_spaces.sh "Registry URL" $REGISTRY_URL; then echo "✅ Registry URL does not contain spaces." else echo "❌ Registry URL contains spaces. Please update the URL." exit 1 fi # Ensure Registry URL is lowercase. if sh ./ensure_lowercase.sh "Registry URL" $REGISTRY_URL; then echo "✅ Registry URL is lowercase." else echo "❌ Registry URL is not lowercase. Please update the URL." exit 1 fi # Ensure Registry URL starts with "https://" if [[ $REGISTRY_URL != https://* ]]; then echo "❌ Invalid Registry URL: $REGISTRY_URL" echo "â„šī¸ Registry URL must start with 'https://'" exit 1 fi echo "✅ Registry URL starts with 'https://'" # Check if Registry URL is reachable. echo "🔍 Checking if Registry URL is reachable..." if curl -s --head "$REGISTRY_URL" | head -n 1 | grep "HTTP/[12]\.[0-9] [23].." >/dev/null; then echo "✅ Registry URL is reachable: $REGISTRY_URL" else echo "❌ Registry URL is not reachable: $REGISTRY_URL" echo "â„šī¸ Please verify the URL and check your network connection." exit 1 fi echo "============================================" echo "All validations passed successfully!" echo "============================================"