如果Jenkins Slave处于离线状态,则发送电子邮件警报-脚本

时间:2019-07-09 16:37:46

标签: jenkins groovy jenkins-slave

我有Jenkins Slave节点,如果某些节点出现故障,则需要设置电子邮件警报。 设法获取脱机节点的列表,但在电子邮件部分遇到了麻烦。

int exitcode = 0
for (slave in hudson.model.Hudson.instance.slaves) {
    if (slave.getComputer().isOffline().toString() == "true"){
        println('Slave ' + slave.name + " is offline!");
        exitcode++;
    }
}

if (exitcode > 0){
    println("We have a Slave down, failing the build!");
    return 1;
    send_email_notification()
}

def send_email_notification(String body) {
    emailext body: "$body",
            to: "username@domainname.com",
            subject: "Offline Slave",
            presendScript : "msg.addHeader('X-Priority', '1 (Highest)')"
}

Jenkins脚本控制台内部没有错误:

结果 从站NODE01当前离线! 我们有一个奴隶,建设失败! 结果:1

但是我没有收到电子邮件。

1 个答案:

答案 0 :(得分:0)

您有任何错误吗?您可以接收任何电子邮件吗?电子邮件服务器是否在https:// {JENKINS_URL} / configure中配置?

运行此命令会发生什么情况

emailext body: "Testing",
            to: "username@domainname.com",
            subject: "Offline Slave",
            presendScript : "msg.addHeader('X-Priority', '1 (Highest)')"

在我看来,您没有将主体传递给send_email_notification()调用。试试这个:

def send_email_notification(String body) {
    emailext body: "${body}",
            to: "username@domainname.com",
            subject: "Offline Slave",
            presendScript : "msg.addHeader('X-Priority', '1 (Highest)')"
}
for (slave in hudson.model.Hudson.instance.slaves) {
    if (slave.getComputer().isOffline().toString() == "true"){
        msg = "Slave " + slave.name + " is offline!";
        send_email_notification("${msg}")
    }
}