Jenkins 2.0 Pipeline 失败发送邮件功能
18
4月
主要代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
def checkpoint = fileLoader.fromGit() // 实现你自己的 jenkins checkpoint // 我们发送的内容为 build 链接 String emailContent = "${env.BUILD_URL}" // 获取 email 收件人, 默认为相关干系人 def to = emailextrecipients([ [$class: 'CulpritsRecipientProvider'], [$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider'] ]) // 给 Jenkins 一个 build 参数, 默认发送的邮箱 String defaultToEmail = "${NOTIFACATION_EMAIL}" Closure sendEmail = { stageName -> String body = """ STEP: $stageName DETAIL: $emailContent """ if (to == null || to == "") { echo "发送邮件的列表 ${to}, 默认发送给 ${defaultToEmail}" to = defaultToEmail } echo "发邮件给 ${to} 所在步骤 ${stageName} 内容 ${emailContent}" mail body: body, from: "someEmail@Alfred", subject: " Jenkins 挂啦" to:to } Closure stageEmailFailure = { nodeLabel, stageName, closure -> try { checkpoint.stage(stageName) { node(nodeLabel) { closure.call() } } } catch (InterruptedException ire) { echo " Jenkins 被终止" currentBuild.result = "ABORTED" throw ire } catch (err) { sendEmail.call(stageName) throw err } } stageEmailFailure("buildSlaveLabel", "Build", { // 你的 build 脚本 }) |
坑 … Read More »