factorio build to use custom functions and sperates from glibc build process

This commit is contained in:
Hyatt 2022-01-28 09:46:33 -06:00
parent 8ff1c8676e
commit fa6bf6b241
Signed by: nhyatt
GPG Key ID: C50D0BBB5BC40BEA

View File

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