This commit is contained in:
35
Jenkinsfile
vendored
35
Jenkinsfile
vendored
@@ -4,14 +4,22 @@ pipeline {
|
|||||||
environment {
|
environment {
|
||||||
// Automatically detect registry and image name based on repo
|
// Automatically detect registry and image name based on repo
|
||||||
REGISTRY = "myharbor.local:80"
|
REGISTRY = "myharbor.local:80"
|
||||||
APP_NAME = "eCommerce-backend" // Change this per repo (backend, web, admin)
|
|
||||||
IMAGE_TAG = "${REGISTRY}/library/${APP_NAME}:${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
|
// 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"
|
NAMESPACE = "ecommerce"
|
||||||
}
|
}
|
||||||
|
|
||||||
stages {
|
stages {
|
||||||
stage('Checkout') {
|
stage('Checkout') {
|
||||||
steps {
|
steps {
|
||||||
|
// Ensure we have a clean copy of the code
|
||||||
checkout scm
|
checkout scm
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -19,9 +27,9 @@ pipeline {
|
|||||||
stage('Build Image') {
|
stage('Build Image') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
// DOCKER_BUILDKIT=0 forces the use of the legacy builder
|
// Using --pull and forcing the builder often bypasses BuildKit hang-ups
|
||||||
sh "DOCKER_BUILDKIT=0 docker build -t ${IMAGE_TAG} ."
|
sh "docker build --pull --no-cache -t ${IMAGE_TAG} ."
|
||||||
sh "DOCKER_BUILDKIT=0 docker tag ${IMAGE_TAG} ${APP_NAME}:latest"
|
sh "docker tag ${IMAGE_TAG} ${IMAGE_NAME}:latest"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,7 +37,6 @@ pipeline {
|
|||||||
stage('Push to Harbor') {
|
stage('Push to Harbor') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
// Log in and push (uses the port 80 bypass we set up)
|
|
||||||
withCredentials([usernamePassword(credentialsId: 'harbor-creds', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
|
withCredentials([usernamePassword(credentialsId: 'harbor-creds', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
|
||||||
sh "docker login ${REGISTRY} -u ${USER} -p ${PASS}"
|
sh "docker login ${REGISTRY} -u ${USER} -p ${PASS}"
|
||||||
sh "docker push ${IMAGE_TAG}"
|
sh "docker push ${IMAGE_TAG}"
|
||||||
@@ -41,8 +48,7 @@ pipeline {
|
|||||||
stage('Inject to K8s Cache') {
|
stage('Inject to K8s Cache') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
// Since we are bypassing Harbor pull in K8s for now:
|
// This moves the image from Docker to Containerd so K8s can see it locally
|
||||||
// Export from Docker and Import to Containerd k8s namespace
|
|
||||||
sh "docker save ${IMAGE_TAG} | sudo ctr -n k8s.io images import -"
|
sh "docker save ${IMAGE_TAG} | sudo ctr -n k8s.io images import -"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -52,14 +58,13 @@ pipeline {
|
|||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
withCredentials([file(credentialsId: 'k8s-config', variable: 'KUBECONFIG')]) {
|
withCredentials([file(credentialsId: 'k8s-config', variable: 'KUBECONFIG')]) {
|
||||||
// Update the deployment image
|
// Patching the existing deployment with the new build-specific image tag
|
||||||
// We use --patch to update the image without needing the full YAML
|
// We use 'imagePullPolicy: Never' to force K8s to use the injected local image
|
||||||
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"}]}}}}'
|
||||||
"""
|
"""
|
||||||
|
|
||||||
// Verify deployment
|
|
||||||
sh "kubectl --kubeconfig=${KUBECONFIG} rollout status deployment/${APP_NAME} -n ${NAMESPACE}"
|
sh "kubectl --kubeconfig=${KUBECONFIG} rollout status deployment/${APP_NAME} -n ${NAMESPACE}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -68,8 +73,14 @@ pipeline {
|
|||||||
}
|
}
|
||||||
|
|
||||||
post {
|
post {
|
||||||
|
success {
|
||||||
|
echo "Successfully deployed ${IMAGE_TAG} to ${NAMESPACE} namespace."
|
||||||
|
}
|
||||||
|
failure {
|
||||||
|
echo "Build or Deployment failed. Check Docker/BuildKit status on the agent."
|
||||||
|
}
|
||||||
always {
|
always {
|
||||||
// Clean up old images to save disk space
|
// Clean up to keep the agent's disk from filling up
|
||||||
sh "docker rmi ${IMAGE_TAG} || true"
|
sh "docker rmi ${IMAGE_TAG} || true"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user