Files
eveAI/docker/build_and_push_eveai.sh
2024-08-07 08:37:13 +02:00

78 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
# Exit on any error
set -e
# Load environment variables
source .env
# Docker registry
REGISTRY="josakola"
# Tag (you might want to use a version or git commit hash)
TAG="latest"
# Platforms to build for
PLATFORMS="linux/amd64,linux/arm64"
# Function to build and push a service
build_and_push_service() {
local SERVICE="$1"
echo "Building and pushing $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
# Build and push
docker buildx build \
--platform "$PLATFORMS" \
-t "$REGISTRY/$SERVICE:$TAG" \
-f "$CONTEXT/$DOCKERFILE" \
"$CONTEXT" \
--push
}
# If no arguments are provided, build all services
if [ $# -eq 0 ]; then
mapfile -t SERVICES < <(yq e '.services | keys | .[]' compose_dev.yaml | grep -E '^(nginx|eveai_)')
else
SERVICES=("$@")
fi
# Check if eveai_builder exists, if not create it
if ! docker buildx inspect eveai_builder > /dev/null 2>&1; then
echo "Creating eveai_builder..."
docker buildx create --name eveai_builder
fi
# Use eveai_builder
echo "Using eveai_builder..."
docker buildx use eveai_builder
# Loop through services
for SERVICE in "${SERVICES[@]}"; do
if [[ "$SERVICE" == "nginx" || "$SERVICE" == eveai_* ]]; then
if build_and_push_service "$SERVICE"; then
echo "Successfully built and pushed $SERVICE"
else
echo "Failed to build and push $SERVICE"
fi
else
echo "Skipping $SERVICE as it's not nginx or doesn't start with eveai_"
fi
done
echo "All specified services processed."