zoukankan      html  css  js  c++  java
  • 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]); } } }
  • 相关阅读:
    Javaweb之 servlet 开发具体解释1
    struts2_6_多个struts配置文件的应用
    PHP多种序列化/反序列化的方法
    CF459C Pashmak and Buses 打印全排列
    HDOJ 题目3564 Another LIS(线段树单点更新,LIS)
    Android UI开发第四十三篇——使用Property Animation实现墨迹天气3.0引导界面及动画实现
    [ACM] HDU 1533 Going Home (二分图最小权匹配,KM算法)
    POJ 3895 Cycles of Lanes (dfs)
    【iOS开发-51】案例学习:动画新写法、删除子视图、视图顺序、延迟方法、button多功能使用方法及icon图标和启动页设置
    iOS-代理托付的使用
  • 原文地址:https://www.cnblogs.com/zhenhui/p/12291361.html
Copyright © 2011-2022 走看看