smartgears-distribution/buildDistribution.sh

105 lines
3.3 KiB
Bash
Executable File

#!/bin/bash
ACCEPTED_JAVA_VERSIONS=(11 17)
################################################################################
# Help #
################################################################################
Help()
{
# Display Help
echo "build and create docker image form smartgears distribution"
echo
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
echo "to build a multiplatform image and push on d4science harbor"
echo "./buid ./buildDistribution.sh -m -l -p"
}
################################################################################
################################################################################
# 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
IMAGE_VERSION=${SMARTGEARS_VERSION}-java${JAVA_VERSION}-tomcat${TOMCAT_VERSION}
echo "IMAGE_VERSION=$IMAGE_VERSION"
BUILD_NAME=smartgears-distribution:$IMAGE_VERSION
if [ -z $GOAL ];
then mvn clean package;
else mvn clean ${GOAL};
fi
if [ -z $MULTI_PLATFORM ];
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
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
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"
fi