jenkinsfile
Some checks failed
eCommerce-backend/pipeline/head There was a failure building this commit

This commit is contained in:
2026-02-20 17:26:23 +00:00
parent a7d3975db5
commit 8d83d359fc

69
Jenkinsfile vendored
View File

@@ -2,67 +2,77 @@ pipeline {
agent any agent any
environment { environment {
// Harbor LoadBalancer IP and Port
REGISTRY = "192.168.108.200:80" REGISTRY = "192.168.108.200:80"
APP_NAME = "ecommerce-app" APP_NAME = "ecommerce-app"
IMAGE_NAME = "ecommerce-backend" IMAGE_NAME = "ecommerce-backend"
IMAGE_TAG = "${REGISTRY}/library/${IMAGE_NAME}:${env.BRANCH_NAME}-${env.BUILD_NUMBER}" IMAGE_TAG = "${REGISTRY}/library/${IMAGE_NAME}:${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
NAMESPACE = "ecommerce" NAMESPACE = "ecommerce"
// Containerd and BuildKit configurations
CONTAINERD_ADDR = "/run/containerd-pod/containerd.sock" CONTAINERD_ADDR = "/run/containerd-pod/containerd.sock"
BK_SOCK = "/tmp/buildkitd.sock"
} }
stages { stages {
stage('Initialize Agent Tools') { stage('Initialize & Build') {
steps { steps {
script { script {
sh """ sh """
# 1. Install BuildKit if missing # 1. Install missing tools (Self-Healing)
if ! command -v buildkitd >/dev/null; then 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 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 ln -sf /usr/local/bin/buildctl /usr/bin/buildctl
fi fi
# 2. Install kubectl if missing (The cause of error 127)
if ! command -v kubectl >/dev/null; then if ! command -v kubectl >/dev/null; then
echo "Installing kubectl..."
curl -LO "https://dl.k8s.io/release/\$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" curl -LO "https://dl.k8s.io/release/\$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl chmod +x kubectl && mv kubectl /usr/local/bin/
mv kubectl /usr/local/bin/
fi fi
# 3. Start BuildKit # 2. Reset and Start BuildKit
mkdir -p /run/buildkit /run/buildkit-default pkill buildkitd || true
rm -f ${BK_SOCK}
export JENKINS_NODE_COOKIE=dontKillMe export JENKINS_NODE_COOKIE=dontKillMe
if ! pgrep buildkitd > /dev/null; then nohup buildkitd --addr unix://${BK_SOCK} > /tmp/buildkitd.log 2>&1 &
nohup buildkitd --addr unix:///run/buildkit/buildkitd.sock --addr unix:///run/buildkit-default/buildkitd.sock > /tmp/buildkitd.log 2>&1 &
sleep 10 # 3. Wait for BuildKit Socket
fi 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
export BUILDKIT_HOST=unix://${BK_SOCK}
nerdctl --address ${CONTAINERD_ADDR} build --insecure-registry -t ${IMAGE_TAG} .
""" """
} }
} }
} }
stage('Build & Push') { stage('Push to Harbor') {
steps { steps {
script { script {
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')]) { withCredentials([usernamePassword(credentialsId: 'harbor-creds', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
sh "echo '${PASS}' | nerdctl --address ${CONTAINERD_ADDR} login ${REGISTRY} -u ${USER} --password-stdin --insecure-registry" sh """
sh "nerdctl --address ${CONTAINERD_ADDR} push ${IMAGE_TAG} --insecure-registry" echo '${PASS}' | nerdctl --address ${CONTAINERD_ADDR} login ${REGISTRY} -u ${USER} --password-stdin --insecure-registry
nerdctl --address ${CONTAINERD_ADDR} push ${IMAGE_TAG} --insecure-registry
"""
} }
} }
} }
} }
stage('Deploy to K8s') { stage('Deploy to Kubernetes') {
steps { steps {
script { script {
withCredentials([file(credentialsId: 'k8s-config', variable: 'KUBECONFIG')]) { withCredentials([file(credentialsId: 'k8s-config', variable: 'KUBECONFIG')]) {
// 1. Change imagePullPolicy to 'Always' // This patch forces K8s to pull from Harbor and uses the pull secret you created
// 2. Ensure imagePullSecrets is added so K8s can login to Harbor
sh """ sh """
kubectl --kubeconfig=${KUBECONFIG} patch deployment ${APP_NAME} -n ${NAMESPACE} --patch \ kubectl --kubeconfig=${KUBECONFIG} patch deployment ${APP_NAME} -n ${NAMESPACE} --patch \
'{"spec": {"template": {"spec": { '{"spec": {"template": {"spec": {
@@ -73,11 +83,22 @@ pipeline {
"imagePullPolicy": "Always" "imagePullPolicy": "Always"
}] }]
}}}}' }}}}'
echo "Verifying Rollout..."
kubectl --kubeconfig=${KUBECONFIG} rollout status deployment/${APP_NAME} -n ${NAMESPACE} --timeout=90s
""" """
sh "kubectl --kubeconfig=${KUBECONFIG} rollout status deployment/${APP_NAME} -n ${NAMESPACE}"
} }
} }
} }
} }
} }
post {
success {
echo "Successfully deployed ${IMAGE_TAG}"
}
failure {
echo "Pipeline failed. Check BuildKit logs or K8s events."
}
}
} }