diff --git a/Jenkinsfile b/Jenkinsfile index 01b0709..c8ec998 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -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}" - } - } } diff --git a/kubeconfig.yaml b/kubeconfig.yaml new file mode 100644 index 0000000..fe19f6d Binary files /dev/null and b/kubeconfig.yaml differ