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

This commit is contained in:
2026-02-20 16:12:22 +00:00
parent ef641f5edf
commit 527d6dbbf2

45
Jenkinsfile vendored
View File

@@ -3,6 +3,7 @@ pipeline {
environment { environment {
REGISTRY = "myharbor.local:80" REGISTRY = "myharbor.local:80"
HARBOR_IP = "192.168.108.101" // Your Master Node IP
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}"
@@ -11,37 +12,22 @@ pipeline {
} }
stages { stages {
stage('Checkout') { stage('Network Setup') {
steps { steps {
checkout scm // Manually inject the host entry for this build session
sh "echo '${HARBOR_IP} myharbor.local' | sudo tee -a /etc/hosts"
} }
} }
stage('Build Image') { stage('Build Image') {
steps { steps {
script { script {
// Force nerdctl to use the socket by creating the directory it expects // Start buildkitd if it died during pod rotation
sh """ sh "mkdir -p /run/buildkit && sudo chmod 777 /run/buildkit"
mkdir -p /run/buildkit-default sh "nohup buildkitd --addr unix:///run/buildkit/buildkitd.sock > /tmp/buildkitd.log 2>&1 &"
mkdir -p /run/buildkit sleep 10
# Search for the buildkit socket in the entire /run directory sh "nerdctl --address ${CONTAINERD_ADDR} build --insecure-registry -t ${IMAGE_TAG} ."
# and symlink it to the default location nerdctl wants
BK_SOCKET=\$(find /run -name "buildkitd.sock" | head -n 1)
if [ -n "\$BK_SOCKET" ]; then
echo "Found BuildKit socket at \$BK_SOCKET. Linking..."
ln -sf \$BK_SOCKET /run/buildkit/buildkitd.sock
ln -sf \$BK_SOCKET /run/buildkit-default/buildkitd.sock
else
echo "BuildKit socket not found. Attempting to start it locally..."
nohup buildkitd --addr unix:///run/buildkit/buildkitd.sock > /tmp/buildkitd.log 2>&1 &
sleep 10
fi
# Run the build
nerdctl --address ${CONTAINERD_ADDR} build --insecure-registry -t ${IMAGE_TAG} .
"""
} }
} }
} }
@@ -50,24 +36,19 @@ pipeline {
steps { steps {
script { script {
withCredentials([usernamePassword(credentialsId: 'harbor-creds', passwordVariable: 'PASS', usernameVariable: 'USER')]) { withCredentials([usernamePassword(credentialsId: 'harbor-creds', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
sh "nerdctl --address ${CONTAINERD_ADDR} login ${REGISTRY} -u ${USER} -p ${PASS} --insecure-registry" // Using --password-stdin to remove the "insecure" warning
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" sh "nerdctl --address ${CONTAINERD_ADDR} push ${IMAGE_TAG} --insecure-registry"
} }
} }
} }
} }
stage('Deploy to K8s') { stage('Deploy') {
steps { steps {
script { script {
withCredentials([file(credentialsId: 'k8s-config', variable: 'KUBECONFIG')]) { withCredentials([file(credentialsId: 'k8s-config', variable: 'KUBECONFIG')]) {
// Using 'Never' because nerdctl builds directly into the containerd store sh "kubectl --kubeconfig=${KUBECONFIG} patch deployment ${APP_NAME} -n ${NAMESPACE} --patch '{\"spec\": {\"template\": {\"spec\": {\"containers\": [{\"name\": \"${APP_NAME}\", \"image\": \"${IMAGE_TAG}\", \"imagePullPolicy\": \"Never\"}]}}}}'"
// shared by the node.
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}"
} }
} }
} }