add for web
This commit is contained in:
109
clinic/service/Jenkinsfile.Web
Normal file
109
clinic/service/Jenkinsfile.Web
Normal file
@@ -0,0 +1,109 @@
|
||||
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: 'http://192.168.100.103:3000/Clinic/clinic-frontend.git',
|
||||
credentialsId: env.GITEA_CREDENTIALS
|
||||
]]
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
stage('Increase Version') {
|
||||
steps {
|
||||
script {
|
||||
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 http://${GIT_USER}:${GIT_PASS}@192.168.100.103:3000/Clinic/clinic-frontend.git HEAD:develop || exit 1
|
||||
"""
|
||||
}
|
||||
|
||||
env.NEW_VERSION = newVersion
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Install & Build') {
|
||||
steps {
|
||||
script {
|
||||
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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
failure {
|
||||
echo "Pipeline failed! Check the logs for details."
|
||||
}
|
||||
success {
|
||||
echo "Pipeline completed successfully!"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user