From cbf0472b90a734f594a20194fa4939fd904332e0 Mon Sep 17 00:00:00 2001 From: The_Spider Date: Tue, 29 Mar 2022 09:43:22 -0500 Subject: [PATCH] Adds declarative functions --- vars/declarativeFunctions.groovy | 100 ++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) diff --git a/vars/declarativeFunctions.groovy b/vars/declarativeFunctions.groovy index 18186de..57a5ed8 100644 --- a/vars/declarativeFunctions.groovy +++ b/vars/declarativeFunctions.groovy @@ -1,5 +1,103 @@ #!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 "printf '%s\n' \"" + config.secret + "\" | kubectl apply -f -" + } +} + +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 \\ + --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 \\ + --context "${ws}" \\ + -f "${ws}/Dockerfile" \\ + ${buildArguments} + """ +} + def pushArtifact (Map config) { def ws = pwd() sh "apk add --no-cache curl" @@ -9,4 +107,4 @@ def pushArtifact (Map config) { )]) { sh 'curl --fail --request PUT --user "$aCreds" --upload-file "' + config.filePath + config.fileName + '" "' + config.fileURL + config.fileName + '"' } -} \ No newline at end of file +}