Skip to content

Commit

Permalink
Add dockerfile and embed git hash into properties file
Browse files Browse the repository at this point in the history
  • Loading branch information
1Revenger1 committed Sep 10, 2024
1 parent 640a30e commit 8ccdaa9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
ARG APP_DIR=/opt/wylx
ARG BUILD_DIR=/tmp/wylx
ARG GIT_COMMIT="Unknown"

# Build Container
FROM gradle:jdk17-alpine AS BUILD

ARG BUILD_DIR
ARG GIT_COMMIT
ENV GIT_COMMIT=$GIT_COMMIT
WORKDIR $BUILD_DIR
COPY --chown=gradle:gradle build.gradle settings.gradle $BUILD_DIR/
COPY --chown=gradle:gradle src $BUILD_DIR/src

RUN gradle build --no-daemon

RUN unzip $BUILD_DIR/build/distributions/WylxBot.zip -d $BUILD_DIR/unzip

# Final Application Container
FROM eclipse-temurin:17-jre-alpine
ARG BUILD_DIR
ARG APP_DIR
WORKDIR $APP_DIR

COPY --from=BUILD $BUILD_DIR/unzip $APP_DIR/

ENTRYPOINT ./WylxBot/bin/WylxBot
18 changes: 17 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'org.wylx'
version '1.0-SNAPSHOT'
version = System.getenv("GIT_COMMIT") ?: "git describe --dirty --always --exclude '*'".execute().text.trim()

repositories {
mavenCentral()
Expand Down Expand Up @@ -46,3 +46,19 @@ tasks.jar {
tasks.distZip {
archiveFileName.set("${project.name}.zip")
}

// Create version.properties with git hash
task createProperties(dependsOn: processResources) {
doLast {
new File("$buildDir/resources/main/version.properties").withWriter { w ->
Properties p = new Properties()
p['version'] = version.toString()
p['build-date'] = new Date().format("EEE MMM dd HH':'mm':'ss zzz yyyy")
p.store w, null
}
}
}

classes {
dependsOn createProperties
}
24 changes: 20 additions & 4 deletions src/main/java/com/wylxbot/wylx/Commands/BotUtil/StatusCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.*;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.stream.Stream;


public class StatusCommand extends ServerCommand {
private final String commitID = getCommitID();
private final String mCommitID;
private final String mBuildDate;
private final boolean mIsDocker = EnvUtils.isRunningInsideDocker();

private static final long BYTES_PER_MEGABYTE = 1024 * 1024;
private static final long MILLI_PER_SECOND = 1000;
Expand All @@ -28,6 +34,15 @@ public class StatusCommand extends ServerCommand {

public StatusCommand() {
super("status", CommandPermission.EVERYONE, "Provides information on the host machine of the bot", "system", "stats");
Properties prop = new Properties();
try (InputStream inputStream = StatusCommand.class.getResourceAsStream("/version.properties")) {
prop.load(inputStream);
} catch (IOException e) {
e.printStackTrace(System.out);
System.exit(1);
}
mBuildDate = prop.getProperty("build-date");
mCommitID = prop.getProperty("version");
}

private String getCommitID() {
Expand Down Expand Up @@ -91,7 +106,8 @@ public void runCommand(CommandContext ctx) {

String systemBuilder =
String.format("OS: %s\n", System.getProperty("os.name")) +
String.format("Commit: %s\n", commitID) +
String.format("Commit: %s\n", mCommitID) +
String.format("Build Date: %s\n", mBuildDate) +
String.format("Threads: %d\n", rt.availableProcessors()) +
String.format("Bot Uptime: %d Days, %d Hours, and %d minutes\n",
milliToDays(uptime), milliToHours(uptime), milliToMinutes(uptime)) +
Expand Down

0 comments on commit 8ccdaa9

Please sign in to comment.