pipeline { agent any environment { HARBOR_URL = '192.168.108.200:80' HARBOR_PROJECT = 'vaishnavi-ecommerce' IMAGE_TAG = "${env.BUILD_NUMBER}" K8S_CRED_ID = 'k8s-config' K8S_NAMESPACE = 'ecommerce' K8S_OVERLAY = 'k8s/overlays/on-premise' IMAGE = '192.168.108.200:80/vaishnavi-ecommerce/ecommerce-backend' // SONAR_HOST_URL = 'http://sonarqube.example.com:9000' // ← update this // SONAR_PROJECT = 'ecommerce-backend' } options { buildDiscarder(logRotator(numToKeepStr: '10')) timeout(time: 30, unit: 'MINUTES') disableConcurrentBuilds() } stages { stage('Checkout') { steps { checkout scm echo "Workspace: ${env.WORKSPACE}" sh 'ls -la' } } stage('Install Tools') { steps { sh ''' # Ensure kustomize is available if ! command -v kustomize &>/dev/null; then echo "Installing kustomize..." curl -sL "https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv5.4.3/kustomize_v5.4.3_linux_amd64.tar.gz" \ | tar xz -C /usr/local/bin/ fi kustomize version ''' } } stage('Test') { steps { sh 'npm ci && npm test -- --reporter=verbose 2>&1 || true' } } // ─── SonarQube stages disabled until credentials are resolved ─── // stage('SonarQube Analysis') { // steps { // withSonarQubeEnv('SonarQube') { // sh """ // npx sonar-scanner \\ // -Dsonar.projectKey=${SONAR_PROJECT} \\ // -Dsonar.projectName='eCommerce Backend' \\ // -Dsonar.sources=src \\ // -Dsonar.host.url=${SONAR_HOST_URL} // """ // } // } // } // stage('Quality Gate') { // steps { // timeout(time: 5, unit: 'MINUTES') { // waitForQualityGate abortPipeline: false // } // } // } // ──────────────────────────────────────────────────────────────── stage('Build Image') { steps { sh """ docker build \ -f Dockerfile \ -t ${IMAGE}:${IMAGE_TAG} \ -t ${IMAGE}:latest \ . """ } } stage('Push to Harbor') { steps { withCredentials([usernamePassword( credentialsId: 'harbor-creds', usernameVariable: 'HARBOR_USER', passwordVariable: 'HARBOR_PASS' )]) { sh """ echo \$HARBOR_PASS | docker login ${HARBOR_URL} -u \$HARBOR_USER --password-stdin docker push ${IMAGE}:${IMAGE_TAG} docker push ${IMAGE}:latest """ } } } stage('Patch Image Tag') { steps { dir("${K8S_OVERLAY}") { sh """ kustomize edit set image \ ecommerce-backend=${IMAGE}:${IMAGE_TAG} """ } } } stage('Validate Manifests') { steps { sh """ echo "── Validating Kustomize build ──" kustomize build ${K8S_OVERLAY} echo "✅ Kustomize build succeeded" """ } } stage('Deploy Infrastructure') { steps { withKubeConfig([credentialsId: "${K8S_CRED_ID}"]) { // Apply the full kustomize overlay (namespace, secrets, DBs, redis, app, job) sh "kubectl apply -k ${K8S_OVERLAY}" // Wait for databases to be ready first sh "kubectl rollout status statefulset/postgres -n ${K8S_NAMESPACE} --timeout=300s" sh "kubectl rollout status statefulset/mongodb -n ${K8S_NAMESPACE} --timeout=300s" sh "kubectl rollout status deployment/redis -n ${K8S_NAMESPACE} --timeout=120s" echo "✅ Infrastructure (Postgres, MongoDB, Redis) is ready." } } } stage('Run DB Migrations') { steps { withKubeConfig([credentialsId: "${K8S_CRED_ID}"]) { // Delete previous migration job if it exists (Jobs are immutable) sh "kubectl delete job prisma-migrate -n ${K8S_NAMESPACE} --ignore-not-found=true" // Re-apply to create a fresh migration job sh "kubectl apply -k ${K8S_OVERLAY}" // Wait for the migration job to complete sh """ echo "Waiting for Prisma migration job to complete..." kubectl wait --for=condition=complete \ job/prisma-migrate \ -n ${K8S_NAMESPACE} \ --timeout=120s \ && echo "✅ Prisma migration succeeded" \ || { echo "❌ Migration failed — showing logs:" kubectl logs job/prisma-migrate -n ${K8S_NAMESPACE} --tail=50 exit 1 } """ } } } stage('Deploy Application') { steps { withKubeConfig([credentialsId: "${K8S_CRED_ID}"]) { sh "kubectl rollout status deployment/ecommerce-app -n ${K8S_NAMESPACE} --timeout=300s" echo "✅ Backend deployed successfully." } } } stage('Smoke Test') { steps { withKubeConfig([credentialsId: "${K8S_CRED_ID}"]) { sh """ kubectl run smoke-${BUILD_NUMBER} \ --image=curlimages/curl:latest \ --restart=Never \ --rm \ --attach \ -n ${K8S_NAMESPACE} \ -- curl -sf http://ecommerce-app:80/health \ && echo "✅ Health check PASSED" \ || echo "⚠️ Health check FAILED (non-blocking)" """ } } } stage('Clean Up') { steps { sh """ docker rmi ${IMAGE}:${IMAGE_TAG} || true docker rmi ${IMAGE}:latest || true """ } } } post { success { echo "✅ Build #${env.BUILD_NUMBER} — eCommerce Backend deployed → NodePort 30080" } failure { echo "❌ Pipeline failed. Check stage logs above." } always { sh "docker logout ${HARBOR_URL} || true" } } }