#!/bin/bash

# This script compiles the project, builds a docker image with the tag <BranchName>-<CommitHash> and pushes it to our nexus.

# Set the Nexus repository URL
NEXUS_REPO="nexus.knecon.com:5001"
# Set the image name
IMAGE_NAME="red/persistence-service-server-v1"
# path to image repo
IMAGE_REPO="persistence-service-image-v1"

echo "Running build"
mvn clean install -Pquickbuild

# Get the current Git branch
GIT_BRANCH=$(git symbolic-ref --short HEAD)
# Get the first 5 characters of the commit hash
GIT_COMMIT_HASH=$(git rev-parse --short=5 HEAD)
# Create the image tag by combining branch and commit hash
IMAGE_TAG="${GIT_BRANCH}-${GIT_COMMIT_HASH}"
IMAGE_NAME="$NEXUS_REPO/$IMAGE_NAME:$IMAGE_TAG"

echo "Building docker image: {$IMAGE_NAME}"
# Build the Docker image with the specified name and tag and push to nexus
mvn -f $IMAGE_REPO docker:build docker:push -Ddocker.image.version=$IMAGE_TAG

echo "Docker image '$IMAGE_NAME' has been built and pushed to Nexus."
