带有循环电子邮件的 Jenkinsfile 在第一次迭代时失败
Jenkinsfile with email in loop fails on first iteration
使用最新的 Jenkins。
编辑:完整 Java 异常的 pastebin - https://pastebin.com/zZDNj18E
目标:遍历所有节点,检查离线,为每个离线节点发送电子邮件警报。
(也尝试了 emailext 警报,无法使用 "offline")
失败:我的 jenkinsfile 运行完美,没有电子邮件。
对于 for 循环中的电子邮件或在函数中单独定义的电子邮件,作业在发送第一封电子邮件后 崩溃。
[Pipeline] End of Pipeline an exception which occurred: in field hudson.model.Slave.launcher in object hudson.slaves.DumbSlave@ae938e61 .... and many more
我的詹金斯文件:
pipeline {
agent{
label 'master'
}
options {
// Enable timestamps in log
timestamps()
skipDefaultCheckout()
timeout(time: 4, unit: 'MINUTES')
}
stages {
stage('Monitor') {
steps{
script{
def offlineSlaves = []
for (aSlave in hudson.model.Hudson.instance.slaves) {
def thisSlave = aSlave.name
echo 'Name: ' + thisSlave + ' is being checked.'
if ( aSlave.getComputer().isOffline().toString() == 'true') {
slaveState = 'OFFLINE'
echo 'Name: ' + thisSlave + ' is ' + slaveState + ' !'
emailext (
mimeType: 'text/html',
body: "${env.JOB_NAME} found an OFFLINE node: ${name} ",
subject: "Jenkins ERROR: Build Node ${name} is OFFLINE " ,
to: 'jfisher@xxx')
}
}
}
}
}
}
post {
failure {
emailext (
body: 'Monitor Nodes Jenkins Job failed !',
presendScript: '$DEFAULT_PRESEND_SCRIPT',
recipientProviders: [requestor(),culprits()],
subject: 'Monitor Nodes Jenkins Failed',
to: 'jfisher@intouchhealth.com')
}
}
}
此代码的问题在于 getComputer()
部分。在管道中,您应该只使用 Serializable 而从 getComputer()
返回的 SlaveComputer
不是。
https://javadoc.jenkins.io/hudson/slaves/SlaveComputer.html
你应该做的是将这部分移动到一个用 NonCPS 注释的函数中
@NonCPS
def shallTrigger() {
for (aSlave in hudson.model.Hudson.instance.slaves) {
def thisSlave = aSlave.name
echo 'Name: ' + thisSlave + ' is being checked.'
if ( aSlave.getComputer().isOffline().toString() == 'true') {
slaveState = 'OFFLINE'
echo 'Name: ' + thisSlave + ' is ' + slaveState + ' !'
emailext (
mimeType: 'text/html',
body: "${env.JOB_NAME} found an OFFLINE node: ${name} ",
subject: "Jenkins ERROR: Build Node ${name} is OFFLINE " ,
to: 'jfisher@xxx')
}
}
}
使用最新的 Jenkins。
编辑:完整 Java 异常的 pastebin - https://pastebin.com/zZDNj18E
目标:遍历所有节点,检查离线,为每个离线节点发送电子邮件警报。 (也尝试了 emailext 警报,无法使用 "offline")
失败:我的 jenkinsfile 运行完美,没有电子邮件。 对于 for 循环中的电子邮件或在函数中单独定义的电子邮件,作业在发送第一封电子邮件后 崩溃。
[Pipeline] End of Pipeline an exception which occurred: in field hudson.model.Slave.launcher in object hudson.slaves.DumbSlave@ae938e61 .... and many more
我的詹金斯文件:
pipeline {
agent{
label 'master'
}
options {
// Enable timestamps in log
timestamps()
skipDefaultCheckout()
timeout(time: 4, unit: 'MINUTES')
}
stages {
stage('Monitor') {
steps{
script{
def offlineSlaves = []
for (aSlave in hudson.model.Hudson.instance.slaves) {
def thisSlave = aSlave.name
echo 'Name: ' + thisSlave + ' is being checked.'
if ( aSlave.getComputer().isOffline().toString() == 'true') {
slaveState = 'OFFLINE'
echo 'Name: ' + thisSlave + ' is ' + slaveState + ' !'
emailext (
mimeType: 'text/html',
body: "${env.JOB_NAME} found an OFFLINE node: ${name} ",
subject: "Jenkins ERROR: Build Node ${name} is OFFLINE " ,
to: 'jfisher@xxx')
}
}
}
}
}
}
post {
failure {
emailext (
body: 'Monitor Nodes Jenkins Job failed !',
presendScript: '$DEFAULT_PRESEND_SCRIPT',
recipientProviders: [requestor(),culprits()],
subject: 'Monitor Nodes Jenkins Failed',
to: 'jfisher@intouchhealth.com')
}
}
}
此代码的问题在于 getComputer()
部分。在管道中,您应该只使用 Serializable 而从 getComputer()
返回的 SlaveComputer
不是。
https://javadoc.jenkins.io/hudson/slaves/SlaveComputer.html
你应该做的是将这部分移动到一个用 NonCPS 注释的函数中
@NonCPS
def shallTrigger() {
for (aSlave in hudson.model.Hudson.instance.slaves) {
def thisSlave = aSlave.name
echo 'Name: ' + thisSlave + ' is being checked.'
if ( aSlave.getComputer().isOffline().toString() == 'true') {
slaveState = 'OFFLINE'
echo 'Name: ' + thisSlave + ' is ' + slaveState + ' !'
emailext (
mimeType: 'text/html',
body: "${env.JOB_NAME} found an OFFLINE node: ${name} ",
subject: "Jenkins ERROR: Build Node ${name} is OFFLINE " ,
to: 'jfisher@xxx')
}
}
}