From 575b236d0ee3356cb05cfc4b8b8532f869cf4329 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Wed, 12 Mar 2025 20:36:04 -0500 Subject: [PATCH] kubeRestartDeployment: Add function The `kubeRestartDeployment` function restarts a Kubernetes deployment using `kubectl rollout restart`. This command is run in a dedicated pod, with the `kubectl` binary bind-mounted from the host. --- vars/kubeRestartDeployment.groovy | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 vars/kubeRestartDeployment.groovy diff --git a/vars/kubeRestartDeployment.groovy b/vars/kubeRestartDeployment.groovy new file mode 100644 index 0000000..b0103b1 --- /dev/null +++ b/vars/kubeRestartDeployment.groovy @@ -0,0 +1,35 @@ +def POD_YAML = '''\ +spec: + containers: + - name: jnlp + volumeMounts: + - name: kubectl + mountPath: /bin/kubectl + readOnly: true + volumes: + - name: kubectl + hostPath: + path: /usr/bin/kubectl + type: File +''' + +def call(args) { + def namespace = args?.namespace + def name = args?.name + + if (name == null) { + name = env.JOB_NAME.split('/')[1] + } + + if (namespace == null) { + namespace = null + } + + podTemplate(yaml: POD_YAML) { + node(POD_LABEL) { + stage('Deploy') { + sh "kubectl rollout restart deployment -n ${namespace} ${name}" + } + } + } +}