diff --git a/Jenkinsfile b/Jenkinsfile index 0e85105..3b1c74c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -2,8 +2,10 @@ pipeline { agent any environment { - REGISTRY = "myharbor.local:80" - HARBOR_IP = "192.168.108.101" + // INTERNAL K8S ADDRESS: ..svc.cluster.local + // Assuming your harbor service is named 'harbor' in namespace 'harbor' + REGISTRY = "harbor.harbor.svc.cluster.local" + APP_NAME = "ecommerce-app" IMAGE_NAME = "ecommerce-backend" IMAGE_TAG = "${REGISTRY}/library/${IMAGE_NAME}:${env.BRANCH_NAME}-${env.BUILD_NUMBER}" @@ -12,32 +14,24 @@ pipeline { } stages { - stage('Initialize Agent & Environment') { + stage('Initialize Environment') { steps { script { - // 1. Fix Network (Harbor Resolution) - sh "grep -q '${HARBOR_IP} myharbor.local' /etc/hosts || echo '${HARBOR_IP} myharbor.local' >> /etc/hosts" - - // 2. Install BuildKit Binaries (If missing after pod restart) + // Install BuildKit binaries if missing sh """ if ! command -v buildkitd >/dev/null; then - echo "Installing BuildKit binaries..." curl -L https://github.com/moby/buildkit/releases/download/v0.12.5/buildkit-v0.12.5.linux-amd64.tar.gz | tar -xz -C /usr/local/bin/ --strip-components=1 fi - # Create the symlink nerdctl expects for 'buildctl' ln -sf /usr/local/bin/buildctl /usr/bin/buildctl """ - // 3. Setup and Start BuildKit Daemon + // Start buildkitd sh """ mkdir -p /run/buildkit /run/buildkit-default if ! pgrep buildkitd > /dev/null; then - echo "Starting buildkitd..." nohup buildkitd --addr unix:///run/buildkit/buildkitd.sock --addr unix:///run/buildkit-default/buildkitd.sock > /tmp/buildkitd.log 2>&1 & sleep 10 fi - # Ensure nerdctl finds the socket where it expects - ln -sf /run/buildkit/buildkitd.sock /run/buildkit-default/buildkitd.sock """ } } @@ -46,11 +40,11 @@ pipeline { stage('Build & Push') { steps { script { - // Build + // Build using internal registry name sh "nerdctl --address ${CONTAINERD_ADDR} build --insecure-registry -t ${IMAGE_TAG} ." - // Push withCredentials([usernamePassword(credentialsId: 'harbor-creds', passwordVariable: 'PASS', usernameVariable: 'USER')]) { + // Login and Push using internal K8s DNS sh "echo '${PASS}' | nerdctl --address ${CONTAINERD_ADDR} login ${REGISTRY} -u ${USER} --password-stdin --insecure-registry" sh "nerdctl --address ${CONTAINERD_ADDR} push ${IMAGE_TAG} --insecure-registry" } @@ -58,29 +52,15 @@ pipeline { } } - stage('Deploy to Kubernetes') { + stage('Deploy') { steps { script { withCredentials([file(credentialsId: 'k8s-config', variable: 'KUBECONFIG')]) { - // Patch deployment to use new image and force local use - 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} 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}" } } } } } - - post { - success { - echo "Successfully deployed Version: ${env.BRANCH_NAME}-${env.BUILD_NUMBER}" - } - always { - // Clean up to keep agent node storage healthy - sh "nerdctl --address ${CONTAINERD_ADDR} rmi ${IMAGE_TAG} || true" - } - } } \ No newline at end of file