diff --git a/build-rcon.jenkins b/build-rcon.jenkins new file mode 100644 index 0000000..f8f896e --- /dev/null +++ b/build-rcon.jenkins @@ -0,0 +1,72 @@ +#!groovy + +def label = podLabel() +def repositoryCreds = "harbor-repository-creds" + +podTemplate( + label: label, + name: "pipeline-worker", + yaml: podYaml( + kaniko: true, + alpine: true, + ), +) { + node (label) { + 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("Download RCON") { + container("alpine") { + sh """ + apk add --no-cache curl jq + VER="\$(curl --silent "https://api.github.com/repos/gorcon/rcon-cli/releases/latest" | jq -r '.tag_name' | sed -r 's/v//')" + curl --silent \\ + --location \\ + "https://github.com/gorcon/rcon-cli/releases/download/v\${VER}/rcon-\${VER}-amd64_linux.tar.gz" \\ + | \\ + tar -z -x -C /tmp -f - + cp /tmp/rcon-\${VER}-amd64_linux/rcon "${workspace}/rcon" + """ + } + } + + stage("Build Image") { + container("kaniko") { + def DF = """FROM ${repository}/library/alpine:latest + +RUN addgroup -S -g 1000 rcon && \\ + adduser --disabled-password -G rcon --gecos "rcon" --home "/home/rcon" --shell "/sbin/nologin" --uid 1000 rcon + +COPY --chown=rcon:rcon ./rcon /home/rcon/rcon + +USER rcon:rcon +WORKDIR /home/rcon + +ENTRYPOINT ["/bin/sh"] +""" + writeFile(file: workspace + "/Dockerfile", text: DF) + sh "/kaniko/executor --context \"${workspace}\" -f \"${workspace}/Dockerfile\" --destination \"${repository}/library/rcon:latest\" --single-snapshot" + } + } + } +} diff --git a/functions/PodTemplates.groovy b/functions/PodTemplates.groovy new file mode 100644 index 0000000..6f15fd9 --- /dev/null +++ b/functions/PodTemplates.groovy @@ -0,0 +1,40 @@ +#!groovy + +// Return random label for runner +def podLabel() { + return "jenkins-${UUID.randomUUID().toString()}" +} + +// return kubernetes pod defination +def podYaml(def requiredContainers){ + def dockerRepo = "registry.c.test-chamber-13.lan" + def output = """ +--- +apiVersion: v1 +kind: Pod +metadata: + name: pipeline-worker +spec: + containers: +""" + if requiredContainers['kaniko'] == true { + output += """ + - name: kaniko + imagePullPolicy: Always + image: ${dockerRepo}/library/kaniko:latest + tty: true + command: + - /busybox/sh +""" + } + if requiredContainer['alpine'] == true { + output += """ + - name: alpine + imagePullPolicy: Always + image: ${dockerRepo}/library/alpine:latest + tty: true +""" + } + + return output +}