首先安装好Email Extension Plugin插件并在 system下配置好邮件
然后配置流水线需要的参数
#pic_center
pipeline如下:文章来源:https://www.toymoban.com/news/detail-580164.html
def skipRemainingStages = false //是否跳过生产部署
def timeout_mins = 5 //超时时间,单位分
def input_message //提示语
def randomToken //发布秘钥
def skipadminUser = false
//系统个人信息
def Applier_id
def Applier_name
def Applier_mail
pipeline{
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 = true
Exec = true
return
}
//判断审批人
if ("$adminUser" != ""){
adminUser = "$adminUser".split('@')[0] //只取审批人@前面字符
//如果审批人为自己,则退出任务
if (Applier_id == adminUser){
echo "\033[31m 审批人不能为本人,任务已终止。 \033[0m"
skipRemainingStages = true //跳过下一个stage
currentBuild.result = 'ABORTED' //当前stage build result设置为 aborted
return
}
}else{
echo "\033[31m 审批人不能为空,任务已终止。 \033[0m"
skipRemainingStages = true
currentBuild.result = 'ABORTED' //修改任务状态为终止状态
return
}
skipRemainingStages = false
echo "\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 = true
echo "\033[31m 秘钥错误 \033[0m"
exit
}
echo "\033[31m 当前输入秘钥为: ${userInput} \033[0m"
}catch(err) { // input false
echo "\033[31m ******任务已被审批人 $adminUser 拒绝****** \033[0m"
currentBuild.result = 'ABORTED' //修改任务状态为终止状态
return
}
}
}
}
}
}
}
效果如下:
文章来源地址https://www.toymoban.com/news/detail-580164.html
到了这里,关于jenkins发布使用邮件添加审批的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!