Updated Jenkinsfile for Kubernetes deployment

This commit is contained in:
VIPIN 2025-09-12 09:26:28 +05:30
parent e0d2e013ea
commit d08eeea7ae
2 changed files with 32 additions and 26 deletions

54
Jenkinsfile vendored
View File

@ -2,48 +2,54 @@ pipeline {
agent any agent any
environment { environment {
DOCKERHUB_REPO = "vipin2025devops/myapp" DOCKERHUB_USER = 'vipin2025devops'
IMAGE_TAG = "${env.BUILD_NUMBER}" IMAGE_NAME = 'myapp'
IMAGE_TAG = "latest"
K8S_DEPLOYMENT = 'k8s/deployment.yaml'
K8S_SERVICE = 'k8s/service.yaml'
} }
stages { stages {
stage('Checkout') { stage('Checkout Code') {
steps { steps {
checkout scm checkout scm
} }
} }
stage('Build & Push Image') { stage('Build Docker Image') {
steps { steps {
script { powershell """
docker.withRegistry('https://index.docker.io/v1/', 'dockerhub-creds') { docker build -t ${DOCKERHUB_USER}/${IMAGE_NAME}:${IMAGE_TAG} .
def img = docker.build("${DOCKERHUB_REPO}:${IMAGE_TAG}") """
img.push()
// cleanup local image to save space
bat "docker image rm ${DOCKERHUB_REPO}:${IMAGE_TAG} || exit 0"
} }
} }
stage('Push Image to Docker Hub') {
steps {
withCredentials([usernamePassword(credentialsId: 'docker-hub-credentials', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
powershell """
echo $env:DOCKER_PASS | docker login -u $env:DOCKER_USER --password-stdin
docker push ${DOCKERHUB_USER}/${IMAGE_NAME}:${IMAGE_TAG}
"""
}
} }
} }
stage('Deploy to Kubernetes') { stage('Deploy to Kubernetes') {
steps { steps {
withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) { powershell """
script { kubectl apply -f ${K8S_DEPLOYMENT}
bat """ kubectl apply -f ${K8S_SERVICE}
powershell -Command "(Get-Content k8s/deployment.yaml) -replace 'IMAGE_PLACEHOLDER', '${DOCKERHUB_REPO}:${IMAGE_TAG}' | Set-Content k8s/deployment-to-apply.yaml" """
kubectl --kubeconfig=%KUBECONFIG% apply -f k8s/deployment-to-apply.yaml }
kubectl --kubeconfig=%KUBECONFIG% apply -f k8s/service.yaml }
stage('Verify Deployment') {
steps {
powershell """
kubectl get pods -o wide
""" """
} }
} }
} }
}
}
post {
always {
echo "Pipeline finished: ${currentBuild.fullDisplayName}"
}
}
} }

BIN
kubeconfig.yaml Normal file

Binary file not shown.