NoIssue: Support for building runable docker container
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@
|
||||
|
||||
# Ignore Gradle build output directory
|
||||
build
|
||||
/logs/NenjimHub.log
|
||||
|
||||
@@ -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<Sync>("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<JavaCompile> {
|
||||
options.release.set(24)
|
||||
}
|
||||
|
||||
28
build.sh
28
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
|
||||
|
||||
3
clean.sh
3
clean.sh
@@ -1 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
./gradlew clean ; rm -rf build
|
||||
|
||||
23
conf/log4j2.xml
Normal file
23
conf/log4j2.xml
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<RollingFile name="File"
|
||||
fileName="logs/NenjimHub.log"
|
||||
filePattern="logs/NenjimHub-%d{yyyy-MM-dd}-%i.log.gz">
|
||||
<PatternLayout pattern="%d{ISO8601} %-5p %c - %m%n"/>
|
||||
|
||||
<Policies>
|
||||
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
|
||||
<SizeBasedTriggeringPolicy size="50 MB"/>
|
||||
</Policies>
|
||||
|
||||
<DefaultRolloverStrategy max="14"/>
|
||||
</RollingFile>
|
||||
</Appenders>
|
||||
|
||||
<Loggers>
|
||||
<Root level="info">
|
||||
<AppenderRef ref="File"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
5
run.sh
Executable file
5
run.sh
Executable file
@@ -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"
|
||||
39
src/main/docker/Dockerfile_template
Normal file
39
src/main/docker/Dockerfile_template
Normal file
@@ -0,0 +1,39 @@
|
||||
FROM dockerreg.r35157.com/r35157/jre:24.0.1-2_amd64
|
||||
|
||||
LABEL maintainer="Minimons <minimons@r35157.com>"
|
||||
|
||||
# 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"]
|
||||
|
||||
18
src/main/docker/publish_template
Normal file
18
src/main/docker/publish_template
Normal file
@@ -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!"
|
||||
22
src/main/docker/stack.yml
Normal file
22
src/main/docker/stack.yml
Normal file
@@ -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
|
||||
6
src/main/docker/start.sh
Executable file
6
src/main/docker/start.sh
Executable file
@@ -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
|
||||
3
src/main/docker/up_stack
Executable file
3
src/main/docker/up_stack
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
|
||||
docker stack deploy --compose-file /home/op/stack.yml stack --detach=true
|
||||
18
src/main/java/com/r35157/nenjim/hubd/impl/ref/Main.java
Normal file
18
src/main/java/com/r35157/nenjim/hubd/impl/ref/Main.java
Normal file
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user