-
After adding the plugin in your Jenkins script, click on Generate pipeline syntax.
-
Next, select secret file from the dropdown and provide the path of your kube config. Usually, you will find your kubeconfig file from the below path
Once you execute the select command, then it will appear content like below
Just upload this file in the credential section like this

-
Next, once you complete this, it will generate syntax for you
, copy this and use it in your script. -
Use the below script for Windows:
pipeline { agent any tools { // Define Maven tool maven "maven" } environment { // Define environment variables APP_NAME = "user-service" RELEASE_NO = "1.0.0" DOCKER_USER = "javatechie4u" IMAGE_NAME = "${DOCKER_USER}/${APP_NAME}" IMAGE_TAG = "${RELEASE_NO}-${BUILD_NUMBER}" } stages { stage("SCM checkout") { steps { // Checkout code from Git checkout([$class: 'GitSCM', branches: [[name: '*/main']], userRemoteConfigs: [[url: 'https://github.com/javatechie-devops/user-service.git']]]) } } stage("Build Process") { steps { // Build the project using Maven bat 'mvn clean install' } } stage("Build Image") { steps { // Build Docker image bat "docker build -t ${IMAGE_NAME}:${IMAGE_TAG} ." } } stage("Deploy Image to Hub") { steps { // Login to Docker Hub withCredentials([string(credentialsId: 'dpwd', variable: 'dpwd')]) { bat "docker login -u ${DOCKER_USER} -p ${dpwd}" bat "docker push ${IMAGE_NAME}:${IMAGE_TAG}" } } } stage("Deploy to Kubernetes") { steps { script { kubeconfig(credentialsId: 'kubeconfig', serverUrl: '') { bat """powershell -Command "(Get-Content k8s-app.yaml) -replace 'image: .*', 'image: ${IMAGE_NAME}:${IMAGE_TAG}' | Set-Content k8s-app.yaml" """ bat 'kubectl apply -f k8s-app.yaml' } } } } stage("Verify deployment") { steps { script { kubeconfig(credentialsId: 'kubeconfig', serverUrl: '') { // Check deployed pods bat 'kubectl get pods -n user-app' } } } } } } -
Build your job then you could see successful results in the pipeline




