From f2ef4b03cca8c2c19082bbcafdba9bcccc627c59 Mon Sep 17 00:00:00 2001 From: valentinab25 <30239069+valentinab25@users.noreply.github.com> Date: Thu, 19 Oct 2023 10:59:58 +0300 Subject: [PATCH] Docker multistage build image on commit (#58) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * build in docker, with multistage approach * Aling Docker image with official GN image * Remove unused Maven profiles that can be now set via environment variables/properties * Using Ubuntu Focal based image due to incompatibilities of newer images with the Jenkins host OS. --------- Co-authored-by: michimau Co-authored-by: Juan Luis Rodríguez --- Jenkinsfile | 56 +++++++++++++++++++ build-in-docker/Dockerfile | 52 +++++++++++++++++ build-in-docker/build_last_image.sh | 8 +++ build-in-docker/docker-entrypoint.sh | 24 ++++++++ .../jetty/geonetwork_context_template.xml | 17 ++++++ pom.xml | 1 - web/pom.xml | 30 ---------- 7 files changed, 157 insertions(+), 31 deletions(-) create mode 100644 Jenkinsfile create mode 100644 build-in-docker/Dockerfile create mode 100755 build-in-docker/build_last_image.sh create mode 100755 build-in-docker/docker-entrypoint.sh create mode 100644 build-in-docker/jetty/geonetwork_context_template.xml diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000000..19de07b20b --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,56 @@ +pipeline { + agent { + node { label "docker-host" } + } + + environment { + GIT_NAME = "geonetwork-eea" + registry = "eeacms/eea-geonetwork" + default_branch = "eea-4.2.0" + } + + stages { + + stage ('Docker build and push') { + when { + environment name: 'CHANGE_ID', value: '' + } + + steps { + script{ + if (env.BRANCH_NAME == env.default_branch ) { + tagName = GIT_COMMIT.take(8) + } else { + tagName = "$BRANCH_NAME" + } + try { + dockerImage = docker.build("$registry:$tagName", "--pull --no-cache --build-arg COMMIT_OR_BRANCH=$tagName ./build-in-docker/") + docker.withRegistry( '', 'eeajenkins' ) { + dockerImage.push() + } + } + finally { + sh "docker rmi $registry:$tagName" + } + } + } + } + + } + + post { + always { + cleanWs(cleanWhenAborted: true, cleanWhenFailure: true, cleanWhenNotBuilt: true, cleanWhenSuccess: true, cleanWhenUnstable: true, deleteDirs: true) + script { + def url = "${env.BUILD_URL}/display/redirect" + def status = currentBuild.currentResult + def subject = "${status}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'" + def summary = "${subject} (${url})" + def details = """

${env.JOB_NAME} - Build #${env.BUILD_NUMBER} - ${status}

+

Check console output at ${env.JOB_BASE_NAME} - #${env.BUILD_NUMBER}

+ """ + emailext (subject: summary, body: details, attachLog: true, compressLog: true, recipientProviders: [ [$class: 'CulpritsRecipientProvider']]) + } + } + } +} diff --git a/build-in-docker/Dockerfile b/build-in-docker/Dockerfile new file mode 100644 index 0000000000..7dd098706c --- /dev/null +++ b/build-in-docker/Dockerfile @@ -0,0 +1,52 @@ +FROM maven:3-eclipse-temurin-8-focal AS build + +ARG COMMIT_OR_BRANCH=eea-4.2.0 + +RUN apt-get -y update && \ + apt-get install -y --no-install-recommends \ + unzip + + +RUN git clone --recursive https://github.com/eea/geonetwork-eea.git /tmp/geonetwork-eea +WORKDIR /tmp/geonetwork-eea +RUN git checkout $COMMIT_OR_BRANCH +RUN git submodule update --init --recursive +RUN mvn -B install -DskipTests +RUN unzip /tmp/geonetwork-eea/web/target/geonetwork.war -d /tmp/geonetwork + + +FROM jetty:9-jdk8-eclipse-temurin AS final +LABEL maintainer="michimau " + +ENV DATA_DIR /catalogue-data +ENV WEBAPP_CONTEXT_PATH /geonetwork +ENV GN_CONFIG_PROPERTIES -Dgeonetwork.dir=${DATA_DIR} \ + -Dgeonetwork.formatter.dir=${DATA_DIR}/data/formatter \ + -Dgeonetwork.schema.dir=/opt/geonetwork/WEB-INF/data/config/schema_plugins \ + -Dgeonetwork.indexConfig.dir=/opt/geonetwork/WEB-INF/data/config/index + + +ENV JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -Djava.awt.headless=true \ + -Xms512M -Xss512M -Xmx2G -XX:+UseConcMarkSweepGC + + +USER root + +RUN rm -rf /var/lib/jetty/webapps/* && \ + chown jetty:jetty /var/lib/jetty/webapps && \ + mkdir -p ${DATA_DIR} && \ + chown -R jetty:jetty ${DATA_DIR} + +USER jetty +COPY jetty/geonetwork_context_template.xml /usr/local/share/geonetwork/geonetwork_context_template.xml +RUN java -jar /usr/local/jetty/start.jar --create-startd --add-module=http-forwarded +COPY docker-entrypoint.sh /geonetwork-entrypoint.sh + +COPY --chown=jetty:jetty --from=build /tmp/geonetwork /opt/geonetwork + +ENTRYPOINT ["/geonetwork-entrypoint.sh"] +CMD ["java","-jar","/usr/local/jetty/start.jar"] + + + + diff --git a/build-in-docker/build_last_image.sh b/build-in-docker/build_last_image.sh new file mode 100755 index 0000000000..302ed1a55d --- /dev/null +++ b/build-in-docker/build_last_image.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +GIT_COMMIT=$(git ls-remote https://github.com/eea/geonetwork-eea.git HEAD | cut -f1) +echo "Building https://github.com/eea/geonetwork-eea.git at $GIT_COMMIT" +docker build \ + -t eeacms/geonetwork-eea:${GIT_COMMIT} \ + --build-arg COMMIT_OR_BRANCH=${GIT_COMMIT} \ + . diff --git a/build-in-docker/docker-entrypoint.sh b/build-in-docker/docker-entrypoint.sh new file mode 100755 index 0000000000..69c125700e --- /dev/null +++ b/build-in-docker/docker-entrypoint.sh @@ -0,0 +1,24 @@ +#!/bin/bash +set -e + +export JAVA_OPTIONS="${JAVA_OPTS} ${GN_CONFIG_PROPERTIES}" + +GN_BASE_DIR=/opt/geonetwork + +if ! command -v -- "$1" >/dev/null 2>&1 ; then + set -- java -jar "$JETTY_HOME/start.jar" "$@" +fi + +if [[ "$1" = jetty.sh ]] || [[ $(expr "$*" : 'java .*/start\.jar.*$') != 0 ]]; then + # Customize context path + if [ ! -f "${JETTY_BASE}/webapps/geonetwork.xml" ]; then + echo "Using ${WEBAPP_CONTEXT_PATH} for deploying the application" + cp /usr/local/share/geonetwork/geonetwork_context_template.xml "${JETTY_BASE}/webapps/geonetwork.xml" + sed -i "s#GEONETWORK_CONTEXT_PATH#${WEBAPP_CONTEXT_PATH}#" "${JETTY_BASE}/webapps/geonetwork.xml" + fi + + # Delegate on base image entrypoint to start jetty + exec /docker-entrypoint.sh "$@" +else + exec "$@" +fi diff --git a/build-in-docker/jetty/geonetwork_context_template.xml b/build-in-docker/jetty/geonetwork_context_template.xml new file mode 100644 index 0000000000..d667206133 --- /dev/null +++ b/build-in-docker/jetty/geonetwork_context_template.xml @@ -0,0 +1,17 @@ + + + + + GEONETWORK_CONTEXT_PATH + /opt/geonetwork + + -javax.mail. + + + javax.mail. + + + org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern + nomatches + + diff --git a/pom.xml b/pom.xml index c136a23f4a..00e4ef0e91 100644 --- a/pom.xml +++ b/pom.xml @@ -1305,7 +1305,6 @@ healthmonitor services wro4j - web inspire-atom doi es diff --git a/web/pom.xml b/web/pom.xml index 38677ba75d..7f917ea303 100644 --- a/web/pom.xml +++ b/web/pom.xml @@ -1229,8 +1229,6 @@ - geonetwork - config-security prod @@ -1244,9 +1242,6 @@ prod - config-security - geonetwork - config-security-noldap.xml @@ -1259,34 +1254,9 @@ dev - ${geonetwork.webapp.dir}/WEB-INF/data - config-security - geonetwork - config-security-noldap.xml true - - env-catalogue - - - env - prod - - - - prod - config-securitycatalogue - catalogue - /var/local/gn_data/logs/catalogue.log - file:///catalogue-data - - config-security-noldap.xml - xml/schemas/**,WEB-INF/web*.xml - Editor - SDI_EDITING_DEFAULT - - env-inspire