// Node Label def nodeLabel = "${UUID.randomUUID().toString()}" // Kaniko Image def repository = "registry.c.test-chamber-13.lan" def kanikoImage = "${repository}/library/kaniko:latest" def repositoryCreds = "harbor-repository-creds" // Container Variables def buildImage = "${repository}/library/alpine" def buildImageTag = "latest" // PaperMC URL def paperVersion = "1.18" // Minecraft Configuration def memoryMin = "1g" def memoryMax = "48g" podTemplate( name: "pipelineContainer", label: nodeLabel, yaml: """--- apiVersion: v1 kind: Pod metadata: name: kaniko spec: containers: - name: kaniko image: ${kanikoImage} imagePullPolicy: Always tty: true command: - /busybox/cat - name: alpine image: ${repository}/library/alpine:latest imagePullPolicy: Always tty: true command: - /bin/sh """) { node (nodeLabel) { // Set working directory def workspace = pwd() stage ("Prepare Kaniko") { container ("kaniko") { withCredentials([usernameColonPassword( credentialsId: repositoryCreds, variable: "dCreds", )]) { def dockerJSON = """{ "auths": { "${repository}": { "auth": "${dcreds.bytes.encodeBase64().toString()}" } } }""" sh """ set +x echo '${dockerJSON}' > /kaniko/.docker/config.json """ } } } stage ("Get Paper-MC Version") { container ("alpine") { sh "apk add --no-cache curl jq" paperDownload = sh ( script: """ paperBuild=\$(curl --silent --location --fail https://papermc.io/api/v2/projects/paper/versions/${paperVersion} | jq '.builds | max') paperFile=\$(curl --silent --location --fail https://papermc.io/api/v2/projects/paper/versions/${paperVersion}/builds/\${paperBuild} | jq '.downloads.application.name') echo https://papermc.io/api/v2/projects/paper/versions/${paperVersion}/builds/\${paperBuild}/downloads/\${paperFile} """, returnStdout: true ).trim() } } stage ("Create Dockerfile") { // Set dockerfile def dockerFile = """ FROM ${buildImage}:${buildImageTag} as builder RUN addgroup -S -g 1000 minecraft && \\ adduser --disabled-password -G minecraft --gecos "application account" --home "/minecraft" --shell "/sbin/nologin" --uid 1000 minecraft && \\ apk add --no-cache curl && \\ mkdir /minecraft/data && \\ mkdir /minecraft/html && \\ curl --location --fail --silent \${paperDownload} -o /minecraft/paper-mc.jar FROM gcr.io/distroless/java17:latest COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt COPY --from=builder /etc/passwd /etc/group /etc/ COPY --from=builder --chown=minecraft:minecraft /minecraft/ /minecraft/ USER minecraft WORKDIR /minecraft/data CMD ["java", "-Xms${memoryMin}", "-Xmx${memoryMax}", "-jar ../paper-mc.jar"] """ writeFile(file: workspace + '/Dockerfile', text: dockerFile) } stage ("Build & Push") { container ("kaniko") { sh """ /kaniko/executor \\ --cleanup \\ --context "${workspace}" \\ -f "${workspace}/Dockerfile" \\ --destination "${repository}/library/minecraft:latest" \\ --build-arg "paperDownload=${paperDownload}" """ } } } }