How to specify recipients in email-ext step in declarative script?

16,726 views
Skip to first unread message

David Aldrich

unread,
May 16, 2017, 4:25:40 AM5/16/17
to jenkins...@googlegroups.com

Hi

 

I am experimenting with declarative scripts and have this simple script:

 

pipeline {

    agent { label "mypc" }

    stages {

        stage('build') {

            steps {

                sh 'python --version'

            }

        }

    }

    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']])

        }

    }

}

 

I can’t specify the list of default recipients in the global settings for the email-ext plugin, as the required recipients change from project to project.  I need to specify the list in the script.  I’ve received a suggestion to:

 

use this pipeline step https://jenkins.io/doc/pipeline/steps/email-ext/#emailextrecipients-extended-email-recipients to get the recipients and then use the "to" parameter to the emailext step and add your own addresses to the return of that step”

 

I don’t know how to do this. Please could someone show me how to modify my script to do it?

 

Best regards

 

David

 

Danny Rehelis

unread,
May 16, 2017, 9:08:53 AM5/16/17
to jenkins...@googlegroups.com
Hey David,

This should be pretty straightforward, try this:
    post {
        success {
            emailext (
                to: a...@me.com; a...@him.com,
                subject: "SUCCESS",
                body: "SUCCESS!"
            )
        }
        failure {
emailext (
                to: a...@me.com; a...@him.com,
                subject: "FAILURE",
                body: "FAILURE!"
            )
        }
    }

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/21c2aac48caa41998f52a2e233d4ed76%40EUX13SRV1.EU.NEC.COM.
For more options, visit https://groups.google.com/d/optout.

David Aldrich

unread,
May 16, 2017, 9:18:59 AM5/16/17
to jenkins...@googlegroups.com

Hi Danny,

 

That worked (with email addresses in quotes) – thanks.

 

Do you know how I would specify ‘to:’ as a certain email address + committers?

 

Best regards

 

David

 


For more options, visit https://groups.google.com/d/optout.

Click here to report this email as spam.

Slide

unread,
May 16, 2017, 9:39:27 AM5/16/17
to jenkins...@googlegroups.com
Something like this might work:

script { 
    def recipients = emailextrecipients([ [$class: 'DevelopersRecipientProvider'] ])
    emailext(..., to: recipients)
}

David Aldrich

unread,
May 16, 2017, 10:12:14 AM5/16/17
to jenkins...@googlegroups.com

Hi Slide

 

Thanks. I tried the code below based on your suggestion but the syntax of my change isn’t quite right. Can you help please?

 

    post {

        success {

            script{ def recipients = emailextrecipients([ [$class: 'DevelopersRecipientProvider'] ])

                   emailext (to: "daldrich"; recipients,

                              subject: '${DEFAULT_SUBJECT}',                                                         

                              body: '${DEFAULT_CONTENT}',  

                              recipientProviders: [[$class: 'DevelopersRecipientProvider']])

        }

    }

 

    org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:

WorkflowScript: 13: expecting ')', found ',' @ line 13, column 57.

   xt (to: "daldrich"; recipients, 

                                 ^

 

1 error

 

Slide

unread,
May 16, 2017, 11:38:53 AM5/16/17
to jenkins...@googlegroups.com
to: "daldrich," + recipients  

Also, you may be missing a '}' to close the script block.

David Aldrich

unread,
May 16, 2017, 12:02:17 PM5/16/17
to jenkins...@googlegroups.com

David Aldrich

unread,
May 17, 2017, 3:53:24 AM5/17/17
to jenkins...@googlegroups.com

Hi Slide

 

I think the script is still not quite right.  I now have:

 

    post {

        success {

            script{ def recipients = emailextrecipients([ [$class: 'DevelopersRecipientProvider'] ])

                    emailext (to: "daldrich," + recipients,

                              subject: '${DEFAULT_SUBJECT}',                                                           

                              body: '${DEFAULT_CONTENT}',  

                              recipientProviders: [[$class: 'DevelopersRecipientProvider']])

            }

        }

    }

 

I see two problems:

 

1) The output shows:

 

Failed to create e-mail address for co...@us.COM co...@us.COM co...@us.COM

Sending email to: co...@us.COM co...@us.COM co...@us.COM

 

These are the correct committers’ email addresses but I have disguised them for this question.  I receive an email but I don’t know why I see the ‘Failed’ message in the log. Any ideas please?

 

2) The subject uses : '${DEFAULT_SUBJECT}', which ends with  ‘$BUILD_STATUS!’  In the received email the build status was ‘Building!’.  This is in ‘post’ so I expect the status to be ‘Success!’.  Any idea why this is wrong?

