#!groovy

def repository = "registry.c.test-chamber-13.lan"
def repositoryCreds = "harbor-repository-creds"

def label = "kubernetes-${UUID.randomUUID().toString()}"
def templateName = "pipeline-worker"
podTemplate(
    label: label,
    name: templateName,
    yaml: functions.podYaml(
        repo: repository,
        templateName: templateName,
        [
            [
                name: "compile",
                path: "${repository}/dockerhub/library/golang:alpine",
            ]
        ]
    )
) {
    node (label) {
        def workspace = pwd()

        stage ("Pull Local Repo") {
            checkout([
                $class: "GitSCM",
                branches: [
                    [
                        name: "refs/remotes/origin/main",
                    ],
                ],
                userRemoteConfigs: [
                    [
                        url: "ssh://git@gitea.smoothnet.org:31822/nhyatt/bind-response-policy-zone-creator.git",
                        credentialsId: "Gitea-Read-Only-Token",
                    ],
                ],
                extensions: [
                    [
                        $class: "CloneOption",
                        shallow: true,
                    ],
                    [
                        $class: "CheckoutOption",
                        timeout: 2,
                    ],
                ],
            ])
        }

        stage("Build Binary") {
            container("compile") {
                sh """
                    apk add --no-cache gcc musl-dev upx
                    GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -v -ldflags="-s -w" -tags timetzdata -o pihole-zone-generator ./cmd/bind
                    upx --lzma pihole-zone-generator
                """
            }
        }

        stage ("Store Jenkins Artifacts") {
            archiveArtifacts (artifacts: "pihole-zone-generator",
                fingerprint: true,
                allowEmptyArchive: false,
                onlyIfSuccessful: true,
            )
        }

        functions.pushArtifact(
            repoCreds: "nexus-generic-upload-bot",
            fileName: "pihole-zone-generator",
            filePath: "${workspace}/pihole-zone-generator",
            fileURL: "https://nexus.c.test-chamber-13.lan/repository/generic/go/"
        )
    }
}