jenkinsfile
Some checks failed
eCommerce-backend/pipeline/head There was a failure building this commit
Some checks failed
eCommerce-backend/pipeline/head There was a failure building this commit
This commit is contained in:
56
Jenkinsfile
vendored
56
Jenkinsfile
vendored
@@ -12,36 +12,44 @@ pipeline {
|
|||||||
}
|
}
|
||||||
|
|
||||||
stages {
|
stages {
|
||||||
stage('Network Setup') {
|
stage('Initialize Agent & Environment') {
|
||||||
steps {
|
|
||||||
// No sudo needed, agent is running as root
|
|
||||||
sh "echo '${HARBOR_IP} myharbor.local' >> /etc/hosts"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stage('Build Image') {
|
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
// Ensure directories exist for BuildKit
|
// 1. Fix Network (Harbor Resolution)
|
||||||
sh "mkdir -p /run/buildkit && chmod 777 /run/buildkit"
|
sh "grep -q '${HARBOR_IP} myharbor.local' /etc/hosts || echo '${HARBOR_IP} myharbor.local' >> /etc/hosts"
|
||||||
|
|
||||||
// Start buildkitd in background if not running
|
// 2. Install BuildKit Binaries (If missing after pod restart)
|
||||||
sh """
|
sh """
|
||||||
if ! pgrep buildkitd > /dev/null; then
|
if ! command -v buildkitd >/dev/null; then
|
||||||
echo "Starting buildkitd..."
|
echo "Installing BuildKit binaries..."
|
||||||
nohup buildkitd --addr unix:///run/buildkit/buildkitd.sock > /tmp/buildkitd.log 2>&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
|
||||||
sleep 10
|
|
||||||
fi
|
fi
|
||||||
|
# Create the symlink nerdctl expects for 'buildctl'
|
||||||
|
ln -sf /usr/local/bin/buildctl /usr/bin/buildctl
|
||||||
"""
|
"""
|
||||||
|
|
||||||
sh "nerdctl --address ${CONTAINERD_ADDR} build --insecure-registry -t ${IMAGE_TAG} ."
|
// 3. Setup and Start BuildKit Daemon
|
||||||
|
sh """
|
||||||
|
mkdir -p /run/buildkit /run/buildkit-default
|
||||||
|
if ! pgrep buildkitd > /dev/null; then
|
||||||
|
echo "Starting buildkitd..."
|
||||||
|
nohup buildkitd --addr unix:///run/buildkit/buildkitd.sock --addr unix:///run/buildkit-default/buildkitd.sock > /tmp/buildkitd.log 2>&1 &
|
||||||
|
sleep 10
|
||||||
|
fi
|
||||||
|
# Ensure nerdctl finds the socket where it expects
|
||||||
|
ln -sf /run/buildkit/buildkitd.sock /run/buildkit-default/buildkitd.sock
|
||||||
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Push to Harbor') {
|
stage('Build & Push') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
|
// Build
|
||||||
|
sh "nerdctl --address ${CONTAINERD_ADDR} build --insecure-registry -t ${IMAGE_TAG} ."
|
||||||
|
|
||||||
|
// Push
|
||||||
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 "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"
|
||||||
@@ -50,17 +58,29 @@ pipeline {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Deploy') {
|
stage('Deploy to Kubernetes') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
withCredentials([file(credentialsId: 'k8s-config', variable: 'KUBECONFIG')]) {
|
withCredentials([file(credentialsId: 'k8s-config', variable: 'KUBECONFIG')]) {
|
||||||
|
// Patch deployment to use new image and force local use
|
||||||
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": {"containers": [{"name": "${APP_NAME}", "image": "${IMAGE_TAG}", "imagePullPolicy": "Never"}]}}}}'
|
'{"spec": {"template": {"spec": {"containers": [{"name": "${APP_NAME}", "image": "${IMAGE_TAG}", "imagePullPolicy": "Never"}]}}}}'
|
||||||
"""
|
"""
|
||||||
|
sh "kubectl --kubeconfig=${KUBECONFIG} rollout status deployment/${APP_NAME} -n ${NAMESPACE}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
post {
|
||||||
|
success {
|
||||||
|
echo "Successfully deployed Version: ${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
|
||||||
|
}
|
||||||
|
always {
|
||||||
|
// Clean up to keep agent node storage healthy
|
||||||
|
sh "nerdctl --address ${CONTAINERD_ADDR} rmi ${IMAGE_TAG} || true"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user