首先安装好Email Extension Plugin插件并在 system下配置好邮件
然后配置流水线需要的参数
![在这里插入图片描述](https://img-blog.csdnimg.cn/418fc89bfa89429783a1eb37d3e4ee26.png#pic_center
pipeline如下:
def skipRemainingStages = false //是否跳过生产部署
def timeout_mins = 5 //超时时间,单位分
def input_message //提示语
def randomToken //发布秘钥
def skipadminUser = false//系统个人信息
def Applier_id
def Applier_name
def Applier_mailpipeline{agent {label "java_agent"}options { //调用颜色插件ansiColor('xterm')skipDefaultCheckout(true)}environment {Applier_name = ""Applier_mail = ""Operator_mail = ""input_message = ""}stages{stage('构建初始化') {steps("拉取代码"){echo "\033[32m ******开始拉取代码分支代码,****** \033[0m"}}stage("发送审批邮件"){steps{wrap([$class: 'BuildUser']) {script {//获取当前登录用户账户、姓名、邮箱Applier_id = "${env.BUILD_USER_ID}"Applier_name = "${env.BUILD_USER}"Applier_mail = "${env.BUILD_USER_EMAIL}"}}script{//判断当前任务是否需要审批if ("$Mode" == "NO"){skipadminUser = trueExec = truereturn}//判断审批人if ("$adminUser" != ""){adminUser = "$adminUser".split('@')[0] //只取审批人@前面字符//如果审批人为自己,则退出任务if (Applier_id == adminUser){echo "\033[31m 审批人不能为本人,任务已终止。 \033[0m"skipRemainingStages = true //跳过下一个stagecurrentBuild.result = 'ABORTED' //当前stage build result设置为 abortedreturn} }else{echo "\033[31m 审批人不能为空,任务已终止。 \033[0m"skipRemainingStages = truecurrentBuild.result = 'ABORTED' //修改任务状态为终止状态return}skipRemainingStages = falseecho "\033[32m ******发布生产操作需审批,接下来执行生产审批流程****** \033[0m"randomToken = sh (script: "/bin/bash -c 'echo \$RANDOM'" , returnStdout: true).trim()input_message = " $Applier_name 申请发布生产"echo "${randomToken}"emailext(subject:"【请审批】${env.JOB_NAME}(#${env.BUILD_NUMBER})生产部署任务",body:"""$input_message,随机验证码是:"${randomToken}" !!! <br> <a href="${BUILD_URL}input">请点击该链接登录后审批填入token发布</a><br><h3>或者将token值"${randomToken}"告诉运维人员:${Applier_name},让其输入token进行确认</h3>""",to:"${emails}")echo "\033[32m ******申请邮件已经发送,待 $adminUser 审批****** \033[0m"}}}stage("等待审批"){when { expression {!skipRemainingStages}}steps{script{def isAbort = false //取消按钮timeout(timeout_mins){ //等待审批人审批,并通过timeout设置任务过期时间,防止任务永远挂起try {def userInput = input(id: 'inputap', message: "$input_message", ok:"同意", submitter:"$adminUser", parameters: [[$class: 'StringParameterDefinition',defaultValue: "", name: 'token',description: '请输入发布的秘钥' ]])if (userInput == randomToken) {skipRemainingStages = false} else {skipRemainingStages = trueecho "\033[31m 秘钥错误 \033[0m"exit}echo "\033[31m 当前输入秘钥为: ${userInput} \033[0m"}catch(err) { // input falseecho "\033[31m ******任务已被审批人 $adminUser 拒绝****** \033[0m"currentBuild.result = 'ABORTED' //修改任务状态为终止状态return}}}}}}
}
效果如下: