3

I am trying to send emails from a Jenkins scripted pipeline with help of the ext-email plugin.

I have configured the plugin with Default Recipients.

Here is my pipeline:

node {
  try {
    echo "hi"
  } catch (e) {
    currentBuild.result = "FAILED"
    notifyBuild(currentBuild.result)
    throw e
  }
  finally {
    notifyBuild(currentBuild.result)
  }
}

def notifyBuild(String buildStatus = 'STARTED') {
  buildStatus =  buildStatus ?: 'SUCCESSFUL'
  def subject = "${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
  def summary = "${subject} (${env.BUILD_URL})"
  def details = """
  <p>STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
  <p>Check console output at "<a href="${env.BUILD_URL}">${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>
  """ 
  emailext (
    subject: subject,
    body: details,
    attachLog: true,
    to: ${DEFAULT_RECIPIENTS},
    recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider'], [$class: 'CulpritsRecipientProvider']]
  )
}

I trying to send the email to a user who triggered the job but I did not get emails to DEFAULT_RECIPIENTS. I also tried with to: env.DEFAULT_RECIPIENTS.

I am getting this error:

groovy.lang.MissingPropertyException: No such property: $DEFAULT_RECIPIENTS for class: groovy.lang.Binding
Andrew Gray
  • 3,593
  • 3
  • 35
  • 62
Guru
  • 1,303
  • 18
  • 32

2 Answers2

7

I also having same problem, just now I got solution on this check the below step:

emailext body: '''${SCRIPT, template="groovy-html.template"}''', 
        subject: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - Successful", 
        mimeType: 'text/html',to: '$DEFAULT_RECIPIENTS'

note down the single qoutes around $DEFAULT_RECIPIENTS that is the key to success here.

I don't know why is not working with double qoutes :(

Amit Nanaware
  • 3,203
  • 1
  • 6
  • 19
  • 2
    That's why: https://stackoverflow.com/questions/6761498/whats-the-difference-of-strings-within-single-or-double-quotes-in-groovy – hakamairi Oct 23 '19 at 10:41
0

You should go to Manage Jenkins->Configure System->Extended E-mail Notification. There you should fill the Default Recipient field and Save. (If you do not have the Email Extension Plugin, please install it).

Then you can use the $DEFAULT_RECIPIENT variable in the 'to' clause. Please, remove the braces.

Hope this helps.

Michael Lihs
  • 7,460
  • 17
  • 52
  • 85
  • When I try this, I just get an error -> `No such property: DEFAULT_RECIPIENTS`. Note that the actual variable is DEFAULT_RECIPIENTS, not DEFAULT_RECIPIENT. Same result for both. Code: `emailext ( to: "$DEFAULT_RECIPIENTS", subject: "STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'", body: """

    STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':

    Check console output at "${env.JOB_NAME} [${env.BUILD_NUMBER}]"

    """) }`
    – Naxin Dec 20 '18 at 05:19