diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..6b40b5b --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,77 @@ +pipeline { + agent any + + environment { + // Automatically detect registry and image name based on repo + 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}" + NAMESPACE = "ecommerce" + } + + stages { + stage('Checkout') { + steps { + checkout scm + } + } + + stage('Build Image') { + steps { + script { + // Build the Docker image locally + sh "docker build -t ${IMAGE_TAG} ." + // Also tag as latest for easy local reference + sh "docker tag ${IMAGE_TAG} ${APP_NAME}:latest" + } + } + } + + stage('Push to Harbor') { + steps { + script { + // Log in and push (uses the port 80 bypass we set up) + 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 { + // Since we are bypassing Harbor pull in K8s for now: + // Export from Docker and Import to Containerd k8s namespace + 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')]) { + // Update the deployment image + // We use --patch to update the image without needing the full YAML + sh """ + kubectl --kubeconfig=${KUBECONFIG} patch deployment ${APP_NAME} -n ${NAMESPACE} --patch \ + '{"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}" + } + } + } + } + } + + post { + always { + // Clean up old images to save disk space + sh "docker rmi ${IMAGE_TAG} || true" + } + } +} \ No newline at end of file