jenkins pipeline 之 deploy k8s 环境并发送邮件通知

项目中有更新代码之后触发jenkins任务,部署好之后并发送邮件给发开人员

#!/usr/bin/env groovy Date date = new Date()
def time
= date.format("yyyyMMdd") def project_name = "test1" def mail_list = "test@163.com" def cmd_pods = "kubectl get pods -n test" def depoly_ip = "192.168.9.181" def access_port = "30002" def pod_status(cmd,type){ script{ if(type == "clean"){ sum = 1 while(true){ sleep 20 sum +=1 echo "${cmd}| grep test| wc -l" out = sh(script: "${cmd}| grep test| wc -l", returnStdout: true) echo "${out}" echo "${cmd}| grep test | wc -l" if(out.toInteger() == 0){ echo "clean test env" break } if(sum == 20 ){ error "not clean env pods" break } } } if(type == "deploy"){ sum = 1 while(true){ sleep 30 sum += 1 out = sh(script: "${cmd}| grep Running| wc -l ", returnStdout: true) out1 = sh(script: "${cmd}| grep Completed| wc -l ", returnStdout: true) if(out1.toInteger() == 5) { if(out.toInteger() >= 14) { echo "depoly success " break } } if(sum == 20 ){ error "pods not Running" break } } } } } pipeline { agent { label '192.168.9.181' } //agent any environment { def changeBranch = "change-${GERRIT_CHANGE_NUMBER}-${GERRIT_PATCHSET_NUMBER}" } stages { stage("download code"){ steps{ git branch: "oi" ,url: 'git@test.com:test/test-helm.git' script { build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim() commit_id = sh(returnStdout: true, script: 'git show --pretty=%H| head -1').trim() mail_name = sh(returnStdout: true, script: 'git show --pretty=%ae| head -1').trim() change_id = sh(returnStdout: true, script: 'git show --pretty=%b| head -1').trim() update_info = sh(returnStdout: true, script: 'git show --pretty=%s| head -1').trim() } } } stage("clean env"){ steps{ dir("/root/appaiops/"){ script { sh(returnStatus: true, script: "./clean-test.sh") pod_status(cmd_pods,"clean") } } sh "sudo rm -rf /root/appaiops" } } stage ('deploy env') { steps { echo "start deploy env" sh "./build.sh " dir("/root/appaiops/"){ sh "./run-test.sh ${depoly_ip}" } } } stage('check pods status') { steps { script{ pod_status(cmd_pods,"deploy") sh "${cmd_pods}" } } } } post { always { script { email_format = """ <table border="1"> <tr> <td>Project</td> <td>${project_name}</td> </tr> <tr> <td>commit_id:</td> <td>${commit_id}</td> </tr> <tr> <td>Author:</td> <td>${mail_name}</td> </tr> <tr> <td>access address </td> <td>http://${depoly_ip}:${access_port}</td> </tr> <tr> <td>pods depoly status</td> <td>Successful</td> </tr> </table> """ } } success { emailext ( subject: " Deploy Successful ${env.JOB_NAME} [${update_info}]", body: """ <p>Deploy test/${project_name} env Successful </p> <p>${BUILD_URL}: SUCCESS </p> ${email_format}""", to: "${mail_list},${mail_name}", from: "test1@163.com",) } failure { step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "${mail_list}", sendToIndividuals: true]); } } }

 

posted @ 2020-02-10 16:16  扛把子修BUG  阅读(844)  评论(0编辑  收藏  举报