Some checks failed
eCommerce-backend/pipeline/head There was a failure building this commit
66 lines
2.5 KiB
Groovy
66 lines
2.5 KiB
Groovy
pipeline {
|
|
agent any
|
|
|
|
environment {
|
|
REGISTRY = "myharbor.local:80"
|
|
HARBOR_IP = "192.168.108.101"
|
|
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('Network Setup') {
|
|
steps {
|
|
// No sudo needed, agent is running as root
|
|
sh "echo '${HARBOR_IP} myharbor.local' >> /etc/hosts"
|
|
}
|
|
}
|
|
|
|
stage('Build Image') {
|
|
steps {
|
|
script {
|
|
// Ensure directories exist for BuildKit
|
|
sh "mkdir -p /run/buildkit && chmod 777 /run/buildkit"
|
|
|
|
// Start buildkitd in background if not running
|
|
sh """
|
|
if ! pgrep buildkitd > /dev/null; then
|
|
echo "Starting buildkitd..."
|
|
nohup buildkitd --addr unix:///run/buildkit/buildkitd.sock > /tmp/buildkitd.log 2>&1 &
|
|
sleep 10
|
|
fi
|
|
"""
|
|
|
|
sh "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"
|
|
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"}]}}}}'
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |