build-rcon now uses supporting libraries
This commit is contained in:
		@@ -1,74 +1,56 @@
 | 
			
		||||
#!groovy
 | 
			
		||||
 | 
			
		||||
def label = podTemplates.podLabel()
 | 
			
		||||
def repository = "registry.c.test-chamber-13.lan"
 | 
			
		||||
def repositoryCreds = "harbor-repository-creds"
 | 
			
		||||
 | 
			
		||||
podTemplate(
 | 
			
		||||
    label: label,
 | 
			
		||||
    name: "pipeline-worker",
 | 
			
		||||
    yaml: podTemplates.podYaml(
 | 
			
		||||
        repo: repository,
 | 
			
		||||
        kaniko: true,
 | 
			
		||||
        alpine: true,
 | 
			
		||||
    ),
 | 
			
		||||
) {
 | 
			
		||||
    node (label) {
 | 
			
		||||
        def workspace = pwd()
 | 
			
		||||
def dockerFile = """FROM ${repository}/library/alpine:latest as builder
 | 
			
		||||
 | 
			
		||||
        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
 | 
			
		||||
                    """
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
RUN 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 "/tmp/rcon"
 | 
			
		||||
 | 
			
		||||
        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
 | 
			
		||||
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
 | 
			
		||||
COPY --from=builder --chown=rcon:rcon /tmp/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"
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
def label = "kubernetes-${UUID.randomUUID().toString()}"
 | 
			
		||||
def templateName = "pipeline-worker"
 | 
			
		||||
podTemplate (
 | 
			
		||||
    label: label,
 | 
			
		||||
    name: templateName,
 | 
			
		||||
    yaml: functions.podYaml(
 | 
			
		||||
        repo: repository,
 | 
			
		||||
        templateName: templateName,
 | 
			
		||||
        kaniko: true,
 | 
			
		||||
        alpine: true
 | 
			
		||||
    )
 | 
			
		||||
){
 | 
			
		||||
    node (label) {
 | 
			
		||||
        functions.buildContainer(
 | 
			
		||||
            repository: repository,
 | 
			
		||||
            imageDest: "${repository}/library/rcon:latest",
 | 
			
		||||
            dockerFile: dockerFile,
 | 
			
		||||
            repoCreds: repositoryCreds,
 | 
			
		||||
        )
 | 
			
		||||
        functions.deletePod(
 | 
			
		||||
            kubeAuth: "rancher-admin-token",
 | 
			
		||||
            kubeURL: "https://rancher.test-chamber-13.lan/k8s/clusters/c-mc9cq",
 | 
			
		||||
            namespace: "game-servers",
 | 
			
		||||
            selector: "app=rcon"
 | 
			
		||||
        )
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,39 +0,0 @@
 | 
			
		||||
#!groovy
 | 
			
		||||
 | 
			
		||||
// Return random label for runner
 | 
			
		||||
def podLabel() {
 | 
			
		||||
    return "jenkins-${UUID.randomUUID().toString()}"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// return kubernetes pod defination
 | 
			
		||||
def podYaml(Map config){
 | 
			
		||||
    def output = """
 | 
			
		||||
---
 | 
			
		||||
apiVersion: v1
 | 
			
		||||
kind: Pod
 | 
			
		||||
metadata:
 | 
			
		||||
  name: pipeline-worker
 | 
			
		||||
spec:
 | 
			
		||||
  containers:
 | 
			
		||||
"""
 | 
			
		||||
    if (config.kaniko == true) {
 | 
			
		||||
        output += """
 | 
			
		||||
  - name: kaniko
 | 
			
		||||
    imagePullPolicy: Always
 | 
			
		||||
    image: ${config.repo}/library/kaniko:latest
 | 
			
		||||
    tty: true
 | 
			
		||||
    command:
 | 
			
		||||
    - /busybox/sh
 | 
			
		||||
"""
 | 
			
		||||
    }
 | 
			
		||||
    if (config.alpine == true) {
 | 
			
		||||
        output += """
 | 
			
		||||
  - name: alpine
 | 
			
		||||
    imagePullPolicy: Always
 | 
			
		||||
    image: ${config.repo}/library/alpine:latest
 | 
			
		||||
    tty: true
 | 
			
		||||
"""
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return output
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user