1

I am using the Email Ext Jenkins plugin and it was working quite well.

Now I need to set the recipients list dynamically. Basically for each build I get a list of email recipients in a file and I need to use that list. My question is:

  • Is there a way to set an Environment Variable so that that can be modified and Recipient List will get that consume that environment variable.

  • I know there is a solution to set programmatically recipients of Jenkins Email-ext plugin in the pre-send script.How To set programmatically recipients of jenkins email ext plugin. However for my case there are some difficulty with that solution as I need to read a file which contains a list of Emails.

Community
  • 1
  • 1
Exploring
  • 2,493
  • 11
  • 56
  • 97

3 Answers3

1

If the format of the file is either comma separated or space separated, you could just use the FILE token (see the content token reference in the plugin). That should put the contents of the file into the recipients list.

slide
  • 792
  • 7
  • 9
0

I can't test this right now so I can't remember if apache commons is available.

Create a file called recipients.groovy with the following contents:

<%
  def stream = new FilePath(build.workspace, "yourfile.txt").read();
  def recipients = IOUtils.toString(stream, "UTF-8");
%>

${recipients}

And in your jobs configuration, in the recipients list, you put ${SCRIPT, script="recipients.groovy"}

API References:

Referring to the recipients.groovy in the Recipient List, gives the following exception:

Failed to create e-mail address for Error in script or template: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script1.groovy: 1:

unexpected token: < @ line 1, column 1. <% ^ 1 error 

Full Exception below:

groovy.lang.MissingPropertyException: No such property: build for class: Script1
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
    at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
    at Script1.run(Script1.groovy:4)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:580)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:618)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:589)
    at hudson.util.RemotingDiagnostics$Script.call(RemotingDiagnostics.java:150)
    at hudson.util.RemotingDiagnostics$Script.call(RemotingDiagnostics.java:122)
    at hudson.remoting.LocalChannel.call(LocalChannel.java:45)
    at hudson.util.RemotingDiagnostics.executeGroovy(RemotingDiagnostics.java:119)
    at jenkins.model.Jenkins._doScript(Jenkins.java:3400)
    at jenkins.model.Jenkins.doScript(Jenkins.java:3377)
    at sun.reflect.GeneratedMethodAccessor344.invoke(Unknown Source)
Exploring
  • 2,493
  • 11
  • 56
  • 97
Josh Unger
  • 6,717
  • 6
  • 33
  • 55
0

You can use the Inject environment variables plugin (https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin) and to create a var during run time, or the Propagate build environment variables (https://wiki.jenkins-ci.org/display/JENKINS/Build+Env+Propagator+Plugin) to change an existing one, and then you can use this var in the Project recipient list when you are using Editable Email Notification (https://wiki.jenkins-ci.org/display/JENKINS/Email-ext+plugin)

kazerm
  • 499
  • 1
  • 4
  • 11