From b091c74f1616f0e5a97422eee7d9bc887d6b53d6 Mon Sep 17 00:00:00 2001 From: subodh Date: Fri, 20 Feb 2026 17:54:06 +0000 Subject: [PATCH] jenkinsfile --- Jenkinsfile | 70 ++++++++--------------------------------------------- 1 file changed, 10 insertions(+), 60 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 3a9137a..7dc032a 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,90 +1,49 @@ pipeline { agent any - environment { - // Harbor LoadBalancer IP and Port - REGISTRY = "192.168.108.200:80" - APP_NAME = "ecommerce-app" + REGISTRY = "192.168.108.200:80" + APP_NAME = "ecommerce-app" IMAGE_NAME = "ecommerce-backend" IMAGE_TAG = "${REGISTRY}/library/${IMAGE_NAME}:${env.BRANCH_NAME}-${env.BUILD_NUMBER}" NAMESPACE = "ecommerce" - - // Containerd and BuildKit configurations CONTAINERD_ADDR = "/run/containerd-pod/containerd.sock" BK_SOCK = "/tmp/buildkitd.sock" } - stages { - stage('Initialize & Build') { + stage('Build & Push') { steps { script { sh """ - # 1. Install missing tools (Self-Healing) + # Install tools if pod is fresh 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 ln -sf /usr/local/bin/buildctl /usr/bin/buildctl fi - - if ! command -v kubectl >/dev/null; then - curl -LO "https://dl.k8s.io/release/\$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" - chmod +x kubectl && mv kubectl /usr/local/bin/ - fi - - # 2. Reset and Start BuildKit pkill buildkitd || true - rm -f ${BK_SOCK} export JENKINS_NODE_COOKIE=dontKillMe nohup buildkitd --addr unix://${BK_SOCK} > /tmp/buildkitd.log 2>&1 & - - # 3. Wait for BuildKit Socket - echo "Waiting for BuildKit..." - for i in \$(seq 1 20); do - if [ -S ${BK_SOCK} ]; then - echo "BuildKit is READY." - break - fi - [ \$i -eq 20 ] && { echo "BuildKit failed to start. Logs:"; cat /tmp/buildkitd.log; exit 1; } - sleep 1 - done - - # 4. Build Image + for i in \$(seq 1 20); do [ -S ${BK_SOCK} ] && break; sleep 1; done + export BUILDKIT_HOST=unix://${BK_SOCK} nerdctl --address ${CONTAINERD_ADDR} build --insecure-registry -t ${IMAGE_TAG} . """ - } - } - } - - stage('Push to Harbor') { - steps { - script { withCredentials([usernamePassword(credentialsId: 'harbor-creds', passwordVariable: 'PASS', usernameVariable: 'USER')]) { - sh """ - echo '${PASS}' | nerdctl --address ${CONTAINERD_ADDR} login ${REGISTRY} -u ${USER} --password-stdin --insecure-registry - nerdctl --address ${CONTAINERD_ADDR} push ${IMAGE_TAG} --insecure-registry - """ + 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" } } } } - - stage('Deploy to Kubernetes') { + stage('Deploy') { steps { script { withCredentials([file(credentialsId: 'k8s-config', variable: 'KUBECONFIG')]) { - // This patch forces K8s to pull from Harbor and uses the pull secret you created sh """ kubectl --kubeconfig=${KUBECONFIG} patch deployment ${APP_NAME} -n ${NAMESPACE} --patch \ '{"spec": {"template": {"spec": { "imagePullSecrets": [{"name": "harbor-pull-secret"}], - "containers": [{ - "name": "${APP_NAME}", - "image": "${IMAGE_TAG}", - "imagePullPolicy": "Always" - }] + "containers": [{"name": "${APP_NAME}", "image": "${IMAGE_TAG}", "imagePullPolicy": "Always"}] }}}}' - - echo "Verifying Rollout..." kubectl --kubeconfig=${KUBECONFIG} rollout status deployment/${APP_NAME} -n ${NAMESPACE} --timeout=90s """ } @@ -92,13 +51,4 @@ pipeline { } } } - - post { - success { - echo "Successfully deployed ${IMAGE_TAG}" - } - failure { - echo "Pipeline failed. Check BuildKit logs or K8s events." - } - } } \ No newline at end of file