NC

Configuring the Jenkins email-ext plugin with Groovy

The default email notifications provided by Jenkins are a little inflexible, but the email-ext plugin adds a whole host of configuration options. I use this to provide the build log in some sync jobs I have, but I wanted to configure this via a script in Jenkins’ init.groovy.d directory to automate the configuration.

I’ve leaned this article, this Gist about setting up users and this other Gist about configuring credentials as I’ve done this before, but I couldn’t find anything which pulled together how you’d configure email-ext. But I did come across this now implemented issue which exposes the important bits of what we’ll need to configure, so a combination of the other examples lead me to come up with the following:

import jenkins.model.Jenkins

def inst = Jenkins.getInstance()
def emailExt = instance.getDescriptor(
  "hudson.plugins.emailext.ExtendedEmailPublisher")

emailExt.setSmtpAuth("username",
                     "password")
emailExt.setDefaultReplyTo("jenkins@example.com")
emailExt.setSmtpServer("smtp.example.com")
emailExt.setUseSsl(true)
emailExt.setSmtpPort("587")
emailExt.setCharset("utf-8")
emailExt.setDefaultRecipients("someone@example.com")

emailExt.save()

…which allows you to configure the basics of the account setup. This is loading the current instant of ExtendedEmailPublisherDescriptor, so any of the setters in that class can be called this way.