5

I am writing a simple Jenkins declarative script to run 'make' and send an email with the result (success/failure).

I can send a simple email using:

post {
    success {
        mail to:"myname@me.com", subject:"${currentBuild.fullDisplayName} - Failed!", body: "Success!"
    }
    failure {
        mail to:"myname@me.com", subject:"${currentBuild.fullDisplayName} - Failed!", body: "Failure!"
    }
}

The resulting email is rather simplistic.

How can I call the email-ext plugin from the script to send an old-style post-build email? (I guess this should use email-ext's groovy-text.template).

I would like to be able to access lists like CulpritsRecipientProvider and to include the tail of the console log.

centic
  • 15,565
  • 9
  • 68
  • 125
DavidA
  • 2,053
  • 6
  • 30
  • 54

1 Answers1

9

You can use it in this way:

emailext (
    subject: "STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
    body: """<p>STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
        <p>Check console output at &QUOT;<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>&QUOT;</p>""",
    recipientProviders: [[$class: 'DevelopersRecipientProvider']]
)

For more information you can check:

thokuest
  • 5,800
  • 24
  • 36
Daniel Hernández
  • 4,078
  • 6
  • 27
  • 38
  • Yes, anything you could do in Groovy Script pipelines you can do it in Declarative Syntax, in case you need some script, use "script" step https://jenkins.io/doc/pipeline/steps/pipeline-model-definition/#script-run-arbitrary-pipeline-script – Daniel Hernández May 15 '17 at 15:40
  • Thanks again, so now I have: post { success { script: emailext (subject: "STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'", body: """

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

    Check console output at &QUOT;${env.JOB_NAME} [${env.BUILD_NUMBER}]&QUOT;

    """, recipientProviders: [[$class: 'DevelopersRecipientProvider']]) } }
    – DavidA May 15 '17 at 15:49
  • 7
    I get error: "An attempt to send an e-mail to empty list of recipients, ignored. " How do I set the recipients? – DavidA May 15 '17 at 15:51
  • Configure some mails: https://issues.jenkins-ci.org/secure/attachment/32671/Global%20configuration.png – Daniel Hernández May 15 '17 at 17:20
  • Daniel, thanks for your help. My problem is that my intended list of recipients varies from project to project, so I can't specify them in the global configuration. I want to specify them in the script. Do you know how I can do that? – DavidA May 16 '17 at 07:38
  • In this case it might be easier to use token macros. You can find a list of them here: https://gist.github.com/arjabbar/c6f27c64fd18153680f3b52102688c13 – arjabbar Jun 25 '18 at 14:45
  • How to load style through jenkins – Shankar Jan 11 '21 at 16:20