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

58
Jenkinsfile vendored
View File

@ -2,48 +2,54 @@ pipeline {
agent any
environment {
DOCKERHUB_REPO = "vipin2025devops/myapp"
IMAGE_TAG = "${env.BUILD_NUMBER}"
DOCKERHUB_USER = 'vipin2025devops'
IMAGE_NAME = 'myapp'
IMAGE_TAG = "latest"
K8S_DEPLOYMENT = 'k8s/deployment.yaml'
K8S_SERVICE = 'k8s/service.yaml'
}
stages {
stage('Checkout') {
stage('Checkout Code') {
steps {
checkout scm
}
}
stage('Build & Push Image') {
stage('Build Docker Image') {
steps {
script {
docker.withRegistry('https://index.docker.io/v1/', 'dockerhub-creds') {
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"
}
powershell """
docker build -t ${DOCKERHUB_USER}/${IMAGE_NAME}:${IMAGE_TAG} .
"""
}
}
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') {
steps {
withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) {
script {
bat """
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
"""
}
}
powershell """
kubectl apply -f ${K8S_DEPLOYMENT}
kubectl apply -f ${K8S_SERVICE}
"""
}
}
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.