125 lines
5.0 KiB
Plaintext
125 lines
5.0 KiB
Plaintext
pipeline {
|
|
agent any
|
|
|
|
environment {
|
|
REGISTRY_CREDENTIALS = "docker"
|
|
KUBECONFIG_CREDENTIALS = "k3s-kubeconfig"
|
|
GITEA_CREDENTIALS = "gitea"
|
|
K8S_NAMESPACE = "clinic-system"
|
|
DEPLOYMENT_NAME = "clinic-frontend"
|
|
}
|
|
|
|
stages {
|
|
stage('Checkout Code') {
|
|
steps {
|
|
checkout([$class: 'GitSCM',
|
|
branches: [[name: '*/develop']],
|
|
userRemoteConfigs: [[
|
|
url: 'https://git.hzwnrw.my/Clinic/clinic-frontend.git',
|
|
credentialsId: env.GITEA_CREDENTIALS
|
|
]]
|
|
])
|
|
}
|
|
}
|
|
|
|
stage('Increase Version') {
|
|
steps {
|
|
script {
|
|
// Set Git identity before running npm version
|
|
sh '''
|
|
git config user.email "jenkins@yourdomain.com"
|
|
git config user.name "Jenkins"
|
|
'''
|
|
|
|
def currentVersion = sh(script: "node -p \"require('./package.json').version\"", returnStdout: true).trim()
|
|
if (!currentVersion) {
|
|
error "Unable to retrieve current version from package.json."
|
|
}
|
|
echo "Current version: '${currentVersion}'"
|
|
|
|
sh "npm version patch -m 'Auto-increment version [skip ci]'"
|
|
|
|
def newVersion = sh(script: "node -p \"require('./package.json').version\"", returnStdout: true).trim()
|
|
echo "New version: '${newVersion}'"
|
|
|
|
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 package.json package-lock.json
|
|
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-frontend.git HEAD:develop || exit 1
|
|
"""
|
|
}
|
|
|
|
env.NEW_VERSION = newVersion
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Install & Build') {
|
|
steps {
|
|
script {
|
|
sh "cp production.env .env || exit 1"
|
|
sh "npm install || exit 1"
|
|
sh "npm run build || exit 1"
|
|
|
|
def dockerImage = "hzwnrw/dev:clinic-frontend-${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 npm artifacts
|
|
sh "rm -rf node_modules build || echo 'No npm 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!"
|
|
}
|
|
}
|
|
} |