Files
eveAI/docker/build_and_push_eveai.sh
Josako 84afc0b2ee - Debugging of redis setup issues
- Debugging of celery startup
- Moved flower to a standard image iso own build
2025-09-02 10:25:17 +02:00

252 lines
7.0 KiB
Bash
Executable File

#!/bin/bash
# Exit on any error
set -e
source ./podman_env_switch.sh dev
# Load environment variables
source .env
# Check if podman is available
if ! command -v podman &> /dev/null; then
echo "Error: podman not found"
exit 1
fi
echo "Using container runtime: podman"
# Local registry
REGISTRY="registry.ask-eve-ai-local.com"
# Account prefix voor consistency met Docker Hub
ACCOUNT="josakola"
# Tag (you might want to use a version or git commit hash)
TAG="latest"
# Single platform - AMD64 only for simplicity
PLATFORM="linux/amd64"
# Default action
ACTION="both"
# Default build options
NO_CACHE=""
PROGRESS=""
DEBUG=""
BUILD_BASE=""
BASE_ONLY=""
# Function to display usage information
usage() {
echo "Usage: $0 [-b|-p|-bb|--base-only] [--no-cache] [--progress=plain] [--debug] [service1 service2 ...]"
echo " -b: Build only"
echo " -p: Push only"
echo " -bb: Build base image (in addition to services)"
echo " --base-only: Build only base image (skip services)"
echo " --no-cache: Perform a clean build without using cache"
echo " --progress=plain: Show detailed progress of the build"
echo " --debug: Enable debug mode for the build"
echo " If no option is provided, both build and push will be performed."
echo " If no services are specified, all eveai_ services and nginx will be processed."
echo " All images are built for AMD64 platform (compatible with both x86_64 and Apple Silicon via emulation)."
}
# Parse command-line options
while [[ $# -gt 0 ]]; do
case $1 in
-b)
ACTION="build"
shift
;;
-p)
ACTION="push"
shift
;;
-bb)
BUILD_BASE="true"
shift
;;
--base-only)
BASE_ONLY="true"
shift
;;
--no-cache)
NO_CACHE="--no-cache"
shift
;;
--progress=plain)
PROGRESS="--progress=plain"
shift
;;
--debug)
DEBUG="--debug"
shift
;;
-*)
echo "Unknown option: $1"
usage
exit 1
;;
*)
break
;;
esac
done
# Function to build base image
build_base_image() {
echo "🏗️ Building base image..."
local BASE_IMAGE_NAME="$REGISTRY/$ACCOUNT/eveai-base:$TAG"
echo "Building base image for platform: $PLATFORM"
echo "Base image tag: $BASE_IMAGE_NAME"
podman build \
--platform "$PLATFORM" \
$NO_CACHE \
$PROGRESS \
$DEBUG \
-t "$ACCOUNT/eveai-base:$TAG" \
-t "$BASE_IMAGE_NAME" \
-f Dockerfile.base \
..
if [ "$ACTION" = "push" ] || [ "$ACTION" = "both" ]; then
echo "Pushing base image to registry..."
podman push "$BASE_IMAGE_NAME"
fi
echo "✅ Base image built successfully"
}
# Function to check if we should build base image
should_build_base() {
if [ "$BUILD_BASE" = "true" ] || [ "$BASE_ONLY" = "true" ]; then
return 0 # true
else
return 1 # false
fi
}
# Function to build and/or push a service
process_service() {
local SERVICE="$1"
echo "Processing $SERVICE..."
# Extract the build context and dockerfile from the compose file
CONTEXT=$(yq e ".services.$SERVICE.build.context" compose_dev.yaml)
DOCKERFILE=$(yq e ".services.$SERVICE.build.dockerfile" compose_dev.yaml)
# Check if context directory exists
if [ ! -d "$CONTEXT" ]; then
echo "Error: Build context directory '$CONTEXT' for service '$SERVICE' does not exist."
return 1
fi
# Check if Dockerfile exists
if [ ! -f "$CONTEXT/$DOCKERFILE" ]; then
echo "Error: Dockerfile '$DOCKERFILE' for service '$SERVICE' does not exist in context '$CONTEXT'."
return 1
fi
# Construct image names
LOCAL_IMAGE_NAME="$ACCOUNT/$SERVICE:$TAG"
REGISTRY_IMAGE_NAME="$REGISTRY/$ACCOUNT/$SERVICE:$TAG"
echo "Building for platform: $PLATFORM"
echo "Local tag: $LOCAL_IMAGE_NAME"
echo "Registry tag: $REGISTRY_IMAGE_NAME"
# Build and/or push based on ACTION
if [ "$ACTION" = "build" ]; then
echo "Building $SERVICE for $PLATFORM..."
podman build \
--platform "$PLATFORM" \
$NO_CACHE \
$PROGRESS \
$DEBUG \
-t "$LOCAL_IMAGE_NAME" \
-t "$REGISTRY_IMAGE_NAME" \
-f "$CONTEXT/$DOCKERFILE" \
"$CONTEXT"
elif [ "$ACTION" = "push" ]; then
echo "Building and pushing $SERVICE for $PLATFORM..."
podman build \
--platform "$PLATFORM" \
$NO_CACHE \
$PROGRESS \
$DEBUG \
-t "$LOCAL_IMAGE_NAME" \
-t "$REGISTRY_IMAGE_NAME" \
-f "$CONTEXT/$DOCKERFILE" \
"$CONTEXT"
echo "Pushing $SERVICE to registry..."
podman push "$REGISTRY_IMAGE_NAME"
else
# Both build and push
echo "Building $SERVICE for $PLATFORM..."
podman build \
--platform "$PLATFORM" \
$NO_CACHE \
$PROGRESS \
$DEBUG \
-t "$LOCAL_IMAGE_NAME" \
-t "$REGISTRY_IMAGE_NAME" \
-f "$CONTEXT/$DOCKERFILE" \
"$CONTEXT"
echo "Pushing $SERVICE to registry..."
podman push "$REGISTRY_IMAGE_NAME"
fi
}
# If no arguments are provided, process all services
if [ $# -eq 0 ]; then
SERVICES=()
while IFS= read -r line; do
SERVICES+=("$line")
done < <(yq e '.services | keys | .[]' compose_dev.yaml | grep -E '^(nginx|eveai_|prometheus|grafana)')
else
SERVICES=("$@")
fi
# Handle base-only mode
if [ "$BASE_ONLY" = "true" ]; then
echo "🎯 Base-only mode: Building only base image"
build_base_image
echo -e "\033[32m✅ Base image build completed!\033[0m"
exit 0
fi
# Build base image if requested
if should_build_base; then
build_base_image
echo "" # Empty line for readability
fi
echo "Using simplified AMD64-only approach for maximum compatibility..."
echo "Images will be tagged as: $REGISTRY/$ACCOUNT/[service]:$TAG"
# Loop through services
for SERVICE in "${SERVICES[@]}"; do
if [[ "$SERVICE" == "nginx" ]]; then
./copy_specialist_svgs.sh ../config ../nginx/static/assets 2>/dev/null || echo "Warning: copy_specialist_svgs.sh not found or failed"
fi
if [[ "$SERVICE" == "nginx" || "$SERVICE" == eveai_* || "$SERVICE" == "prometheus" || "$SERVICE" == "grafana" ]]; then
if process_service "$SERVICE"; then
echo "✅ Successfully processed $SERVICE"
else
echo "❌ Failed to process $SERVICE"
fi
else
echo "⏭️ Skipping $SERVICE as it's not nginx, prometheus, grafana or doesn't start with eveai_"
fi
done
echo -e "\033[32m✅ All specified services processed successfully!\033[0m"
echo -e "\033[32m📦 Images are available locally and in registry\033[0m"
echo -e "\033[32m🕐 Finished at $(date +"%d/%m/%Y %H:%M:%S")\033[0m"