电子邮件循环的Jenkinsfile在第一次迭代时失败

时间:2019-02-08 01:32:52

标签: groovy jenkins-pipeline jenkins-plugins

使用了最新的詹金斯。

编辑:完整Java异常的pastebin-https://pastebin.com/zZDNj18E

目标:遍历所有节点,检查每个离线节点的离线,电子邮件警报。 (也尝试了emailext警报,不能使用“离线”)

失败:我的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

我的jenkinsfile:

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')
        }
    }
}

1 个答案:

答案 0 :(得分:0)

此代码的问题是tenThings部分。在管道中,您应该仅使用Serializable,而不能使用从let tenMoreThings: [Thing] = [Thing(), Thing(), Thing(), Thing(), Thing(), Thing(), Thing(), Thing(), Thing(), Thing()]返回的getComputer()

https://javadoc.jenkins.io/hudson/slaves/SlaveComputer.html

您应该做的是将此部分移至带有NonCPS注释的功能

SlaveComputer