def label = "jenkins-${UUID.randomUUID().toString()}" def repository = "registry.c.test-chamber-13.lan" def repositoryCreds = "harbor-repository-creds" def kanikoImage = "${repository}/library/kaniko:latest" def imageName = "library/factorio" def imageTag = "latest" def FactorioDownloadURL="https://factorio.com/get-download/stable/headless/linux64" def AlpineGlibcBaseURL="https://github.com/sgerrand/alpine-pkg-glibc/releases/download" def AlpineGlibcPackageVersion = "2.33-r0" def AlpineGlibcBasePackageFilename = "glibc-${AlpineGlibcPackageVersion}.apk" def AlpineGlibcBinPackageFilename = "glibc-bin-${AlpineGlibcPackageVersion}.apk" def AlpineGlibcI18nPackageFilename = "glibc-i18n-${AlpineGlibcPackageVersion}.apk" def GlibcLanguage = "C.UTF-8" def TimeZone = "America/Chicago" def dockerFile = """ FROM registry.c.test-chamber-13.lan/library/alpine:latest MAINTAINER The_Spider ENV PORT=34197 \\ RCON_PORT=27015 \\ SAVES=/data/saves \\ CONFIG=/data/config \\ MODS=/data/mods \\ SCENARIOS=/data/scenarios \\ SCRIPT_OUTPUT=/data/script-output \\ TZ=${TimeZone} RUN apk add --update --no-cache \\ pwgen \\ binutils \\ gettext \\ libintl \\ shadow && \\ apk add --update --no-cache --virtual=.build-dependencies \\ curl \\ ca-certificates && \\ printf '%s\\n' \\ "-----BEGIN PUBLIC KEY-----" \\ "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApZ2u1KJKUu/fW4A25y9m" \\ "y70AGEa/J3Wi5ibNVGNn1gT1r0VfgeWd0pUybS4UmcHdiNzxJPgoWQhV2SSW1JYu" \\ "tOqKZF5QSN6X937PTUpNBjUvLtTQ1ve1fp39uf/lEXPpFpOPL88LKnDBgbh7wkCp" \\ "m2KzLVGChf83MS0ShL6G9EQIAUxLm99VpgRjwqTQ/KfzGtpke1wqws4au0Ab4qPY" \\ "KXvMLSPLUp7cfulWvhmZSegr5AdhNw5KNizPqCJT8ZrGvgHypXyiFvvAH5YRtSsc" \\ "Zvo9GI2e2MaZyo9/lvb+LbLEJZKEQckqRj4P26gmASrZEPStwc+yqy1ShHLA0j6m" \\ "1QIDAQAB" \\ "-----END PUBLIC KEY-----" > "/etc/apk/keys/sgerrand.rsa.pub" && \\ curl -sSLO "${AlpineGlibcBaseURL}/${AlpineGlibcPackageVersion}/${AlpineGlibcBasePackageFilename}" && \\ curl -sSLO "${AlpineGlibcBaseURL}/${AlpineGlibcPackageVersion}/${AlpineGlibcBinPackageFilename}" && \\ curl -sSLO "${AlpineGlibcBaseURL}/${AlpineGlibcPackageVersion}/${AlpineGlibcI18nPackageFilename}" && \\ apk add --no-cache \\ "${AlpineGlibcBasePackageFilename}" \\ "${AlpineGlibcBinPackageFilename}" \\ "${AlpineGlibcI18nPackageFilename}" && \\ rm -v "/etc/apk/keys/sgerrand.rsa.pub" && \\ /usr/glibc-compat/bin/localedef --force --inputfile POSIX --charmap UTF-8 "${GlibcLanguage}" || true && \\ printf '%s\\n' \\ "export LANG=${GlibcLanguage}" && \\ mkdir -p /opt/factorio \\ /data \\ \${SAVES} \\ \${CONFIG} \\ \${MODS} \\ \${SCENARIOS} \\ \${SCRIPT_OUTPUT} && \\ curl --location --silent "${FactorioDownloadURL}" --output /tmp/factorio.tar.xz && \\ tar -x -f /tmp/factorio.tar.xz --directory /opt && \\ apk del glibc-i18n && \\ apk del .build-dependencies && \\ rm -v \\ ${AlpineGlibcBasePackageFilename} \\ ${AlpineGlibcBinPackageFilename} \\ ${AlpineGlibcI18nPackageFilename} \\ /tmp/factorio.tar.xz && \\ ln -s \${SAVES} /opt/factorio/saves && \\ ln -s \${MODS} /opt/factorio/mods && \\ ln -s \${SCENARIOS} /opt/factorio/scenarios && \\ ln -s \${SCRIPT_OUTPUT} /opt/factorio/script-output && \\ addgroup -g 1000 -S factorio && \\ adduser -u 1000 -G factorio -s /bin/sh -SDH factorio && \\ chown -R factorio:factorio /opt/factorio /data && \\ printf '%s\\n' \\ "#!/bin/sh -x" \\ "set -e" \\ "id" \\ "[ ! -d \\\"\${SAVES}\\\" ] && mkdir -p \${SAVES}" \ "[ ! -d \\\"\${CONFIG}\\\" ] && mkdir -p \${CONFIG}" \ "[ ! -d \\\"\${MODS}\\\" ] && mkdir -p \${MODS}" \ "[ ! -d \\\"\${SCENARIOS}\\\" ] && mkdir -p \${SCENARIOS}" \ "[ ! -d \\\"\${SCRIPT_OUTPUT}\\\" ] && mkdir -p \${SCRIPT_OUTPUT}" \ "[ ! -f \\\"\${CONFIG}/rconpw\\\" ] && printf '%s' \\\"\\\$(pwgen 15 1)\\\" > \${CONFIG}/rconpw" \\ "[ ! -f \\\"\${CONFIG}/server-settings.json\\\" ] && cp /opt/factorio/data/server-settings.example.json \${CONFIG}/server-settings.json" \\ "[ ! -f \\\"\${CONFIG}/map-gen-settings.json\\\" ] && cp /opt/factorio/data/map-gen-settings.example.json \${CONFIG}/map-gen-settings.json" \\ "[ ! -f \\\"\${CONFIG}/map-settings.json\\\" ] && cp /opt/factorio/data/map-settings.example.json \${CONFIG}/map-settings.json" \\ "if find -L \${SAVES} -iname \\*.tmp.zip -mindepth 1 -print | grep -q .; then" \\ " rm -f \${SAVES}/*.tmp.zip" \\ "fi" \\ "if ! find -L \${SAVES} -iname \\*.zip -mindepth 1 -print | grep -q .; then" \\ " /opt/factorio/bin/x64/factorio \\\\" \\ " --create \${SAVES}/_autosave1.zip \\\\" \\ " --map-gen-settings \${CONFIG}/map-gen-settings.json \\\\" \\ " --map-settings \${CONFIG}/map-settings.json" \\ "fi" \\ "/opt/factorio/bin/x64/factorio \\\\" \\ " --port \${PORT} \\\\" \\ " --start-server-load-latest \\\\" \\ " --server-settings \${CONFIG}/server-settings.json \\\\" \\ " --server-banlist \${CONFIG}/server-banlist.json \\\\" \\ " --rcon-port \${RCON_PORT} \\\\" \\ " --server-whitelist \${CONFIG}/server-whitelist.json \\\\" \\ " --use-server-whitelist \\\\" \\ " --server-adminlist \${CONFIG}/server-adminlist.json \\\\" \\ " --rcon-password \\\"\\\$(cat \${CONFIG}/rconpw)\\\" \\\\" \\ " --server-id /factorio/config/server-id.json \\\\" \\ " \${@}" > /opt/factorio/start-server.sh && \\ chmod +x /opt/factorio/start-server.sh USER factorio WORKDIR /opt/factorio ENTRYPOINT [ "/bin/sh", "-c", "/opt/factorio/start-server.sh" ] """ podTemplate( label: label, name: "pipeline-worker", yaml: """--- apiVersion: v1 kind: Pod metadata: name: pipeline-worker spec: containers: - name: kaniko imagePullPolicy: Always image: ${kanikoImage} tty: true command: - /busybox/cat """, ) { node(label) { 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("Build Factorio Image") { container("kaniko") { writeFile(file: workspace + "/Dockerfile", text: dockerFile) sh "/kaniko/executor --cleanup --context \"${workspace}\" -f \"${workspace}/Dockerfile\" --destination \"${repository}/${imageName}:${imageTag}\"" } } } }