Some checks failed
eCommerce-backend/pipeline/head There was a failure building this commit
66 lines
2.9 KiB
Groovy
66 lines
2.9 KiB
Groovy
pipeline {
|
|
agent any
|
|
|
|
environment {
|
|
// INTERNAL K8S ADDRESS: <service-name>.<namespace>.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}"
|
|
NAMESPACE = "ecommerce"
|
|
CONTAINERD_ADDR = "/run/containerd-pod/containerd.sock"
|
|
}
|
|
|
|
stages {
|
|
stage('Initialize Environment') {
|
|
steps {
|
|
script {
|
|
// Install BuildKit 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
|
|
fi
|
|
ln -sf /usr/local/bin/buildctl /usr/bin/buildctl
|
|
"""
|
|
|
|
// Start buildkitd
|
|
sh """
|
|
mkdir -p /run/buildkit /run/buildkit-default
|
|
if ! pgrep buildkitd > /dev/null; then
|
|
nohup buildkitd --addr unix:///run/buildkit/buildkitd.sock --addr unix:///run/buildkit-default/buildkitd.sock > /tmp/buildkitd.log 2>&1 &
|
|
sleep 10
|
|
fi
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Build & Push') {
|
|
steps {
|
|
script {
|
|
// Build using internal registry name
|
|
sh "nerdctl --address ${CONTAINERD_ADDR} build --insecure-registry -t ${IMAGE_TAG} ."
|
|
|
|
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"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Deploy') {
|
|
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}"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |