k8s集群中用的jenkinsfile部署
java后台项目
# x项目 test-java-eladmin-ci// harbor镜像仓库地址和密码信息 def registry = "harbor.solomon.com" // jenkins中定义的连接harbor的用户名密码凭据 def registry_se = "d2318c77-8a32-4618-bced-d6cabb0454b4"// gitlab地址和密码信息 def gitlab_url = "http://192.168.17.18/java/eladmin.git" // jenkins中定义git连接gitlab的密码凭据 def gitlab_se = "6e58036d-21ad-42b4-a8d2-50fb700f92c7"// jenkins中k8s-api 连线k8s集群的凭据信息 // jenkins中Config File provice插件定义的kubeconfig的内容 def k8s_auth = "782d66a2-d207-4398-b823-786b999db338"// k8s连接harbor的证书(kubectl命令离开k8s集群时需要,例如目前的jenkins环境) def secret_name = "registry-pull-secret"// harbor仓库的项目前缀信息 def prefix = "library"// 部署应用的服务名称 def app_name = "access-ultra-vires" // 部署服务所在的命名空间、副本数、容器暴露的端口、svc的端口 def ns = "default" def rc = 3 def cport = 8008 def cluport = 8008 // 构建编译的环境参数 def env = "default"node(){stage("1.pull git code"){checkout([$class: 'GitSCM', branches: [[name: '*/dev']], userRemoteConfigs: [[credentialsId: "${gitlab_se}", url: "${gitlab_url}"]]]) script {build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()TM = sh(script: 'date +%Y%m%d%H%M')}image_name_build = "${registry}/${prefix}/${app_name}:${BUILD_NUMBER}"image_name_latest = "${registry}/${prefix}/${app_name}:latest"}stage("2.build code"){sh "mvn clean package -P${env} -DskipTests"}stage("3.build docker and push to harbor") {withCredentials([usernamePassword(credentialsId: "${registry_se}", passwordVariable: 'dockerHubPassword', usernameVariable: 'dockerHubUser')]) {sh "docker login -u ${dockerHubUser} -p ${dockerHubPassword} ${registry}"sh "docker build -t ${image_name_build} -f Dockerfile . --no-cache"sh "docker tag ${image_name_build} ${image_name_latest}"sh "docker push ${image_name_build}"sh "docker push ${image_name_latest}"}}stage("4.deploy to k8s") {// sh "/usr/bin/ossutil cp -uf oss://prod-***/api/blue/templete-v1.yaml templete-v1.yaml"sh "cp templete-v1.yaml ${app_name}-dep.yaml"sh "sed -i 's#SVC_NAME#${app_name}#g' ${app_name}-dep.yaml"sh "sed -i 's#NS_NAME#${ns}#g' ${app_name}-dep.yaml"sh "sed -i 's#RC_NUM#${rc}#g' ${app_name}-dep.yaml"sh "sed -i 's#IMAGE_URL#${image_name_latest}#g' ${app_name}-dep.yaml"sh "sed -i 's#CON_PORT#${cport}#g' ${app_name}-dep.yaml"sh "sed -i 's#CLU_PORT#${cluport}#g' ${app_name}-dep.yaml"sh "kubectl apply -f ${app_name}-dep.yaml"echo "deploy success"} }
vue前端项目
# 项目 vue-test-ci // harbor镜像仓库地址和密码信息 def registry = "harbor.solomon.com" // jenkins中定义的连接harbor的用户名密码凭据 def registry_se = "d2318c77-8a32-4618-bced-d6cabb0454b4"// gitlab地址和密码信息 def gitlab_url = "http://192.168.17.18/vue/eladmin-web.git" // jenkins中定义git连接gitlab的密码凭据 def gitlab_se = "6e58036d-21ad-42b4-a8d2-50fb700f92c7"// jenkins中k8s-api 连线k8s集群的凭据信息 // jenkins中Config File provice插件定义的kubeconfig的内容 def k8s_auth = "782d66a2-d207-4398-b823-786b999db338"// k8s连接harbor的证书(kubectl命令离开k8s集群时需要,例如目前的jenkins环境) def secret_name = "registry-pull-secret"// harbor仓库的项目前缀信息 def prefix = "library"// 部署应用的服务名称 def app_name = "eladmin-web" // 部署服务所在的命名空间、副本数、容器暴露的端口、svc的端口 def ns = "default" def rc = 3 def cport = 80 def cluport = 80 // 构建编译的环境参数 def env = "default"node(){stage("1.pull git code"){checkout([$class: 'GitSCM', branches: [[name: '*/main']], userRemoteConfigs: [[credentialsId: "${gitlab_se}", url: "${gitlab_url}"]]]) script {build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()TM = sh(script: 'date +%Y%m%d%H%M')}image_name_build = "${registry}/${prefix}/${app_name}:${BUILD_NUMBER}"image_name_latest = "${registry}/${prefix}/${app_name}:latest"}stage("2.run build"){nodejs("npm"){sh("node -v && npm -v")sh("npm install")sh("npm run build:prod")sh("tar zcvf dist.tar.gz dist")}}stage("3.build docker and push to harbor") {withCredentials([usernamePassword(credentialsId: "${registry_se}", passwordVariable: 'dockerHubPassword', usernameVariable: 'dockerHubUser')]) {sh "docker login -u ${dockerHubUser} -p ${dockerHubPassword} ${registry}"sh "docker build -t ${image_name_build} -f Dockerfile . --no-cache"sh "docker tag ${image_name_build} ${image_name_latest}"sh "docker push ${image_name_build}"sh "docker push ${image_name_latest}"// 删除压缩包,等待下次构建sh "rm -f dist.tar.gz"}}stage("4.deploy to k8s") {sh "cp templete-v1.yaml ${app_name}-dep.yaml"sh "sed -i 's#SVC_NAME#${app_name}#g' ${app_name}-dep.yaml"sh "sed -i 's#NS_NAME#${ns}#g' ${app_name}-dep.yaml"sh "sed -i 's#RC_NUM#${rc}#g' ${app_name}-dep.yaml"sh "sed -i 's#IMAGE_URL#${image_name_latest}#g' ${app_name}-dep.yaml"sh "sed -i 's#CON_PORT#${cport}#g' ${app_name}-dep.yaml"sh "sed -i 's#CLU_PORT#${cluport}#g' ${app_name}-dep.yaml"sh "kubectl apply -f ${app_name}-dep.yaml"echo "deploy success"}}