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]); } } }
  • 相关阅读:
    好的开源项目汇总
    强制SVN上传代码时添加日志
    微信开发-回调模式
    Struct2中自定义的Filter无效
    Ajax 传包含集合的JSON
    PostgreSQL数据库PL/PGSQL学习使用
    单用户对比PG 9.5.4和SYBASE 15.7对超大表的操作性能
    一场一波三折的SQL优化经历
    聚簇索引对数据插入的影响
    磁盘IO初探
  • 原文地址:https://www.cnblogs.com/zhenhui/p/12291361.html
Copyright © 2011-2022 走看看