diff --git a/build-factario-container.jenkins b/build-factario-container.jenkins index 1709690..032ce23 100644 --- a/build-factario-container.jenkins +++ b/build-factario-container.jenkins @@ -1,9 +1,14 @@ -def ImageRepository = "registry.test-chamber-13.lan:5000" -def ImageName = "factorio" -def ImageTag = "latest" +def label = "jenkins-${UUID.randomUUID().toString()}" -def AlpineGlibcBaseURL = "https://github.com/sgerrand/alpine-pkg-glibc/releases/download" -def AlpineGlibcPackageVersion = "2.30-r0" +def repository = "registry.test-chamber-13.lan" +def kanikoImage = "${repository}/nhyatt/kaniko:latest" +def imageName = "nhyatt/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" @@ -12,24 +17,21 @@ def GlibcLanguage = "C.UTF-8" def TimeZone = "America/Chicago" -def FactorioDownloadURL = "https://www.factorio.com/get-download/stable/headless/linux64" -def FactorioFileName = "/tmp/factorio.tar" - def dockerFile = """ -FROM alpine:latest +FROM registry.test-chamber-13.lan/nhyatt/alpine:latest MAINTAINER The_Spider ENV PORT=34197 \\ RCON_PORT=27015 \\ - SAVES=/factorio/saves \\ - CONFIG=/factorio/config \\ - MODS=/factorio/mods \\ - SCENARIOS=/factorio/scenarios \\ - SCRIPT_OUTPUT=/factorio/script-output + 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 \\ +RUN apk add --update --no-cache \\ pwgen \\ binutils \\ gettext \\ @@ -58,30 +60,39 @@ RUN \\ 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}" \\ - "export TZ=${TimeZone}" >> /etc/profile.d/locale.sh && \\ - mkdir -p /opt /factorio && \\ - curl -sSL "${FactorioDownloadURL}" -o ${FactorioFileName} && \\ - tar -x -f ${FactorioFileName} --directory /opt && \\ + "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} \\ - ${FactorioFileName} && \\ + /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 /factorio && \\ + chown -R factorio:factorio /opt/factorio /data && \\ printf '%s\\n' \\ "#!/bin/sh -x" \\ "set -e" \\ "id" \\ - "mkdir -p /factorio \${SAVES} \${CONFIG} \${MODS} \${SCENARIOS} \${SCRIPT_OUTPUT}" \\ + "[ ! -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" \\ @@ -111,61 +122,56 @@ RUN \\ USER factorio -CMD ./start-server.sh +WORKDIR /opt/factorio + +ENTRYPOINT [ "/bin/sh", "-c", "./start-server.sh" ] """ podTemplate( - containers: [ - containerTemplate( - name: "docker", - image: "docker:19.03", - command: "sleep", - args: "99d", - envVars: [ - envVar( - key: "DOCKER_HOST", - value: "tcp://localhost:2375" - ) - ] - ), - containerTemplate( - name: "docker-daemon", - image: "docker:19.03-dind", - command: "dockerd-entrypoint.sh", - args: "--insecure-registry registry.test-chamber-13.lan:5000", - privileged: true, - envVars: [ - envVar( - key: "DOCKER_TLS_CERTDIR", - value: "" - ) - ], - ) - ] + 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(POD_LABEL) { - container("docker") { - stage("Test DinD Daemon") { - sh """ - docker version - """ - } - stage("Write Dockerfile"){ - writeFile(file: "Dockerfile", text: dockerFile) - sh """ - cat Dockerfile - """ - } - stage("Build Container"){ - sh """ - docker build --tag ${ImageRepository}/${ImageName}:${ImageTag} ./ - """ - } - stage("Push Container"){ - sh """ - docker push ${ImageRepository}/${ImageName}:${ImageTag} - """ + 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}\"" } } - } } \ No newline at end of file