Jenkins pipelineのdeploy k 8 s環境でメール通知を送信

12589 ワード

 jenkins , 

#!/usr/bin/env groovy Date date = new Date()
def time
= date.format("yyyyMMdd") def project_name = "test1" def mail_list = "[email protected]" 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: '[email protected]: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 = """
"1">
Project
${project_name}
commit_id:
${commit_id}
Author:
${mail_name}
access address
http://${depoly_ip}:${access_port}
pods depoly status
Successful
"""
}
}
success {
emailext (
subject: "Deploy Successful ${env.JOB_NAME} [${update_info}]",
body: """
Deploy test/${project_name} env Successful
${BUILD_URL}: SUCCESS
${email_format}""",
to: "${mail_list},${mail_name}",
from: "[email protected]",)
}
failure {
step([$class: 'Mailer',
notifyEveryUnstableBuild: true,
recipients: "${mail_list}",
sendToIndividuals: true]);
}
}
}