From 3bd4708e173d0e597c6aa3a76e95491e8f0039f949754bdf58c12cf027bfbd9c Mon Sep 17 00:00:00 2001 From: Morten Green Hermansen Date: Sat, 27 Dec 2025 22:28:39 +0100 Subject: [PATCH] NoIssue: Support for building runable docker container --- .gitignore | 1 + build.gradle.kts | 32 +++++++++++++-- build.sh | 28 ++++++++++++- clean.sh | 3 ++ conf/log4j2.xml | 23 +++++++++++ publish.sh | 8 +++- publishCICD.sh | 8 +++- run.sh | 5 +++ src/main/docker/Dockerfile_template | 39 +++++++++++++++++++ src/main/docker/publish_template | 18 +++++++++ src/main/docker/stack.yml | 22 +++++++++++ src/main/docker/start.sh | 6 +++ src/main/docker/up_stack | 3 ++ .../com/r35157/nenjim/hubd/impl/ref/Main.java | 18 +++++++++ .../r35157/nenjim/hubd/impl/ref/SomeImpl.java | 13 ------- 15 files changed, 207 insertions(+), 20 deletions(-) create mode 100644 conf/log4j2.xml create mode 100755 run.sh create mode 100644 src/main/docker/Dockerfile_template create mode 100644 src/main/docker/publish_template create mode 100644 src/main/docker/stack.yml create mode 100755 src/main/docker/start.sh create mode 100755 src/main/docker/up_stack create mode 100644 src/main/java/com/r35157/nenjim/hubd/impl/ref/Main.java delete mode 100644 src/main/java/com/r35157/nenjim/hubd/impl/ref/SomeImpl.java diff --git a/.gitignore b/.gitignore index eede1c1..127d3dc 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ # Ignore Gradle build output directory build +/logs/NenjimHub.log diff --git a/build.gradle.kts b/build.gradle.kts index 44c1543..e26d1f5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,6 @@ plugins { id("java") + id("application") id("maven-publish") } @@ -9,6 +10,10 @@ if (version == "UNSET" && gradle.startParameter.taskNames.any { it.startsWith("p throw GradleException("You must set -Pversion=... (use publish.sh / publishCICD.sh)") } +application { + mainClass.set("com.r35157.nenjim.hubd.impl.ref.Main") +} + repositories { mavenLocal() @@ -28,15 +33,36 @@ repositories { } dependencies { - implementation("org.jetbrains:annotations:26.0.1") - implementation("log4j:log4j:1.2.17") - implementation("com.r35157.nenjim:hubd-api:0.0.0") + implementation(platform("org.apache.logging.log4j:log4j-bom:2.25.2")) + implementation("org.apache.logging.log4j:log4j-api") + runtimeOnly("org.apache.logging.log4j:log4j-core") + compileOnly("org.jetbrains:annotations:26.0.2-1") + + implementation("com.r35157.nenjim:hubd-api:0.0-dev") + implementation("org.apache.poi:poi-ooxml:5.5.1") } java { toolchain { languageVersion.set(JavaLanguageVersion.of(24)) } } +val dockerLibsDir = layout.buildDirectory.dir("docker/libs") + +tasks.register("prepareDockerLibs") { + group = "distribution" + description = "Copies all runtime dependencies (transitive) to build/docker/libs" + + // Hvis du også vil bygge dit eget jar samtidig, så lad den stå – ellers kan du fjerne den. + dependsOn(tasks.named("jar")) + + into(dockerLibsDir) + + // Kun deps (transitivt) + from(configurations.runtimeClasspath) + + duplicatesStrategy = DuplicatesStrategy.EXCLUDE +} + tasks.withType { options.release.set(24) } diff --git a/build.sh b/build.sh index b25e4e6..1603094 100755 --- a/build.sh +++ b/build.sh @@ -1,6 +1,30 @@ +#!/usr/bin/env bash +set -xeuo pipefail + export VERSION=$(git describe --tags --exact-match 2>/dev/null \ || git symbolic-ref --short -q HEAD \ || git rev-parse --short HEAD) -echo "Building $VERSION..." -./gradlew -Pversion=$VERSION jar +GITHASH=$(git rev-parse --short=8 HEAD) + +export VERSION_LONG=${VERSION}_${GITHASH} + +# Build this artifact +echo "Building 'NenjimHub v${VERSION_LONG}'..." +./gradlew -Pversion=$VERSION jar prepareDockerLibs + +# Prepare container dependencies +mkdir -p build/docker/conf +cd build/docker +sed -e 's/_VERSION_/'"${VERSION}"'/g' ../../src/main/docker/Dockerfile_template > Dockerfile +cp ../../src/main/docker/start.sh start.sh +cp ../libs/hubd-impl-ref-*.jar libs +cp ../../conf/* conf + +# Build the container +docker build -t r35157/nenjimhub:${VERSION}_amd64 . + +# Prepare publishing script +docker tag r35157/nenjimhub:${VERSION}_amd64 r35157/nenjimhub:latest +sed -e 's/_VERSION_/'"${VERSION}"'/g' ../../src/main/docker/publish_template > publish.sh +chmod 755 publish.sh diff --git a/clean.sh b/clean.sh index d97f0e1..1360dac 100755 --- a/clean.sh +++ b/clean.sh @@ -1 +1,4 @@ +#!/usr/bin/env bash +set -euo pipefail + ./gradlew clean ; rm -rf build diff --git a/conf/log4j2.xml b/conf/log4j2.xml new file mode 100644 index 0000000..4e86dfe --- /dev/null +++ b/conf/log4j2.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/publish.sh b/publish.sh index 5f925a9..bb61727 100755 --- a/publish.sh +++ b/publish.sh @@ -1,6 +1,12 @@ +#!/usr/bin/env bash +set -euo pipefail + export VERSION=$(git describe --tags --exact-match 2>/dev/null \ || git symbolic-ref --short -q HEAD \ || git rev-parse --short HEAD) -echo "Building and publishing $VERSION..." +echo "Publishing artifact to local Maven repo ($VERSION)..." ./gradlew -Pversion=$VERSION publishToMavenLocal + +echo "Publishing docker container to public 'dockerreg.r35157.com' ($VERSION)..." +./build/docker/publish.sh diff --git a/publishCICD.sh b/publishCICD.sh index bf096d6..b7d08ac 100755 --- a/publishCICD.sh +++ b/publishCICD.sh @@ -1,6 +1,12 @@ +#!/usr/bin/env bash +set -euo pipefail + export VERSION=$(git describe --tags --exact-match 2>/dev/null \ || git symbolic-ref --short -q HEAD \ || git rev-parse --short HEAD) -echo "Building and publishing (CI/CD) $VERSION..." +echo "Publishing artifact ($VERSION)..." ./gradlew -Pversion=$VERSION publish + +echo "Publishing docker container ($VERSION)..." +./build/docker/publish.sh diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..618b789 --- /dev/null +++ b/run.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -euo pipefail + +export LOG4J_CONFIGURATION_FILE="$PWD/conf/log4j2.xml" +./gradlew run --args="foo bar baz" diff --git a/src/main/docker/Dockerfile_template b/src/main/docker/Dockerfile_template new file mode 100644 index 0000000..87a791f --- /dev/null +++ b/src/main/docker/Dockerfile_template @@ -0,0 +1,39 @@ +FROM dockerreg.r35157.com/r35157/jre:24.0.1-2_amd64 + +LABEL maintainer="Minimons " + +# Setup environment +ENV NENJIMHUB_HOME=/usr/local/software/nenjimhub +WORKDIR /usr/local/software +USER root +RUN mkdir nenjimhub-_VERSION_ +RUN ln -s nenjimhub-_VERSION_ nenjimhub +WORKDIR nenjimhub +RUN mkdir libs + +# These dirs are expected to be overshadowed by host mounts +RUN mkdir conf logs in out archive + +# Install basic tools +RUN apt-get update +RUN apt-get install -y procps + +# Set timezone +ENV TZ=Europe/Copenhagen +RUN apt-get install -y tzdata \ + && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \ + && echo $TZ > /etc/timezone \ + && dpkg-reconfigure -f noninteractive tzdata + +# Clean-up +RUN apt-get autoremove \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +# Install application and configuration (Do this as late as possible to be able to reuse layers between builds) +COPY start.sh . +COPY conf/* conf +COPY libs/*.jar libs + +CMD ["./start.sh"] + diff --git a/src/main/docker/publish_template b/src/main/docker/publish_template new file mode 100644 index 0000000..3459999 --- /dev/null +++ b/src/main/docker/publish_template @@ -0,0 +1,18 @@ +#!/bin/bash + +set -e + +NAME=nenjimhub +VERSION=_VERSION__amd64 + +VTAG=dockerreg.r35157.com/r35157/${NAME}:${VERSION} +LTAG=dockerreg.r35157.com/r35157/${NAME}:latest + +docker tag r35157/${NAME}:${VERSION} ${VTAG} +docker tag r35157/${NAME}:${VERSION} ${LTAG} + +echo "Publishing of '${VTAG}' and '${LTAG}'..." +docker push ${VTAG} +docker push ${LTAG} + +echo "Publishing completed!" diff --git a/src/main/docker/stack.yml b/src/main/docker/stack.yml new file mode 100644 index 0000000..b826caa --- /dev/null +++ b/src/main/docker/stack.yml @@ -0,0 +1,22 @@ +version: '3.9' + +services: + nenjimhub: + hostname: nenjimhub + image: dockerreg.r35157.com/r35157/nenjimhub:latest + deploy: + replicas: 1 + resources: + limits: + memory: 128m + restart_policy: + condition: any + delay: 5s + max_attempts: 3 + window: 120s + volumes: + - /home/op/nenjimhub/conf:/usr/local/software/nenjimhub/conf + - /home/op/nenjimhub/logs:/usr/local/software/nenjimhub/logs + - /home/op/nenjimhub/in:/usr/local/software/nenjimhub/in + - /home/op/nenjimhub/out:/usr/local/software/nenjimhub/out + - /home/op/nenjimhub/archive:/usr/local/software/nenjimhub/archive diff --git a/src/main/docker/start.sh b/src/main/docker/start.sh new file mode 100755 index 0000000..08ba75c --- /dev/null +++ b/src/main/docker/start.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +CLASSPATH=$(echo libs/*.jar | tr ' ' ':') + +java -Dlog4j.configurationFile=conf/log4j2.xml -cp "$CLASSPATH" com.r35157.nenjim.hubd.impl.ref.Main +sleep 100000 diff --git a/src/main/docker/up_stack b/src/main/docker/up_stack new file mode 100755 index 0000000..bfc60d8 --- /dev/null +++ b/src/main/docker/up_stack @@ -0,0 +1,3 @@ +#!/bin/bash + +docker stack deploy --compose-file /home/op/stack.yml stack --detach=true diff --git a/src/main/java/com/r35157/nenjim/hubd/impl/ref/Main.java b/src/main/java/com/r35157/nenjim/hubd/impl/ref/Main.java new file mode 100644 index 0000000..65e9de4 --- /dev/null +++ b/src/main/java/com/r35157/nenjim/hubd/impl/ref/Main.java @@ -0,0 +1,18 @@ +package com.r35157.nenjim.hubd.impl.ref; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class Main { + public static void main(String[] args) throws Exception { + new Main(); + } + + public Main() throws Exception { + log.info("Initializing NenjimHub..."); + log.info("Ready!"); + Thread.sleep(Long.MAX_VALUE); + } + + private static final Logger log = LogManager.getLogger(Main.class); +} diff --git a/src/main/java/com/r35157/nenjim/hubd/impl/ref/SomeImpl.java b/src/main/java/com/r35157/nenjim/hubd/impl/ref/SomeImpl.java deleted file mode 100644 index c649bd5..0000000 --- a/src/main/java/com/r35157/nenjim/hubd/impl/ref/SomeImpl.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.r35157.nenjim.hubd.impl.ref; - -import com.r35157.nenjim.hubd.SomeInterface; - -public class SomeImpl implements SomeInterface { - public String concat(String x, String y) { - return x + y; - } - - public void divideByZero() throws ArithmeticException { - int a = 0/0; - } -}