diff --git a/Jenkinsfile b/Jenkinsfile index fd5e749..5c8e655 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -2,7 +2,6 @@ pipeline { agent any environment { - // Using the LoadBalancer IP found in your kubectl output REGISTRY = "192.168.108.200" APP_NAME = "ecommerce-app" IMAGE_NAME = "ecommerce-backend" @@ -15,7 +14,7 @@ pipeline { stage('Initialize Agent') { steps { script { - // 1. Install BuildKit if missing + // 1. Install binaries if missing sh """ if ! command -v buildkitd >/dev/null; then 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 @@ -23,14 +22,21 @@ pipeline { ln -sf /usr/local/bin/buildctl /usr/bin/buildctl """ - // 2. Start BuildKit and wait for socket + // 2. Start BuildKit and prevent Jenkins from killing it sh """ mkdir -p /run/buildkit /run/buildkit-default if ! pgrep buildkitd > /dev/null; then + echo "Starting buildkitd..." + # The 'dontKillMe' cookie is the secret sauce here + export JENKINS_NODE_COOKIE=dontKillMe nohup buildkitd --addr unix:///run/buildkit/buildkitd.sock --addr unix:///run/buildkit-default/buildkitd.sock > /tmp/buildkitd.log 2>&1 & - for i in \$(seq 1 10); do - [ -S /run/buildkit/buildkitd.sock ] && break + # Wait for the socket to actually appear + for i in \$(seq 1 15); do + if [ -S /run/buildkit/buildkitd.sock ]; then + echo "Socket found!" + break + fi sleep 2 done fi @@ -42,12 +48,11 @@ pipeline { stage('Build & Push') { steps { script { - // Build with the IP-based tag + // Use the socket path verified in your logs sh "export BUILDKIT_HOST=unix:///run/buildkit/buildkitd.sock && \ nerdctl --address ${CONTAINERD_ADDR} build --insecure-registry -t ${IMAGE_TAG} ." withCredentials([usernamePassword(credentialsId: 'harbor-creds', passwordVariable: 'PASS', usernameVariable: 'USER')]) { - // Login and Push to the LoadBalancer IP 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" } @@ -59,11 +64,7 @@ pipeline { steps { script { withCredentials([file(credentialsId: 'k8s-config', variable: 'KUBECONFIG')]) { - // Update K8s to use the new image - 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}" } }