How to invoke email-ext plugin from Jenkins declarative script?
Asked Answered
L

1

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:"[email protected]", subject:"${currentBuild.fullDisplayName} - Failed!", body: "Success!"
    }
    failure {
        mail to:"[email protected]", 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.

Liegeman answered 15/5, 2017 at 14:37 Comment(0)
O
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:

Offence answered 15/5, 2017 at 15:8 Comment(7)
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 jenkins.io/doc/pipeline/steps/pipeline-model-definition/…Bruno
Thanks again, so now I have: post { success { script: 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']]) } }Liegeman
I get error: "An attempt to send an e-mail to empty list of recipients, ignored. " How do I set the recipients?Liegeman
Configure some mails: issues.jenkins-ci.org/secure/attachment/32671/…Bruno
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?Liegeman
In this case it might be easier to use token macros. You can find a list of them here: gist.github.com/arjabbar/c6f27c64fd18153680f3b52102688c13Selfpreservation
How to load style through jenkinsQuartan

© 2022 - 2024 — McMap. All rights reserved.