136 lines
5.8 KiB
Plaintext
136 lines
5.8 KiB
Plaintext
pipeline {
|
|
agent any
|
|
|
|
environment {
|
|
REGISTRY_CREDENTIALS = "docker"
|
|
KUBECONFIG_CREDENTIALS = "k3s-kubeconfig"
|
|
GITEA_CREDENTIALS = "gitea"
|
|
K8S_NAMESPACE = "clinic-system"
|
|
DEPLOYMENT_NAME = "clinic-auth-service"
|
|
}
|
|
|
|
stages {
|
|
stage('Checkout Code') {
|
|
steps {
|
|
checkout([$class: 'GitSCM',
|
|
branches: [[name: '*/develop']],
|
|
userRemoteConfigs: [[
|
|
url: 'https://git.hzwnrw.my/Clinic/clinic-auth-service.git',
|
|
credentialsId: env.GITEA_CREDENTIALS
|
|
]]
|
|
])
|
|
}
|
|
}
|
|
|
|
stage('Increase Version') {
|
|
steps {
|
|
script {
|
|
// 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. Ensure Maven is installed and pom.xml exists."
|
|
}
|
|
echo "Current version raw output: '${currentVersion}'" // Debug output
|
|
|
|
// 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 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\[.*?m/, '')
|
|
echo "New version calculated: '${newVersion}'" // Debug output
|
|
|
|
// Validate newVersion
|
|
if (!newVersion || newVersion.contains("null")) {
|
|
error "Invalid new version: '${newVersion}'. Check Maven version update."
|
|
}
|
|
|
|
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 to commit"
|
|
git push https://${GIT_USER}:${GIT_PASS}@git.hzwnrw.my/Clinic/clinic-auth-service.git HEAD:develop || exit 1
|
|
"""
|
|
}
|
|
|
|
env.NEW_VERSION = newVersion
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Build & Push Docker Image') {
|
|
steps {
|
|
script {
|
|
// Build the JAR file
|
|
sh "mvn clean package -DskipTests || exit 1"
|
|
|
|
def dockerImage = "hzwnrw/dev:clinic-auth-service-${env.NEW_VERSION}"
|
|
echo "Building Docker image: ${dockerImage}"
|
|
sh "docker build -t ${dockerImage} . || exit 1"
|
|
|
|
withCredentials([usernamePassword(credentialsId: env.REGISTRY_CREDENTIALS, usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
|
|
sh "echo ${DOCKER_PASS} | docker login -u ${DOCKER_USER} --password-stdin || exit 1"
|
|
}
|
|
|
|
sh "docker push ${dockerImage} || exit 1"
|
|
env.DOCKER_IMAGE = dockerImage
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Deploy to K3s') {
|
|
steps {
|
|
script {
|
|
withCredentials([file(credentialsId: env.KUBECONFIG_CREDENTIALS, variable: 'KUBECONFIG')]) {
|
|
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!"
|
|
'''
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Cleanup') {
|
|
steps {
|
|
script {
|
|
if (env.DOCKER_IMAGE) {
|
|
sh "docker rmi ${env.DOCKER_IMAGE} || echo 'Image not found, skipping cleanup'"
|
|
} else {
|
|
echo "No Docker image to clean up"
|
|
}
|
|
// 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'"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
post {
|
|
always {
|
|
// Ensure workspace is cleaned up after every run
|
|
cleanWs()
|
|
}
|
|
failure {
|
|
echo "Pipeline failed! Check the logs for details."
|
|
}
|
|
success {
|
|
echo "Pipeline completed successfully!"
|
|
}
|
|
}
|
|
} |