Jenkins emailext plugin with default subject and body in pipeline script - jenkins

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: 'my#my.dom',
replyTo: 'my#my.dom',
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?

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: 'my#my.dom',
replyTo: 'my#my.dom',
subject: '$DEFAULT_SUBJECT',
body: '$DEFAULT_CONTENT',
mimeType: 'text/html'
);
}
}
Make sure to use single quotes, otherwise Groovy will try to expand the variables.

Related

emailext EmailDL usage outside of email stage

How do I use the email-id in prior stages from the variable EmailDL which I have used in the email text plugin in the declarative pipeline template in the last stage?
Is there any Global variable for the EmailDL/recipient/To like $BUILDID which can be used?
we have EmailDL to receive Job Status Emails to collect the email id & use it in the template
post {
always {
emailext (
body:
subject: "XXXXXX",
mimeType: 'text/html',
to: "${EmailDL}"
)
}
}
All works good but how do I reuse the parameter EmailDL outside of the Post stages?
I would suggest define it in environment like this
environment {
EmailDL = "abc#.com"
}
and use it anywhere using ${EmailDL}

Print user defined variable inside a HTML page with Email-ext plugin in Jenkins

I am using email ext in the jenkins pipeline script with email body reference to a file. I want to know how to pass user defined variable.
Ex:- I want to pass the variables "mySonarURL" and "myData" to the HTML file.
def committer = getGitCommitterEmail()
def mySonarURL = "My Sonar URL"
def myData = "My Data"
emailext (
to: committer,
subject: "Jenkins Build ${branchName} #${env.BUILD_NUMBER} [${currentBuild.result}]",
body: '${FILE,path="email.html"}',
mimeType: 'text/html'
)
}
HTML file
<table>
<tr><th>My Data:</th><td>"${myData}"</td></tr>
<tr><th>Sonar URL:</th><td>Sonar URL</td></tr>
</table>
I have tried using as above HTML file but they were returning just the string "${myData}" and "${mySonarURL}"
Mention type as Script in body of mail as guided in official Jenkins wiki
body: '${SCRIPT, template="email.html"}'

How can you configure Jenkins Pipeline so you can automatically send an email with a custom body?

I'm trying to have Jenkins pipeline automatically send an email, but with a custom body. The pipeline is called from a web application by a button, so I was thinking about having a text box there to write the desired message before the button is pressed. However, I don't know how this chunk of text can get sent to Jenkins.
Right now the pipeline is sending emails through emailext, with the body message hardcoded. I know I can pass data from a web app to Jenkins with the Build With Parameters API, which I'm currently using for a Username and Password field, but sending a whole email message as a parameter sounds incorrect.
emailext (
subject: "---subject---",
body: """Hi,
This is the hardcoded message that I would the user to have flexibility to create themselves
""",
to: "---list of recipients---"
)
You can also use something like this, where you could add REGEX and EXCERPT to customize your mail content
emailext(
to: "email_list",
subject: "Subject",
body: '''$BUILD_URL
${BUILD_LOG_REGEX, regex="DRYRUN.*DRYRUN.*DRYRUN",maxMatches=1, showTruncatedLines=false}
${BUILD_LOG_EXCERPT, start="EMAIL CONTENT:",end="END OF EMAIL CONTENT"}''',
recipientProviders: [[$class: 'DevelopersRecipientProvider']]
)
Here is a function ready to be executed, you can also add an attachment.
Adapt to your needs.
def sendMail() {
def body = """
<html>
<body>
<p>Hello</p>
<p><img src="cid:screenshot.jpg" alt="screenshot"/></p>
<ul>
<li><strong>Jenkins Build URL:</strong> ${env.BUILD_URL}</li>
</ul>
</body>
</html>
"""
emailext(to: recipient, subject: 'SUCCESS : ' + subject, body: body, mimeType: 'text/html', attachmentsPattern: 'screenshot.jpg')
}
Ist thing you job needs a parameter MAILTEST and then you can just this parameter in the body of the email just like coldplayer proposed.
Honestley for I used https://wiki.jenkins.io/display/JENKINS/Generic+Webhook+Trigger+Plugin
because the default trigger support only a kind of token as parameter on the rest interface.

Jenkins Declarative Pipeline environment block variables inside email template

I need to pass some variables to an email template after build. I use environment blocks for that (some of them also are created in scripts).
Is this possible?
environment {
SUBSCRIPTION = credentials('subscription')
CERT = credentials('cert')
}
post {
always {
emailext attachLog: true,
body: '''${SCRIPT,template="email.template"}''',
compressLog: true,
mimeType: 'text/html',
subject: "SUCCESS: ${env.JOB_NAME} [${env.BUILD_NUMBER}]",
to: 'email#email.com'
}
}
I made workaround for that, in post step I trigger another job with all data that I need (pass as params) than inside scripted email template I can use them

Display HTML page inside mail body with Email-ext plugin in Jenkins

I am new to Jenkins and I want to know how it is possible to display the HTML report (not the HTML code) generated after a successful build inside a mail body (not as an attachment).
I want to know the exact steps I should follow and what should be the content of my possible jelly template.
Look deeper into the plugin documentations. No need for groovy here.
Just make sure Content Type is set to HTML and add the following to the body:
${FILE,path="my.html"}
This will place the my.html content in your email body (location of file is relative to job's workspace. I use it and it works well.
I hope this helps.
EDIT: Note that you must have the Jenkins version 1.532.1 (or higher) to support this feature with the email-ext plugin.
Besides reading the file with body: ${FILE,path="index.html"}, you need to set the proper content type, either globally or explicitly for one execution, with mimeType: 'text/html.
emailext subject: '$DEFAULT_SUBJECT',
body: '${FILE,path="index.html"}',
recipientProviders: [
[$class: 'CulpritsRecipientProvider'],
[$class: 'DevelopersRecipientProvider'],
[$class: 'RequesterRecipientProvider']
],
replyTo: '$DEFAULT_REPLYTO',
to: '$DEFAULT_RECIPIENTS',
mimeType: 'text/html'
It worked for me with Jenkins 1.558
${FILE,path="target/failsafe-reports/emailable-report.html"}
It should be something like this:
Navigation:
Configure -> Editable Email Notification
Default Content:
${FILE,path="path/result.html"}
If you use a custom path
I had a complication trying to achieve this result because my path was dynamically changing and I had to use a variable inside a FILE variable. So when I tried any of the following
body: '${FILE,path=${report}}'
body: "${FILE,path=${report}}"
body: '''${FILE,path=${report}}'''
and many more, it didn't work. On the other hand I couldn't read the file with groovy because of Jenkins restrictions
My workaround was to read the html directly with shell like so
html_body = sh(script: "cat ${report}", returnStdout: true).trim()
and then just send the email
emailext replyTo: '$DEFAULT_REPLYTO',
subject: "subject",
to: EMAIL,
mimeType: 'text/html',
body: html_body
where ${report} was a path to html file like /var/jenkins/workspace_318/report.html
You just need to assign the link to the environment variable and then you can use that variable to print in the email using ${ENV, var=ENV_VARIABLE}.
You can use Editable Email Notification post build action to send html content as part of mail body.
Copy html content in Default Content and select Content Type as HTML (text/html), as in below image:

Resources