#!/usr/bin/env bash set -Eeuo pipefail env_file="${1:-compose/.env}" source_directory="${2:-/srv/ai-soulmate/sources/mlkj-admin}" for required_command in awk docker git; do if ! command -v "$required_command" >/dev/null 2>&1; then printf 'Required command is unavailable: %s\n' "$required_command" >&2 exit 1 fi done if [[ ! -r "$env_file" || ! -r "$source_directory/backend-java/Dockerfile" || ! -r "$source_directory/frontend/Dockerfile" ]]; then printf 'Admin environment or source Dockerfiles are unavailable.\n' >&2 exit 1 fi env_value() { awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_file" } source_branch="$(git -C "$source_directory" branch --show-current)" source_commit="$(git -C "$source_directory" rev-parse HEAD)" source_short_commit="${source_commit:0:12}" backend_image="$(env_value ADMIN_BACKEND_IMAGE)" frontend_image="$(env_value ADMIN_FRONTEND_IMAGE)" if [[ "$source_branch" != "local-deployment" ]]; then printf 'Expected mlkj-admin branch local-deployment, found: %s\n' "$source_branch" >&2 exit 1 fi if [[ -n "$(git -C "$source_directory" status --short --untracked-files=no)" ]]; then printf 'Tracked mlkj-admin source changes are present; refusing an uncommitted build.\n' >&2 exit 1 fi if [[ "$backend_image" != "ai-soulmate-admin-backend:${source_short_commit}" || "$frontend_image" != "ai-soulmate-admin-frontend:${source_short_commit}" ]]; then printf 'Admin image tags do not match source commit; rerun configure-admin-env.sh.\n' >&2 exit 1 fi docker build \ --build-context "maven-local-repository=$source_directory/.empty-maven-repository" \ --build-arg "MAVEN_IMAGE=$(env_value ADMIN_MAVEN_IMAGE)" \ --build-arg "JRE_IMAGE=$(env_value ADMIN_JRE_IMAGE)" \ --target runtime \ --label "org.opencontainers.image.revision=${source_commit}" \ --file "$source_directory/backend-java/Dockerfile" \ --tag "$backend_image" \ "$source_directory" docker build \ --build-arg "NODE_IMAGE=$(env_value ADMIN_NODE_IMAGE)" \ --build-arg ADMIN_MODULE_CONFIG=module-config.local.json \ --target runtime \ --label "org.opencontainers.image.revision=${source_commit}" \ --tag "$frontend_image" \ "$source_directory/frontend" docker image inspect "$backend_image" "$frontend_image" \ --format 'Admin image built: {{index .RepoTags 0}} id={{.Id}} size={{.Size}} revision={{index .Config.Labels "org.opencontainers.image.revision"}}'