Some checks failed
eCommerce-backend/pipeline/head There was a failure building this commit
87 lines
3.1 KiB
Groovy
87 lines
3.1 KiB
Groovy
pipeline {
|
|
agent any
|
|
|
|
environment {
|
|
// Automatically detect registry and image name based on repo
|
|
REGISTRY = "myharbor.local:80"
|
|
|
|
// This MUST match your 'kubectl get deployments' name exactly
|
|
// Based on your previous logs, your backend deployment is 'ecommerce-app'
|
|
APP_NAME = "ecommerce-app"
|
|
|
|
// The image name for Harbor
|
|
IMAGE_NAME = "ecommerce-backend"
|
|
|
|
IMAGE_TAG = "${REGISTRY}/library/${IMAGE_NAME}:${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
|
|
NAMESPACE = "ecommerce"
|
|
}
|
|
|
|
stages {
|
|
stage('Checkout') {
|
|
steps {
|
|
// Ensure we have a clean copy of the code
|
|
checkout scm
|
|
}
|
|
}
|
|
|
|
stage('Build Image') {
|
|
steps {
|
|
script {
|
|
// Using --pull and forcing the builder often bypasses BuildKit hang-ups
|
|
sh "docker build --pull --no-cache -t ${IMAGE_TAG} ."
|
|
sh "docker tag ${IMAGE_TAG} ${IMAGE_NAME}:latest"
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Push to Harbor') {
|
|
steps {
|
|
script {
|
|
withCredentials([usernamePassword(credentialsId: 'harbor-creds', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
|
|
sh "docker login ${REGISTRY} -u ${USER} -p ${PASS}"
|
|
sh "docker push ${IMAGE_TAG}"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Inject to K8s Cache') {
|
|
steps {
|
|
script {
|
|
// This moves the image from Docker to Containerd so K8s can see it locally
|
|
sh "docker save ${IMAGE_TAG} | sudo ctr -n k8s.io images import -"
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Deploy to K8s') {
|
|
steps {
|
|
script {
|
|
withCredentials([file(credentialsId: 'k8s-config', variable: 'KUBECONFIG')]) {
|
|
// Patching the existing deployment with the new build-specific image tag
|
|
// We use 'imagePullPolicy: Never' to force K8s to use the injected local 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}"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
post {
|
|
success {
|
|
echo "Successfully deployed ${IMAGE_TAG} to ${NAMESPACE} namespace."
|
|
}
|
|
failure {
|
|
echo "Build or Deployment failed. Check Docker/BuildKit status on the agent."
|
|
}
|
|
always {
|
|
// Clean up to keep the agent's disk from filling up
|
|
sh "docker rmi ${IMAGE_TAG} || true"
|
|
}
|
|
}
|
|
} |