Jenkins emailext plugin with default subject and body in pipeline script

1,014 views
Skip to first unread message

David Koenig

unread,
Jul 31, 2020, 3:01:15 AM7/31/20
to Jenkins Users

i am using Jenkins with the email extension plugin and declarative pipelines. In https://jenkinsserver/configure i configured the "Default Subject" and "Default Content" with some variables and nice looking content which i want to use in a pipeline script.
When i add the following code to a pipeline script, everything works perfectly fine.

    post {
            always {
                emailext (
                    to: 'm...@my.dom',
                    replyTo: 'm...@my.dom',
                    subject: "foo",
                    body: "bar",
                    mimeType: 'text/html'
                );
            }
        }

So technical, everything is working perfectly. But i don't want to specify something in the pipeline script, everything should be done with the data specified in the global configuration. When i remove everything and just call `emailext ();` it failes with the comment that subject is missing. What can i do to work with default values specified globally?

Regards

Dave

Slide

unread,
Jul 31, 2020, 8:45:23 AM7/31/20
to Jenkins User Mailing List
Hi Dave,

Email-ext works on the idea of macros. When you define the default subject in the global config, it sets the value of a macro that you can then use in your job config. If you want to get the value you set, you have to use the macro for it.

subject: '$DEFAULT_SUBJECT'

This is the same with the other items as well. 

You could wrap this into a shared library so that it's just a simple call to do everything you want.

Regards,

Alex


--
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/35d1b78c-39e9-46a9-b942-caae4d7f103cn%40googlegroups.com.

David Koenig

unread,
Aug 5, 2020, 8:55:00 AM8/5/20
to Jenkins Users
Hi,

sorry for the late response, i was away a few days.
i am new to jenkins, so please forgive me to ask some silly questions. Did i understand this right? It doesn't work directly out of a Jenkinsfile? I tried

emailext (
                to: 'mym...@my.dom,
                replyTo: ' mym...@my.dom',
                subject: $DEFAULT_SUBJECT,
                body: '<b>testb</b>test<i>testi</i><p>new area</p>',
                mimeType: 'text/html'
            );

and get the error message

groovy.lang.MissingPropertyException: No such property: $DEFAULT_SUBJECT for class: WorkflowScript

Regards

Dave

Slide

unread,
Aug 5, 2020, 11:12:38 AM8/5/20
to Jenkins User Mailing List
The quotes I had in my example are important, you need single quotes so that Groovy (which pipeline is built on) does not try and interpolate the variable (as you have seen, it doesn't exist in the groovy context).

emailext (
                to: 'mym...@my.dom,
                replyTo: ' mym...@my.dom',
                subject: '$DEFAULT_SUBJECT',

                body: '<b>testb</b>test<i>testi</i><p>new area</p>',
                mimeType: 'text/html'
            )


--

David Koenig

unread,
Aug 6, 2020, 6:13:41 AM8/6/20
to Jenkins Users
Hey man,

again what learned. Thank you so much, you saved my day.

regards

Dave

Am Mittwoch, 5. August 2020 17:12:38 UTC+2 schrieb slide:
The quotes I had in my example are important, you need single quotes so that Groovy (which pipeline is built on) does not try and interpolate the variable (as you have seen, it doesn't exist in the groovy context).

emailext (
                to: 'mym...@my.dom,
                replyTo: ' mym...@my.dom',
                subject: '$DEFAULT_SUBJECT',
                body: '<b>testb</b>test<i>testi</i><p>new area</p>',
                mimeType: 'text/html'
            )

To unsubscribe from this group and stop receiving emails from it, send an email to jenkins...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages