cant use podtemplate in a function

This commit is contained in:
Hyatt 2022-01-11 17:33:36 -06:00
parent 4ea9e666e3
commit 30b78e6bde
Signed by: nhyatt
GPG Key ID: C50D0BBB5BC40BEA

View File

@ -1,96 +1,71 @@
#!groovy #!groovy
def deletePod( def podYaml (Map config) {
String kubeAuth, def output = """
String kubeURL,
String namespace,
String selector
){
def label = "kubernetes-${UUID.randomUUID().toString()}"
def ws = pwd()
podTemplate(
label: label,
name: "pipeline-worker",
yaml: """
--- ---
apiVersion: v1 apiVersion: v1
kind: Pod kind: Pod
metadata: metadata:
name: pipeline-worker name: ${config.templateName}
spec: spec:
containers: containers:
- name: alpine """
if (config.kaniko == true) {
output += """
- name: kaniko
imagePullPolicy: Always imagePullPolicy: Always
image: ${repository}/library/alpine:latest image: ${config.repo}/library/kaniko:latest
tty: true tty: true
command: command:
- /busybox/sh - /busybox/sh
""", """
){ }
node(label) { if (config.alpine == true) {
container("alpine") { output += """
sh """ - name: alpine
apk add --no-cache curl imagePullPolicy: Always
curl -L --silent https://storage.googleapis.com/kubernetes-release/release/\$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl image: ${config.repo}/library/alpine:latest
chmod +x /usr/local/bin/kubectl tty: true
""" """
}
withKubeConfig([ return output
credentialsId: kubeAuth, }
serverUrl: kubeURL,
namespace: namespace, def deletePod (Mab config) {
]) { def ws = pwd()
sh "for i in \$(kubectl get pods --selector ${selector} -o name); do kubectl delete \${i}; done" container ("alpine") {
} sh """
} apk add --no-cache curl
curl -L --silent https://storage.googleapis.com/kubernetes-release/release/\$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl
chmod +x /usr/local/bin/kubectl
"""
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 buildContainer(String repository, def buildContainer (Map config) {
String imageDest,
String dockerFile,
String repoCreds) {
def label = "kaniko-${UUID.randomUUID().toString()}"
def ws = pwd() def ws = pwd()
container("kaniko") {
podTemplate( writeFile (file: ws + "/Dockerfile", text: config.dockerFile)
label: label, withCredentials([usernameColonPassword(
name: "pipeline-worker", credentialsId: config.repoCreds,
yaml: """ variable: "dCreds",
--- )]) {
apiVersion: v1 sh "set +x; printf '{\"auths\":{\"%s\":{\"auth\": \"%s\"}}}' \"${config.repository}\" \"${dcreds.bytes.encodeBase64().toString()}\" > /kaniko/.docker/config.json"
kind: Pod
metadata:
name: pipeline-worker
spec:
containers:
- name: kaniko
imagePullPolicy: Always
image: ${repository}/library/kaniko:latest
tty: true
command:
- /busybox/sh
""",
){
node(label) {
container("kaniko") {
writeFile(file: ws + "/Dockerfile", text: DF)
withCredentials([usernameColonPassword(
credentialsId: repoCreds,
variable: "dCreds",
)]) {
sh "set +x; printf '{\"auths\":{\"%s\":{\"auth\": \"%s\"}}}' \"${repository}\" \"${dcreds.bytes.encodeBase64().toString()}\" > /kaniko/.docker/config.json"
}
sh """
/kaniko/executor \\
--context "${ws}" \\
-f "${ws}/Dockerfile" \\
--destination "${repository}/${imageDest}
"""
}
} }
sh """
/kaniko/executor \\
--context "${ws}" \\
-f "${ws}/Dockerfile" \\
--destination "${config.imageDest}"
"""
} }
} }