Jenkins Freestyle Job : Invoke email notification from Evaluated Groovy script

650 views Asked by At

I am using a Groovy script to get an email on a list of offline slaves from a Jenkins freestyle job. I have the following code under Jenkins freestyle Job > Build Environment > Evaluated Groovy script :

import hudson.model.Node
import hudson.model.Slave
import jenkins.model.Jenkins

String[] slaves = []
Jenkins jenkins = Jenkins.instance

for (Node node in jenkins.nodes) {
  if (!node.toComputer().online) {
    slaves += node.name
    }
}

 println "[OFFLINE SLAVES]: "+slaves

try{
 emailext body: "${slaves}",
    subject: "test subject",
    to: "[email protected]"
}
catch (Throwable t) {
  println(t)
  throw t;
}

The slave list is printing out fine but while calling emailext, the following ERROR is returned :

groovy.lang.MissingMethodException: No signature of method: Script1.emailext() is applicable for argument types: (java.util.LinkedHashMap) values: [[body:${slaves}, to:[email protected], subject:test subject]] 08:17:48 [EnvInject] - [ERROR] - Problems occurs on injecting env vars defined in the build wrapper: org.jenkinsci.lib.envinject.EnvInjectException: Failed to evaluate the script.

I am new to this and would appreciate some guidance on making this work. Thanks in advance.

1

There are 1 answers

7
Sourav On

Try like this:

try {
    emailext (
         subject: "test subject",
         to: "[email protected]",
         body: "Offline Slaves : '${slaves}'"
     )
}