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:
31
Jenkinsfile
vendored
31
Jenkinsfile
vendored
@@ -2,10 +2,8 @@ pipeline {
|
|||||||
agent any
|
agent any
|
||||||
|
|
||||||
environment {
|
environment {
|
||||||
// INTERNAL K8S ADDRESS: <service-name>.<namespace>.svc.cluster.local
|
// Using the LoadBalancer IP found in your kubectl output
|
||||||
// Assuming your harbor service is named 'harbor' in namespace 'harbor'
|
REGISTRY = "192.168.108.200"
|
||||||
REGISTRY = "harbor.harbor.svc.cluster.local"
|
|
||||||
|
|
||||||
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}"
|
||||||
@@ -14,10 +12,10 @@ pipeline {
|
|||||||
}
|
}
|
||||||
|
|
||||||
stages {
|
stages {
|
||||||
stage('Initialize Environment') {
|
stage('Initialize Agent') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
// Install BuildKit binaries if missing
|
// 1. Install BuildKit if missing
|
||||||
sh """
|
sh """
|
||||||
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
|
||||||
@@ -25,12 +23,16 @@ pipeline {
|
|||||||
ln -sf /usr/local/bin/buildctl /usr/bin/buildctl
|
ln -sf /usr/local/bin/buildctl /usr/bin/buildctl
|
||||||
"""
|
"""
|
||||||
|
|
||||||
// Start buildkitd
|
// 2. Start BuildKit and wait for socket
|
||||||
sh """
|
sh """
|
||||||
mkdir -p /run/buildkit /run/buildkit-default
|
mkdir -p /run/buildkit /run/buildkit-default
|
||||||
if ! pgrep buildkitd > /dev/null; then
|
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 &
|
nohup buildkitd --addr unix:///run/buildkit/buildkitd.sock --addr unix:///run/buildkit-default/buildkitd.sock > /tmp/buildkitd.log 2>&1 &
|
||||||
sleep 10
|
|
||||||
|
for i in \$(seq 1 10); do
|
||||||
|
[ -S /run/buildkit/buildkitd.sock ] && break
|
||||||
|
sleep 2
|
||||||
|
done
|
||||||
fi
|
fi
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
@@ -40,11 +42,12 @@ pipeline {
|
|||||||
stage('Build & Push') {
|
stage('Build & Push') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
// Build using internal registry name
|
// Build with the IP-based tag
|
||||||
sh "nerdctl --address ${CONTAINERD_ADDR} build --insecure-registry -t ${IMAGE_TAG} ."
|
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')]) {
|
||||||
// Login and Push using internal K8s DNS
|
// Login and Push to the LoadBalancer IP
|
||||||
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"
|
||||||
}
|
}
|
||||||
@@ -56,7 +59,11 @@ pipeline {
|
|||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
withCredentials([file(credentialsId: 'k8s-config', variable: 'KUBECONFIG')]) {
|
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\"}]}}}}'"
|
// Update K8s to use the new image
|
||||||
|
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}"
|
sh "kubectl --kubeconfig=${KUBECONFIG} rollout status deployment/${APP_NAME} -n ${NAMESPACE}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user