groovy 如果Jenkins代理离线,则发送电子邮件警报-脚本

njthzxwz  于 6个月前  发布在  Jenkins
关注(0)|答案(1)|浏览(69)

我有Jenkins代理节点,如果一些节点关闭,需要设置电子邮件警报。设法获取离线节点列表,但与电子邮件部分斗争。

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: "[email protected]",
            subject: "Offline Slave",
            presendScript : "msg.addHeader('X-Priority', '1 (Highest)')"
}

字符串
在Jenkins脚本控制台中没有错误:
结果Slave NODE 01离线!我们有一个Slave关闭,构建失败!结果:1
但我没有收到电子邮件。

zxlwwiss

zxlwwiss1#

您是否有任何错误?您是否能够接收任何电子邮件?电子邮件服务器是否在https://{JENKINS_URL}/configure中配置?
如果你运行这个会发生什么:

emailext body: "Testing",
            to: "[email protected]",
            subject: "Offline Slave",
            presendScript : "msg.addHeader('X-Priority', '1 (Highest)')"

字符串
在我看来,你没有向send_email_notification()调用传递body。试试这个:

def send_email_notification(String body) {
    emailext body: "${body}",
            to: "[email protected]",
            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}")
    }
}

相关问题