node("height_A") { // name name path def projectKey = "scj-v3-front-2026-1-6" //必须是每个项目唯一的key def branch = "${env.BRANCH_NAME}" //当前分支变量 def toEmail = '493840844@qq.com,374362909@qq.com,278725160@qq.com,1012033624@qq.com' //需要接收邮件提醒的 def tag = sh(script: "echo `date +%s`", returnStdout: true).trim() //镜像tag def registryHost = 'docker-new.cyweb.top' //har仓库 def harBorNamespace = 'project' //镜像存储目录 def artifactId = '' def version = '' def needSonarQube = false //是否开启代码质量检测 def isSuccess = true def needNotice = true def needIgnore = false def realpathTemp = ""; def dockerImage = ""; def credentialsId = ""; def remoteHost = ""; def hostPort; def namespace = ""; def profile = ""; try{ stage("初始化分支相关信息"){ switch(branch){ case 'developer' : hostPort = 22 credentialsId = "scj-v3" remoteHost = "192.168.42.127" namespace = "scj-v3" profile = "developer" break; case 'master': hostPort = 22 credentialsId = "cyweb-dev" remoteHost = "192.168.43.18" namespace = "city-zhxy-school-deploy-v3" profile = "deploy" break; default: error('未配置分支!放弃执行') break; } } stage("获取最新代码"){ withFolderProperties{ sh 'printenv' echo "当前分支${branch}" git branch: "${branch}", credentialsId: 'git-ssh-auth', url: "${env.gitSource}" } } stage("打包编译"){ nodejs(cacheLocationStrategy: workspace(), configId: 'bdb7fb3a-3aab-44ab-9d19-b55b4ae896ff', nodeJSInstallationName: '18.20') { sh 'npm config fix' sh 'npm cache clean --force' sh 'npm install --force' // sh 'npm config fix' sh 'npm run build' } } stage("docker 操作"){ if(!needIgnore) { withFolderProperties{ tool name: 'docker: 1.12.6', type: 'dockerTool' withCredentials([usernamePassword(credentialsId: 'dockerharbor_new', passwordVariable: 'password', usernameVariable: 'username')]) { echo "登录docker harbor" sh "docker login ${registryHost} -u ${username} -p ${password} " dockerImage = "${registryHost}/${harBorNamespace}/${projectKey}/${branch}/${projectKey}-${branch}:${tag} " echo "镜像名称:"+dockerImage sh "docker build --build-arg profile=${profile} -t ${dockerImage} -f ${env.WORKSPACE}/deploy-tools/Dockerfile . " echo "提交镜像" sh "docker push ${dockerImage} " //删除本地镜像 防止磁盘漫 sh "docker rmi ${dockerImage} " } } }else{ echo "略过" } } stage("更新服务"){ if(!needIgnore) { withFolderProperties{ echo "开始更新服务" def remote = [:] remote.name = "k8s-master-dev" remote.host = "${remoteHost}" remote.allowAnyHosts = true withCredentials([sshUserPrivateKey(credentialsId: 'root-with-key-ubu-70', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'username')]) { remote.user = "${username}" remote.port = hostPort remote.identityFile = identity sshCommand remote: remote, command: "source ~/.bash_profile;kubectl set image deployment/front front=${dockerImage} --namespace=${namespace}" } } }else{ echo "略过" } } }catch(Exception e){ isSuccess = false currentBuild.result = 'FAILURE' throw e; //必须跑出异常 否则构建失败结果无法被jenkins捕获 }finally{ stage("发送邮件通知"){ if(needNotice) { // if(isSuccess) // { // emailext body: '${FILE,path="deploy-tools/email-notice-success.html"}', subject: '构建通知:${projectName} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS} 请勿回复', to: "$toEmail" // // }else{ // emailext body: '${FILE,path="deploy-tools/email-notice-false.html"}', subject: '构建通知:${projectName} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS} 请勿回复', to: "$toEmail" // // } } } } }