Skip to content

Commit

Permalink
chore: Automatic Docker build using jlink to reduce jvm size
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Jun 14, 2024
1 parent 83bb8fe commit 57250f6
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 14 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/publish-images.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: build java
on:
push:
branches:
- master
workflow_dispatch:

env:
REGISTRY: ghcr.io

jobs:
push:
name: "jib Docker build"
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4

# - name: Set up QEMU for multi-platform support
# uses: docker/setup-qemu-action@v3

# - name: Set up Docker Buildx
# uses: docker/setup-buildx-action@v3
# with:
# buildkitd-flags: --debug

- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Set up JDK
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 21
cache: gradle

- name: Publish image
run: gradle jib
Empty file added README.md
Empty file.
53 changes: 49 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
plugins {
alias(libs.plugins.kotlinJvm)
alias(libs.plugins.kotlinx.serialization)
application
id("com.google.cloud.tools.jib") version "3.4.3"
id("com.ryandens.jlink-application") version "0.4.0"
}

repositories {
Expand All @@ -14,11 +17,53 @@ dependencies {
implementation(libs.kotlinx.serialization.json)
implementation(libs.kaml)
implementation(libs.clikt)
implementation(libs.slf4j)
}

tasks.test {
useJUnitPlatform()
}
kotlin {
jvmToolchain(17)
jvmToolchain(21)
}

application {
mainClass = "com.mineinabyss.discord.bot.MainKt"
}

tasks.jibDockerBuild {
dependsOn("jlinkJre")
}

jlinkJre {
modules.set(setOf("java.logging", "java.management", "jdk.crypto.ec", "java.naming")) // defaults to only java.base
}

jib {
from.image = "gcr.io/distroless/java-base-debian11:nonroot-amd64"
to.image = "ghcr.io/mineinabyss/discord-role-picker"
extraDirectories {
paths {
path {
setFrom(project.file("build/jlink-jre/jre"))
into = "/usr/local"
}
}
permissions = mapOf("/usr/local/bin/java" to "755")
}
container {
environment = mapOf("BOT_CONFIG" to "/app/config.yml")
creationTime = "USE_CURRENT_TIMESTAMP"
ports = listOf("8080")

// good defauls intended for Java 8 (>= 8u191) containers
jvmFlags = listOf(
"-server",
"-Djava.awt.headless=true",
"-XX:InitialRAMFraction=2",
"-XX:MinRAMFraction=2",
"-XX:MaxRAMFraction=2",
"-XX:+UseG1GC",
"-XX:MaxGCPauseMillis=100",
"-XX:+UseStringDeduplication"
)
}
}

7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
discord-role-picker:
image: ghcr.io/mineinabyss/discord-role-picker
environment:
BOT_TOKEN: "YOUR_BOT_TOKEN"
volumes:
- ./config.yml:/app/config.yml
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ kaml = "0.60.0"
kordExtensions = "1.8.1-SNAPSHOT"
kotlin = "2.0.0"
kotlinx-serialization = "1.7.0"
slf4j = "2.0.9"

[libraries]
clikt = { module = "com.github.ajalt.clikt:clikt", version.ref = "clikt" }
kaml = { module = "com.charleskorn.kaml:kaml", version.ref = "kaml" }
kordExtensions = { module = "com.kotlindiscord.kord.extensions:kord-extensions", version.ref = "kordExtensions" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
slf4j = { module = "org.slf4j:slf4j-simple", version.ref = "slf4j" }

[plugins]
kotlinJvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 0 additions & 2 deletions src/main/kotlin/com/mineinabyss/discord/bot/CreateButtons.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ class CreateButtons(
event<SelectMenuInteractionCreateEvent> {
action {
val comp = event.interaction.componentId
println("Interact $comp")
when {
comp.startsWith("select/") -> {
event.interaction.channel.createMessage {
Expand All @@ -108,7 +107,6 @@ class CreateButtons(
event<ButtonInteractionCreateEvent> {
action {
val comp = event.interaction.componentId
println("Interact $comp")
when {
comp.startsWith("roles/") -> {
val response = event.interaction.respondEphemeral {
Expand Down
8 changes: 0 additions & 8 deletions src/main/kotlin/com/mineinabyss/discord/bot/Main.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
package com.mineinabyss.discord.bot

import com.mineinabyss.discord.config.RoleGroup
import com.mineinabyss.discord.config.RoleOption
import dev.kord.common.entity.ButtonStyle
import dev.kord.common.entity.DiscordPartialEmoji
import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.interaction.response.MessageInteractionResponseBehavior
import kotlinx.coroutines.sync.Mutex

fun main(args: Array<String>) = StartCommand().main(args)

0 comments on commit 57250f6

Please sign in to comment.