#!/bin/bash

source /home/anon/.jabba/jabba.sh

# amazon-corretto
jabba use 21.0.2

# Set variables
IMAGE_NAME="nise-backend"
IMAGE_REGISTRY="git.gengo.tech/nuff"
IMAGE_VERSION="latest"

rm -rf target/

# Clean and build Maven project
mvn clean package
if [ $? -ne 0 ]; then
    echo "Maven build failed"
    exit 1
fi

# Build and push Docker image
docker build . -t $IMAGE_NAME:$IMAGE_VERSION
if [ $? -ne 0 ]; then
    echo "Docker build failed"
    exit 1
fi

docker tag $IMAGE_NAME:$IMAGE_VERSION $IMAGE_REGISTRY/$IMAGE_NAME:$IMAGE_VERSION
docker push $IMAGE_REGISTRY/$IMAGE_NAME:$IMAGE_VERSION
if [ "$?" != "0" ]; then
    echo "Error: Failed to push $IMAGE_REGISTRY/$IMAGE_NAME:$IMAGE_VERSION"
    exit 1
fi


echo "Docker image pushed successfully to $IMAGE_REGISTRY/$IMAGE_NAME:$IMAGE_VERSION"