David Aldrich

unread,
May 17, 2017, 7:07:51 AM5/17/17
to jenkins...@googlegroups.com

I tried replacing my code with that produced by the snippet generator:

 

            script{ emailext (body: '${DEFAULT_CONTENT}',

                              recipientProviders: [[$class: 'CulpritsRecipientProvider']],

                              subject: '${DEFAULT_SUBJECT}',

                              to: 'daldrich')

            }

 

But the result is an email to daldrich but not to the culprits.

 

Could this be a bug in the emailext plugin?

 

BR

Slide

unread,
May 17, 2017, 10:16:22 AM5/17/17
to jenkins...@googlegroups.com

Try removing the recipientProvider parameter to emailext, I'm not sure if that will mess things up. I'm not familiar with declarative pipeline enough to say how to get the correct build status. How are you running the job? If you are running it manually and there have been no changes, then there are no culprits. The culprits are pulled from the changes from the scm you specify.


David Aldrich

unread,
May 17, 2017, 10:27:23 AM5/17/17
to jenkins...@googlegroups.com

Hi Slide

 

I am running the job manually. There are commits by another user since the job last ran, so I assume there should be culprits.  The job actually succeeds, but I’m assuming that the culprits should still receive the email.

 

If I remove:

 

recipientProviders: [[$class: 'CulpritsRecipientProvider']],

 

then the culprits definitely won’t get the email?

 

BR

 

David

 

Slide

unread,
May 17, 2017, 12:45:15 PM5/17/17
to jenkins...@googlegroups.com
You are using "to: ", you don't need both to and recipientProviders, especially since you are already using the def recipients = emailext... to get the same thing. If the job succeeds, I don't think the culprits will be emailed, but I could be wrong. Are you using a pipeline or other type of job? Culprits are people who may have broken the build, so if the build is succeeding, then it may not email the culprits. You may want DevelopersRecipientProvider if you want anyone who added a change to be emailed.

David Aldrich

unread,
May 18, 2017, 5:33:30 AM5/18/17
to jenkins...@googlegroups.com

Hi Slide

 

Thanks for helping me with this.  I am using a declarative pipeline. In the ‘post’ clause, I set ‘success’, ‘failure’, ‘unstable’ and ‘changed’ to all be:

 

            script{ emailext (body: '${DEFAULT_CONTENT}',

                              recipientProviders: [[$class: 'CulpritsRecipientProvider']],

                              subject: '${DEFAULT_SUBJECT}',

                              to: 'daldrich')

 

‘Failure’ works fine – daldrich and the culprits are emailed.

 

However, when the failure is fixed, i.e. we have condition ‘changed’, only daldrich is emailed.  In a traditional job the culprits should also be notified of the change to Fixed.

 

Perhaps this is an email-ext plugin bug?

 

Best regards

 

David

 

Slide

unread,
May 18, 2017, 9:52:47 AM5/18/17
to jenkins...@googlegroups.com
Do you have the same recipient providers specified for the pipeline as you do in your normal (free style) job?

David Aldrich

unread,
May 18, 2017, 10:12:36 AM5/18/17
to jenkins...@googlegroups.com

Well I haven’t found definitions of the providers so I can’t be sure, but the free-style job specifies:

 

Fixed: Recipient List, Culprits

 

and the Project Recipient List contains daldrich

 

So, yes, I think they are equivalent.

 

BR

 

David

 

Slide

unread,
May 18, 2017, 2:47:57 PM5/18/17
to jenkins...@googlegroups.com

Then it's possible that it's the way culprits are determined for each job type. Free style inherits from AbstractProject, so the build has a getCulprits() that email-ext can call. It has to determine culprits on its own for pipeline jobs and it may not have all the info that a free style project does.


David Aldrich

unread,
May 19, 2017, 7:29:04 AM5/19/17
to jenkins...@googlegroups.com

Hi Slide

 

Do you know whether it’s possible to get the culprits without using email-ext, i.e. using the ‘mail’ command?

 

Best regards

 

David

 

Slide

unread,
May 19, 2017, 9:34:07 AM5/19/17
to jenkins...@googlegroups.com
I don't know. It uses the Mailer plugin, so maybe you can look at the docs.

Reply all
Reply to author
Forward
0 new messages