Update insurance/Jenkinsfile.InsuranceBe

This commit is contained in:
2025-08-26 11:56:32 +08:00
parent 1b2b40fd9d
commit d863fe6fcd

View File

@@ -6,7 +6,7 @@ pipeline {
KUBECONFIG_CREDENTIALS = "k3s-kubeconfig"
GITEA_CREDENTIALS = "gitea"
K8S_NAMESPACE = "insurance-system"
DEPLOYMENT_NAME = "insurance-backend"
DEPLOYMENT_NAME = "insurance-be"
}
stages {
@@ -25,38 +25,40 @@ pipeline {
stage('Increase Version') {
steps {
script {
// Get current version
// Get current version for logging (optional)
def currentVersion = sh(script: "mvn help:evaluate -Dexpression=project.version -q -DforceStdout || echo 'Failed to get version'", returnStdout: true).trim()
if (currentVersion == 'Failed to get version' || !currentVersion) {
error "Unable to retrieve current version from pom.xml."
error "Unable to retrieve current version from pom.xml. Ensure Maven is installed and pom.xml exists."
}
echo "Current version: '${currentVersion}'"
echo "Current version raw output: '${currentVersion}'" // Debug output
// Increment version using Maven Versions Plugin
// Use build-helper and versions plugins to increment the version
sh """
mvn build-helper:parse-version versions:set \
-DnewVersion='\${parsedVersion.majorVersion}.\${parsedVersion.minorVersion}.\${parsedVersion.nextIncrementalVersion}-SNAPSHOT' \
versions:commit || exit 1
"""
// Get the new version after update
// Get the new version after update for downstream use
def newVersion = sh(script: "mvn help:evaluate -Dexpression=project.version -q -DforceStdout", returnStdout: true).trim()
// Remove ANSI escape codes
newVersion = newVersion.replaceAll(/\u001B\\[[;\\d]*m/, '')
echo "New version: '${newVersion}'"
// Remove ANSI escape codes
newVersion = newVersion.replaceAll(/\u001B\[.*?m/, '')
echo "New version calculated: '${newVersion}'" // Debug output
// Validate
// Validate newVersion
if (!newVersion || newVersion.contains("null")) {
error "Invalid new version: '${newVersion}'. Check Maven configuration."
error "Invalid new version: '${newVersion}'. Check Maven version update."
}
// Commit and push updated pom.xml
echo "Updated version from ${currentVersion} to ${newVersion}"
// Commit and push the updated pom.xml
withCredentials([usernamePassword(credentialsId: env.GITEA_CREDENTIALS, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
sh """
git config user.email "jenkins@yourdomain.com"
git config user.name "Jenkins"
git add pom.xml
git commit -m "Auto-increment version to ${newVersion}" || echo "No changes"
git commit -m "Auto-increment version to ${newVersion}" || echo "No changes to commit"
git push https://${GIT_USER}:${GIT_PASS}@git.hzwnrw.my/Insurance/insurance-be.git HEAD:main || exit 1
"""
}
@@ -69,7 +71,9 @@ pipeline {
stage('Build & Push Docker Image') {
steps {
script {
// Build the JAR file
sh "mvn clean package -DskipTests || exit 1"
def dockerImage = "hzwnrw/dev:insurance-backend-${env.NEW_VERSION}"
echo "Building Docker image: ${dockerImage}"
sh "docker build -t ${dockerImage} . || exit 1"
@@ -88,10 +92,12 @@ pipeline {
steps {
script {
withCredentials([file(credentialsId: env.KUBECONFIG_CREDENTIALS, variable: 'KUBECONFIG')]) {
sh """
sh '''
echo "Updating K3s deployment..."
kubectl --kubeconfig=$KUBECONFIG set image deployment/${DEPLOYMENT_NAME} ${DEPLOYMENT_NAME}=${DOCKER_IMAGE} -n ${K8S_NAMESPACE} || exit 1
kubectl --kubeconfig=$KUBECONFIG rollout status deployment/${DEPLOYMENT_NAME} -n ${K8S_NAMESPACE} || exit 1
"""
echo "Deployment updated successfully!"
'''
}
}
}
@@ -101,10 +107,14 @@ pipeline {
steps {
script {
if (env.DOCKER_IMAGE) {
sh "docker rmi ${env.DOCKER_IMAGE} || echo 'No image to remove'"
sh "docker rmi ${env.DOCKER_IMAGE} || echo 'Image not found, skipping cleanup'"
} else {
echo "No Docker image to clean up"
}
sh "docker image prune -f || echo 'No dangling images'"
sh "docker builder prune -f || echo 'No build cache'"
// Clean up dangling images and build cache
sh "docker image prune -f || echo 'No dangling images to prune'"
sh "docker builder prune -f || echo 'No build cache to prune'"
// Clean up Maven artifacts
sh "mvn clean || echo 'No Maven artifacts to clean'"
}
}
@@ -113,13 +123,14 @@ pipeline {
post {
always {
// Ensure workspace is cleaned up after every run
cleanWs()
}
failure {
echo "Pipeline failed! Check logs."
echo "Pipeline failed! Check the logs for details."
}
success {
echo "Pipeline completed successfully!"
}
}
}
}