Verifying a Kubernetes Deployment in a Jenkins pipeline

One of the tests I’ve been missing in my pipeline was a crucial one. Verifying that the deployment finished – successfully. e.g. that the pods were updates with a new Docker container image, but also restarted successfully(!).

It’s pretty simple I suppose, but here is my interpretation.
The following should sit inside a step:

// Verify deployment
sh '''
   export KUBECONFIG=/.../kube-config-dal12-mycluster.yml
   kubectl rollout status deployment/serviceapi --namespace=\"my-namespace\" | tail -1 > deploymentStatus.txt

def deploymentStatus = readFile('deploymentStatus.txt').trim()
echo "Deployment status is: ${deploymentStatus}"
if (deploymentStatus == "deployment \"serviceapi\" successfully rolled out") {
   sh "rm -rf deploymentStatus.txt"

   // Additional logic

else {
   error "Pipeline aborted due to deployment verification failure. Check the Kubernetes dashboard for more information."