Search code examples
jenkinsjenkins-pipelineemail-ext

Jenkins emailext plugin with default subject and body in pipeline script


I am using Jenkins with the email extension plugin and declarative pipelines. In https://jenkinsserver/configure i configured the "Default Subject" and "Default 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: '[email protected]', 
                replyTo: '[email protected]', 
                subject: "foo", 
                body: "bar", 
                mimeType: 'text/html'
            );
        }
    }

But i don't want to specify something in the pipeline script, everything should be done whith 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?


Solution

  • As stated in the plugin documentation:

    The email-ext plugin uses tokens to allow dynamic data to be inserted into recipient list, email subject line or body. A token is a string that starts with a $ (dollar sign) and is terminated by whitespace. When an email is triggered, any tokens in the subject or content fields will be replaced dynamically by the actual value that it represents.

    This pipeline block should use the default subject and content from Jenkins configuration:

    post {
            always {
                emailext (
                    to: '[email protected]', 
                    replyTo: '[email protected]', 
                    subject: '$DEFAULT_SUBJECT',
                    body: '$DEFAULT_CONTENT',
                    mimeType: 'text/html'
                );
            }
        }
    

    Make sure to use single quotes, otherwise Groovy will try to expand the variables.