pipeline { agent any environment { REGISTRY = "myharbor.local:80" APP_NAME = "ecommerce-app" IMAGE_NAME = "ecommerce-backend" IMAGE_TAG = "${REGISTRY}/library/${IMAGE_NAME}:${env.BRANCH_NAME}-${env.BUILD_NUMBER}" NAMESPACE = "ecommerce" // Explicitly tell the docker wrapper where to find the daemon you just started BUILDKIT_HOST = "unix:///run/buildkit/buildkitd.sock" } stages { stage('Checkout') { steps { checkout scm } } stage('Build Image') { steps { script { // Check if the socket you just created is actually visible to the Jenkins agent sh """ if [ -S /run/buildkit/buildkitd.sock ]; then echo "BuildKit socket found! Proceeding with build..." else echo "BuildKit socket NOT found at /run/buildkit/buildkitd.sock" echo "Listing /run to find correct path:" ls -R /run exit 1 fi """ // Build using the socket sh "docker build -t ${IMAGE_TAG} ." } } } stage('Push to Harbor') { steps { script { withCredentials([usernamePassword(credentialsId: 'harbor-creds', passwordVariable: 'PASS', usernameVariable: 'USER')]) { sh "docker login ${REGISTRY} -u ${USER} -p ${PASS}" sh "docker push ${IMAGE_TAG}" } } } } stage('Inject to K8s Cache') { steps { script { // This is crucial: inject into the k8s namespace sh "docker save ${IMAGE_TAG} | sudo ctr -n k8s.io images import -" } } } stage('Deploy to K8s') { steps { script { withCredentials([file(credentialsId: 'k8s-config', variable: 'KUBECONFIG')]) { sh """ kubectl --kubeconfig=${KUBECONFIG} patch deployment ${APP_NAME} -n ${NAMESPACE} --patch \ '{"spec": {"template": {"spec": {"containers": [{"name": "${APP_NAME}", "image": "${IMAGE_TAG}", "imagePullPolicy": "Never"}]}}}}' """ sh "kubectl --kubeconfig=${KUBECONFIG} rollout status deployment/${APP_NAME} -n ${NAMESPACE}" } } } } } }