diff --git a/buildDistribution.sh b/buildDistribution.sh index 0540081..ec3be55 100755 --- a/buildDistribution.sh +++ b/buildDistribution.sh @@ -1,6 +1,6 @@ #!/bin/bash -accepted_java_versions=(11 17) +ACCEPTED_JAVA_VERSIONS=(11 17) ################################################################################ # Help # @@ -12,12 +12,14 @@ Help() echo echo "Syntax: buildDistribution [-g arg] [-j arg] [-p|u|h]" echo "options:" - echo "-g arg specifies the maven goal {package, install, deploy etc} default is package." - echo "-j arg specify java version (default is 11)" - echo " accepted version are: ${accepted_java_versions[@]}" - echo "-p build docker image for multiple platform (must be suppported by local docker agent)" - echo "-u push image to dockerhub (with docker login already done)" - echo "-h Print this Help." + 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 } @@ -30,41 +32,62 @@ Help() set -e -smartgears_version=4.0.0-SNAPSHOT -java_version=11 -tomcat_version=10.1.19 -push=false +SMARTGEARS_VERSION=4.0.0-SNAPSHOT -while getopts g:j:puh flag +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};; - p) platform=true ;; - u) push=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 + 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} +IMAGE_VERSION=${SMARTGEARS_VERSION}-java${JAVA_VERSION}-tomcat${TOMCAT_VERSION} +echo "IMAGE_VERSION=$IMAGE_VERSION" -if [ -z $goal ]; then mvn clean package; else mvn clean ${goal}; fi - -if [ -z $platform ]; - then docker build -t smartgears-distribution:$image_version --build-arg JAVA_VERSION=${java_version} .; - else docker build -t smartgears-distribution:$image_version --build-arg JAVA_VERSION=${java_version} --platform=linux/amd64,linux/arm64,linux/arm/v7 . ; +if [ -z $GOAL ]; + then mvn clean package; + else mvn clean ${GOAL}; fi -docker tag smartgears-distribution:$image_version d4science/smartgears-distribution:$image_version - -if [ ${push} = true ]; then - docker push d4science/smartgears-distribution:$image_version; +if [ -z $MULTI_PLATFORM ]; + then docker build -t smartgears-distribution:$IMAGE_VERSION --build-arg JAVA_VERSION=${JAVA_VERSION} .; + else docker build -t smartgears-distribution:$IMAGE_VERSION --build-arg JAVA_VERSION=${JAVA_VERSION} --platform=linux/amd64,linux/arm64,linux/arm/v7 . ; fi -echo "generated docker image ${image_version}" \ No newline at end of file + +if [ ${PUSH_DOCKER} = true ]; + then + docker tag smartgears-distribution:$IMAGE_VERSION d4science/smartgears-distribution:$IMAGE_VERSION; + docker push d4science/smartgears-distribution:$IMAGE_VERSION; +fi + +if [ ${LOGIN_HARBOR} = true ]; + then + ./loginHarborHub.sh +fi + +if [ ${PUSH_HARBOR} = true ]; + then + docker tag smartgears-distribution:$IMAGE_VERSION hub.dev.d4science.org/gcube/smartgears-distribution:$IMAGE_VERSION; + docker push hub.dev.d4science.org/gcube/smartgears-distribution:$IMAGE_VERSION; +fi + +echo "generated docker image ${IMAGE_VERSION}" \ No newline at end of file diff --git a/loginHarborHub.sh b/loginHarborHub.sh new file mode 100755 index 0000000..48a84cd --- /dev/null +++ b/loginHarborHub.sh @@ -0,0 +1,13 @@ +#!/bin/bash +REGISTRY_URL="hub.dev.d4science.org" +#USERNAME="alfredo.oliviero" +echo "to obtain Harbor username and CLI secret:" +echo "https://hub.dev.d4science.org/ -> user profile -> CLI secret" + +read -p "username:" USERNAME + +echo "" + +read -s -p "CLI secret:" ACCESS_TOKEN +echo "$ACCESS_TOKEN" | docker login $REGISTRY_URL -u $USERNAME --password-stdin +unset ACCESS_TOKEN