首页 > 其他 > 详细

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

时间:2020-02-10 17:01:25      阅读:247      评论:0      收藏:0      [点我收藏+]
项目中有更新代码之后触发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-isddc.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-isddc.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 ISDDC/${project_name} env Successful </p> <p>${BUILD_URL}: SUCCESS </p> ${email_format}""", to: "${mail_list},${mail_name}", from: "thinkcloud@lenovo.com",) } failure { step([$class: Mailer, notifyEveryUnstableBuild: true, recipients: "${mail_list}", sendToIndividuals: true]); } } }

 

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

原文:https://www.cnblogs.com/zhenhui/p/12291361.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!