smartgears-distribution/buildDistribution.sh

105 lines
3.3 KiB
Bash
Raw Normal View History

#!/bin/bash
ACCEPTED_JAVA_VERSIONS=(11 17)
2024-03-12 11:50:13 +01:00
################################################################################
# Help #
################################################################################
Help()
{
# Display Help
echo "build and create docker image form smartgears distribution"
echo
2024-03-12 11:50:13 +01:00
echo "Syntax: buildDistribution [-g arg] [-j arg] [-p|u|h]"
echo "options:"
echo "-g arg specifies the maven [g]oal {package, install, deploy etc} default is package."
echo "-j arg specify [j]ava version (default is 11)"
echo " accepted version are: ${ACCEPTED_JAVA_VERSIONS[@]}"
echo "-m build docker image for [m]ultiple platform (must be suppported by local docker agent)"
echo "-p [p]ush image to d4science harbor (with login already done, or -l to login)"
echo "-l [l]ogin to d4science harbor"
echo "-u p[u]sh image to dockerhub (with docker login already done)"
echo "-h Print this [h]elp."
echo
2024-05-07 15:02:57 +02:00
echo "to build a multiplatform image and push on d4science harbor"
echo "./buid ./buildDistribution.sh -m -l -p"
}
2024-05-07 15:02:57 +02:00
################################################################################
################################################################################
# Main program #
################################################################################
################################################################################
set -e
SMARTGEARS_VERSION=4.0.0-SNAPSHOT
JAVA_VERSION=11
TOMCAT_VERSION=10.1.19
PUSH_DOCKER=false
PUSH_HARBOR=false
LOGIN_HARBOR=false
while getopts g:muplj:h flag
do
case "${flag}" in
g) GOAL=${OPTARG};;
m) MULTI_PLATFORM=true ;;
u) PUSH_DOCKER=true ;;
p) PUSH_HARBOR=true ;;
l) LOGIN_HARBOR=true ;;
j) if [[ ${ACCEPTED_JAVA_VERSIONS[@]} =~ ${OPTARG} ]]
then JAVA_VERSION=${OPTARG};
else echo "Invalid java version" && echo "accepted version are: ${ACCEPTED_JAVA_VERSIONS[@]}" && exit 1;
fi;;
h) Help
exit 0;;
*) echo "Invalid option"
exit 1;;
esac
done
2024-03-12 11:50:13 +01:00
IMAGE_VERSION=${SMARTGEARS_VERSION}-java${JAVA_VERSION}-tomcat${TOMCAT_VERSION}
echo "IMAGE_VERSION=$IMAGE_VERSION"
2024-05-07 15:02:57 +02:00
BUILD_NAME=smartgears-distribution:$IMAGE_VERSION
if [ -z $GOAL ];
then mvn clean package;
else mvn clean ${GOAL};
fi
if [ -z $MULTI_PLATFORM ];
2024-05-07 15:02:57 +02:00
then docker build -t $BUILD_NAME --build-arg JAVA_VERSION=${JAVA_VERSION} .;
else docker build -t $BUILD_NAME --build-arg JAVA_VERSION=${JAVA_VERSION} --platform=linux/amd64,linux/arm64,linux/arm/v7 . ;
echo ">>> generated docker image ${IMAGE_VERSION}"
fi
if [ ${PUSH_DOCKER} = true ];
then
2024-05-07 15:02:57 +02:00
DOCKER_NAME = d4science/$BUILD_NAME
docker tag $BUILD_NAME $DOCKER_NAME;
docker push $DOCKER_NAME;
echo ">>> pushed on dockerhub the image $DOCKER_NAME"
fi
if [ ${LOGIN_HARBOR} = true ];
then
./loginHarborHub.sh
fi
if [ ${PUSH_HARBOR} = true ];
then
2024-05-07 15:02:57 +02:00
HARBOR_NAME=hub.dev.d4science.org/gcube/$BUILD_NAME
docker tag $BUILD_NAME $HARBOR_NAME;
docker push $HARBOR_NAME;
echo ">>> pushed on hub.dev.d4science.org the image $HARBOR_NAME"
2024-03-12 11:50:13 +01:00
fi