145 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			Groovy
		
	
	
	
	
	
			
		
		
	
	
			145 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			Groovy
		
	
	
	
	
	
#!groovy
 | 
						|
 | 
						|
import groovy.json.JsonOutput
 | 
						|
 | 
						|
def deletePod (Map config) {
 | 
						|
def ws = pwd()
 | 
						|
        sh """
 | 
						|
            if [ ! -f "/usr/bin/curl" ] || [ ! -x "/usr/bin/curl" ]; then
 | 
						|
                apk add --no-cache curl
 | 
						|
            fi
 | 
						|
            if [ ! -f "/usr/local/bin/kubectl" ] || [ ! -x "/usr/local/bin/kubectl" ]; then
 | 
						|
                curl -L --silent https://nexus.c.test-chamber-13.lan/repository/google-k8s/\$(curl -s https://nexus.c.test-chamber-13.lan/repository/google-k8s/stable.txt)/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl
 | 
						|
                chmod +x /usr/local/bin/kubectl
 | 
						|
            fi
 | 
						|
        """
 | 
						|
 | 
						|
        withKubeConfig ([
 | 
						|
            credentialsId: config.kubeAuth,
 | 
						|
            serverUrl: config.kubeURL,
 | 
						|
            namespace: config.namespace
 | 
						|
        ]) {
 | 
						|
            sh "for i in \$(kubectl get pods --selector ${config.selector} -o name); do kubectl delete \${i}; done"
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
def createSecret (Map config) {
 | 
						|
    def ws = pwd()
 | 
						|
    sh """
 | 
						|
        if [ ! -f "/usr/bin/curl" ] || [ ! -x "/usr/bin/curl" ]; then
 | 
						|
            apk add --no-cache curl
 | 
						|
        fi
 | 
						|
        if [ ! -f "/usr/local/bin/kubectl" ] || [ ! -x "/usr/local/bin/kubectl" ]; then
 | 
						|
            curl -L --silent https://nexus.c.test-chamber-13.lan/repository/google-k8s/\$(curl -s https://nexus.c.test-chamber-13.lan/repository/google-k8s/stable.txt)/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl
 | 
						|
            chmod +x /usr/local/bin/kubectl
 | 
						|
        fi
 | 
						|
    """
 | 
						|
 | 
						|
    withKubeConfig ([
 | 
						|
        credentialsId: config.kubeAuth,
 | 
						|
        serverUrl: config.kubeURL,
 | 
						|
        namespace: config.namespace
 | 
						|
    ]) {
 | 
						|
        sh """
 | 
						|
            set +x
 | 
						|
            printf '%s\n' "${config.secret}" | kubectl apply -f -
 | 
						|
        """
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
def readSecret (Map config) {
 | 
						|
    def ws = pwd()
 | 
						|
    sh """
 | 
						|
        if [ ! -f "/usr/bin/curl" ] || [ ! -x "/usr/bin/curl" ]; then
 | 
						|
            apk add --no-cache curl
 | 
						|
        fi
 | 
						|
        if [ ! -f "/usr/local/bin/kubectl" ] || [ ! -x "/usr/local/bin/kubectl" ]; then
 | 
						|
            curl -L --silent https://nexus.c.test-chamber-13.lan/repository/google-k8s/\$(curl -s https://nexus.c.test-chamber-13.lan/repository/google-k8s/stable.txt)/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl
 | 
						|
            chmod +x /usr/local/bin/kubectl
 | 
						|
        fi
 | 
						|
    """
 | 
						|
 | 
						|
    withKubeConfig ([
 | 
						|
        credentialsId: config.kubeAuth,
 | 
						|
        serverUrl: config.kubeURL,
 | 
						|
        namespace: config.namespace
 | 
						|
    ]) {
 | 
						|
        def safeSecretID = config.secretID.replaceAll(/\./, "\\\\.")
 | 
						|
        def output = sh(
 | 
						|
            returnStdout: true,
 | 
						|
            script: """#Read Secret
 | 
						|
                set -x
 | 
						|
                kubectl get secret "${config.secretName}" -o jsonpath={".data.${safeSecretID}"} | base64 -d
 | 
						|
            """
 | 
						|
        )
 | 
						|
        return output
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
def buildContainer (Map config, List buildArg = []) {
 | 
						|
    def ws = pwd()
 | 
						|
    writeFile (file: ws + "/Dockerfile", text: config.dockerFile)
 | 
						|
    withCredentials([usernameColonPassword(
 | 
						|
        credentialsId: config.repoCreds,
 | 
						|
        variable: "dCreds"
 | 
						|
    )]) {
 | 
						|
        sh "set +x; printf '{\"auths\":{\"%s\":{\"auth\": \"%s\"}}}' \"${config.repository}\" \"${dcreds.bytes.encodeBase64().toString()}\" > /kaniko/.docker/config.json"
 | 
						|
    }
 | 
						|
    def buildArguments = ""
 | 
						|
    for (i in buildArg) {
 | 
						|
        buildArguments += " --build-arg ${i}"
 | 
						|
    }
 | 
						|
    sh """
 | 
						|
        /kaniko/executor \\
 | 
						|
            --force \\
 | 
						|
            --context "${ws}" \\
 | 
						|
            -f "${ws}/Dockerfile" \\
 | 
						|
            --destination "${config.imageDest}" \\
 | 
						|
            ${buildArguments}
 | 
						|
    """
 | 
						|
}
 | 
						|
 | 
						|
def buildContainerMultipleDestinations (Map config, List buildArg = []) {
 | 
						|
    def ws = pwd()
 | 
						|
    writeFile (file: ws + "/Dockerfile", text: config.dockerFile)
 | 
						|
 | 
						|
    def repoAuth = ["auths": [:]]
 | 
						|
    for (i in config.repositoryAccess) {
 | 
						|
        withCredentials([usernameColonPassword(
 | 
						|
            credentialsId: i.credentials,
 | 
						|
            variable: "dCreds"
 | 
						|
        )]) {
 | 
						|
            repoAuth."auths"[i.repository] = ["auth": dCreds.bytes.encodeBase64().toString()]
 | 
						|
        }
 | 
						|
    }
 | 
						|
    sh "set +x; printf '${JsonOutput.toJson(repoAuth)}' > /kaniko/.docker/config.json"
 | 
						|
 | 
						|
    def buildArguments = ""
 | 
						|
    for (i in config.destination) {
 | 
						|
        buildArguments += " --destination \"${i}\""
 | 
						|
    }
 | 
						|
 | 
						|
    for (i in buildArg) {
 | 
						|
        buildArguments += " --build-arg ${i}"
 | 
						|
    }
 | 
						|
 | 
						|
    sh """
 | 
						|
        /kaniko/executor \\
 | 
						|
            --force \\
 | 
						|
            --context "${ws}" \\
 | 
						|
            -f "${ws}/Dockerfile" \\
 | 
						|
            ${buildArguments}
 | 
						|
    """
 | 
						|
}
 | 
						|
 | 
						|
def pushArtifact (Map config) {
 | 
						|
    def ws = pwd()
 | 
						|
    sh "apk add --no-cache curl"
 | 
						|
    withCredentials([usernameColonPassword(
 | 
						|
        credentialsId: config.repoCreds,
 | 
						|
        variable: "aCreds"
 | 
						|
    )]) {
 | 
						|
        sh 'curl --fail --request PUT --user "$aCreds" --upload-file "' + config.filePath + config.fileName + '" "' + config.fileURL + config.fileName + '"'
 | 
						|
    }
 | 
						|
}
 |