This guide enhances a Jenkins pipeline to deploy a Kubernetes application with rollout checks and automatic rollback on failure.
In this guide, we’ll enhance a Jenkins pipeline to deploy a Kubernetes application with proper rollout checks and automatic rollback on failure. By combining vulnerability scanning, dynamic image updates, and robust deployment scripts, you ensure that failed releases don’t leave your cluster in an unhealthy state.
Our initial pipeline applied an updated Deployment manifest (including runAsUser: 100), but we never verified the rollout status. The kubectl apply command succeeded, yet the pods failed to start due to a misconfiguration. We need to:
This script replaces the placeholder image name, then either creates a new Deployment or updates the existing one with --record=true to capture change-cause.
Copy
Ask AI
#!/bin/bash# Replace placeholder with real image namesed -i "s|replace|${imageName}|g" k8s_deployment_service.yaml# Check if deployment existsif ! kubectl -n default get deployment "${deploymentName}" > /dev/null; then echo "Creating deployment ${deploymentName}" kubectl -n default apply -f k8s_deployment_service.yamlelse echo "Updating image for ${deploymentName} to ${imageName}" kubectl -n default set image deploy "${deploymentName}" "${containerName}"="${imageName}" --record=truefi
Script
Purpose
Key Command
k8s-deployment.sh
Create or update the Deployment object
kubectl apply / kubectl set image --record
Ensure the Jenkins service account has get, create, update, and rollout permissions on the target namespace.