push on harbor and docker

This commit is contained in:
Alfredo Oliviero 2024-05-08 17:52:16 +02:00
parent 0a930c2ba1
commit 881a26c5e7
3 changed files with 117 additions and 28 deletions

View File

@ -1,10 +1,14 @@
ARG JAVA_VERSION=11 ARG JAVA_VERSION=11
FROM d4science/smartgears-distribution:4.0.0-SNAPSHOT-java$JAVA_VERSION-tomcat10.1.19 ARG SMARTGEARS_VERSION=4.0.0-SNAPSHOT
#FROM smartgears-distribution:4.0.0-SNAPSHOT-java$JAVA_VERSION-tomcat10.1.19
COPY ./docker/logback.xml /etc/ #FROM d4science/smartgears-distribution:${SMARTGEARS_VERSION}-java${JAVA_VERSION}-tomcat10.1.19
COPY ./docker/container.ini /etc/ FROM hub.dev.d4science.org/gcube/smartgears-distribution:${SMARTGEARS_VERSION}-java${JAVA_VERSION}-tomcat10.1.19
COPY ./docker/*.gcubekey /tomcat/lib #FROM smartgears-distribution:4.0.0-SNAPSHOT-java$JAVA_VERSION-tomcat10.1.19
COPY ./target/idm-service.war /tomcat/webapps/ ARG CONTAINER_INI="./docker/container.ini"
EXPOSE 8080 COPY ./docker/logback.xml /etc/
COPY ${CONTAINER_INI} /etc/container.ini
COPY ./docker/*.gcubekey /tomcat/lib
COPY ./target/idm-service.war /tomcat/webapps/
EXPOSE 8080

View File

@ -1,13 +1,30 @@
#!/bin/bash #!/bin/bash
accepted_java_versions=(11 17) # set -x # uncomment to debug script
ACCEPTED_JAVA_VERSIONs=(11 17)
NAME=idm-service NAME=idm-service
BUILD_VERSION=0.0.1-SNAPSHOT
SMARTGEARS_VERSION=4.0.1-SNAPSHOT
CONTAINER_INI="./docker/container.ini"
IMAGE_VERSION=${BUILD_VERSION}-java${JAVA_VERSION}-smartgears${SMARTGEARS_VERSION}
PORT=8080 PORT=8080
DEBUG_PORT=5005 DEBUG_PORT=5005
DEBUG=false DEBUG=false
EXECUTE=false
TEST=false
COMPILE=true COMPILE=true
java_version=11 JAVA_VERSION=11
PUSH_DOCKER=false
PUSH_HARBOR=false
LOGIN_HARBOR=false
BUILD_NAME=$NAME:$IMAGE_VERSION
echo "BUILD_NAME=$BUILD_NAME"
################################################################################ ################################################################################
# Help # # Help #
@ -18,14 +35,20 @@ Help() {
echo echo
echo "Syntax: buildDistribution [-n arg] [-p arg] [-j arg] [-d arg?] [-h]" echo "Syntax: buildDistribution [-n arg] [-p arg] [-j arg] [-d arg?] [-h]"
echo "options:" echo "options:"
echo "-s skip maven package" echo "-s skip maven package"
echo "-n arg specifies the docker image name (default is identity-manager)." echo "-t exec also maven tests"
echo "-p arg specifies the port to be exposed for the docker container to access the service (default $PORT)" echo "-n arg specifies the docker image name (default is identity-manager)."
echo "-j arg specify java version (default is $java_version)" echo "-p arg specifies the port to be exposed for the docker container to access the service (default $PORT)"
echo " accepted version are: ${accepted_java_versions[@]}" echo "-j arg specify java version (default is $JAVA_VERSION)"
echo "-d arg? enable java debug mode" echo " accepted version are: ${ACCEPTED_JAVA_VERSIONs[@]}"
echo " arg is the debug port (default is $DEBUG_PORT)" echo "-e execute the image"
echo "-h Print this Help." echo "-d arg? enable java debug mode for execution"
echo " arg is the debug port (default is $DEBUG_PORT)"
echo "-r push image to d4science harbo[r] (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 "-c arg path of the file to deploy as container.ini (default ./docker/container.ini)"
echo "-h Print this Help."
echo echo
} }
@ -37,28 +60,42 @@ Help() {
set -e set -e
OPTSTRING=":sn:p:d:j:?h" OPTSTRING=":slmurn:p:c:ed:j:?h"
while getopts $OPTSTRING opt; do while getopts $OPTSTRING opt; do
# echo "Option -${opt} was triggered, Argument: ${OPTARG}" # echo "Option -${opt} was triggered, Argument: ${OPTARG}"
case "${opt}" in case "${opt}" in
s) COMPILE=false && echo "compile $COMPILE" ;; s) COMPILE=false && echo "compile $COMPILE" ;;
c)
CONTAINER_INI=${OPTARG}
echo "CONTAINER_INI: $CONTAINER_INI";;
m) MULTI_PLATFORM=true ;;
n) NAME=${OPTARG} ;; n) NAME=${OPTARG} ;;
p) PORT=${OPTARG} ;; p) PORT=${OPTARG} ;;
u) PUSH_DOCKER=true ;;
l) LOGIN_HARBOR=true ;;
r) PUSH_HARBOR=true ;;
t) TEST=true ;;
e) EXECUTE=true ;;
d) d)
EXECUTE=true
DEBUG=true DEBUG=true
DEBUG_PORT=${OPTARG} DEBUG_PORT=${OPTARG}
echo "debug enabled, port $DEBUG_PORT" echo "debug enabled, port $DEBUG_PORT"
;; ;;
j) j)
if [[ ${accepted_java_versions[@]} =~ ${OPTARG} ]]; then if [[ ${ACCEPTED_JAVA_VERSIONs[@]} =~ ${OPTARG} ]]; then
java_version=${OPTARG} JAVA_VERSION=${OPTARG}
else else
echo "Invalid java version" && echo "accepted version are: ${accepted_java_versions[@]}" && exit 1 echo "Invalid java version" && echo "accepted version are: ${ACCEPTED_JAVA_VERSIONs[@]}" && exit 1
fi fi
;; ;;
h) Help && exit 0 ;; h) Help && exit 0 ;;
:) # matched when an option that is expected to have an argument is passed without one
# matched when an option that is expected to have an argument is passed without one
:)
if [ ${OPTARG} = "d" ]; then if [ ${OPTARG} = "d" ]; then
DEBUG=true DEBUG=true
echo "debug enabled, port $DEBUG_PORT" echo "debug enabled, port $DEBUG_PORT"
@ -76,15 +113,50 @@ while getopts $OPTSTRING opt; do
done done
if [ $COMPILE = true ]; then if [ $COMPILE = true ]; then
mvn clean package if [ $TEST = false ]; then
mvn clean package -Dmaven.test.skip
else
mvn clean package
fi
else else
echo "skipping mvn package" echo "skipping mvn package"
fi fi
docker build -t $NAME --build-arg JAVA_VERSION=${java_version} . if [ -z $MULTI_PLATFORM ]; then
docker build -t $BUILD_NAME --build-arg="CONTAINER_INI=$CONTAINER_INI" --build-arg="JAVA_VERSION=${JAVA_VERSION}" --build-arg="SMARTGEARS_VERSION=${SMARTGEARS_VERSION}" .
if [ $DEBUG = false ]; then
docker run -p $PORT:8080 $NAME
else else
docker run -p $PORT:8080 -p $DEBUG_PORT:5005 -e JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,address=*:5005,server=y,suspend=y" $NAME docker build -t $BUILD_NAME --build-arg="CONTAINER_INI=$CONTAINER_INI" --build-arg="JAVA_VERSION=${JAVA_VERSION}" --build-arg="SMARTGEARS_VERSION=${SMARTGEARS_VERSION}" --platform=linux/amd64,linux/arm64,linux/arm/v7 .
# docker manifest create hub.dev.d4science.org/gcube/$BUILD_NAME \
# hub.dev.d4science.org/gcube/$NAME-amd64-linux:$IMAGE_VERSION \
# hub.dev.d4science.org/gcube/$NAME-arm-linux:$IMAGE_VERSION \
# hub.dev.d4science.org/gcube/$NAME-arm-linux:$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
echo ">>> PUSHING on hub.dev.d4science.org the image $HARBOR_NAME"
docker tag $BUILD_NAME $HARBOR_NAME
docker push $HARBOR_NAME
echo ">>> pushed on hub.dev.d4science.org the image $HARBOR_NAME"
fi
if [ $EXECUTE = true ]; then
if [ $DEBUG = true ]; then
docker run -p $PORT:8080 -p $DEBUG_PORT:5005 -e JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,address=*:5005,server=y,suspend=y" $BUILD_NAME
else
docker run -p $PORT:8080 $BUILD_NAME
fi
fi fi

13
loginHarborHub.sh Executable file
View File

@ -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