recipientProviders in jenkins pipeline are always empty - jenkins

after finishing a job I want to send email notification. If the job is triggered manually at least RequesterRecipientProvider if filled with the user who triggered the build. I triggered from gitlab push webhook there is no email address configured in any of the recipientProviders.
emailext (
mimeType: 'text/html',
replyTo: '$DEFAULT_REPLYTO',
subject: subject,
body: details,
to: requester,
recipientProviders: [[$class: 'CulpritsRecipientProvider'],
[$class: 'RequesterRecipientProvider'],
[$class: 'DevelopersRecipientProvider'],
[$class: 'FailingTestSuspectsRecipientProvider' ],
[$class: 'FirstFailingBuildSuspectsRecipientProvider']
]
)
Any idea when those recipientProvider get filled? Where does email-ext get the recipient list from?

I've been banging my head with this for the last couple of days and i think i figured it out:
How it worked(for me):
First - In jenkins.co/configure check all the triggers for email ext plugin and fill in some emails in the default recipients input separated by a single space
Second- In the pipeline place the email ext code snippet just before closing the pipeline inside post brackets as follow
post{
always{
script{emailext(to: '$DEFAULT_RECIPIENTS', stuff....)}}}
This way I did not receive binding errors on build and the email was sent to the mailing list from default recipients, also the brackets nested in post{} seem to be the triggers checked at the previous step.

Related

email on jenkins user input

I have a jenkins pipeline where I wait for user input to proceed or abort. Is there any way I can send the same in email to let the concerned person aware and he can click on the email to procceed Or abort the pipeline.?
It is possible to execute such action by using Jenkins API, this can be achieved using Jenkins Rest Api you can check this SO question.
I haven't tried this solution yet but I guess this pipeline could help you get a better idea of how I would think about implementing such behavior in a pipeline like this:
pipeline {
agent any
stages {
stage('Mail Notification') {
steps {
echo 'Sending Mail'
mail bcc: '',
body: 'Stop job through this link: ${env.BUILD_URL}/job/${env.JOB_NAME}/${env.BUILD_NUMBER}/stop',
cc: '',
from: '',
replyTo: '',
subject: 'Jenkins Job',
to: 'example#domain.com'
}
}
}
}
Jenkins pipeline is aware of such variables like ${env.BUILD_URL} ${env.JOB_NAME} ${env.BUILD_NUMBER}

Send Email to person on build failure on his commits

I want to Send Email specific to a person who's changes caused build failure.
Have you looked up at email-ext plugin?
At post you can set up the trigger failure:
post {
failure {
emailext body: "$env.BODY_CONTENT",
mimeType: 'text/html',
recipientProviders: [requestor(), developers(), culprits()]
subject: '$DEFAULT_SUBJECT',
to: env.PROJECT_RECIPIENTS
}
}
Make sure to configure email-ext plugin.
recipientProviders will add destinations based on:
requestor: the person who started the build
developers: to every person who commit to the repo
culprits: to every person who commit since last non-broken build
You can remove developers and requestor, culprits is the option you're looking for. You can also remove to:

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: '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.

How to get culprits or committers inside a Jenkins workflow with one or more SCMs

Is it possible to access information about committers and/or culprits of a Jenkins workflow job when checking out from one or more SCMs (either via checkout() or other SCM steps like git/svn)?
The intention is to use that information to notify committers and/or culprits about the job status, for example in a mail step.
A small example of a workflow definition:
node {
// checkout from one or more SCMs, e.g.
git url: '<URL>'
checkout([$class:...])
...
// how can we know about committers or culprits at this point?
$committers = ??
// send a mail to committers or culprits
mail to: '$committers', subject: 'JENKINS', body: '<information about the job status>'
}
How could this be adapted to get a collection of the committers after running the SCM steps?
Edit:
I am currently working with Jenkins version 1.596.2 and Workflow: Aggregator version 1.6 and it seems this is an open issue in JENKINS-24141
This is now possible using the email-ext plugin.
def to = emailextrecipients([[$class: 'CulpritsRecipientProvider'],
[$class: 'DevelopersRecipientProvider'],
[$class: 'RequesterRecipientProvider']])
if (to != null && !to.isEmpty()) {
mail to: to, subject: "JENKINS", body: "See ${env.BUILD_URL}"
}
However, if you just want to send an email on failures, you may want to use Mailer (based on the email-ext pipeline examples):
step([$class: 'Mailer',
notifyEveryUnstableBuild: true,
recipients: emailextrecipients([[$class: 'CulpritsRecipientProvider'],
[$class: 'RequesterRecipientProvider']])])
Using groovy within a pipeline script:
#NonCPS // Necessary to allow .each to work.
def changelist() {
def changes = ""
currentBuild.changeSets.each { set ->
set.each { entry ->
changes += "${entry.commitId} by ${entry.author.fullName}\n"
}
}
changes
}
similar to the answer from #szym, but without the #NonCPS required:
def authors = currentBuild.changeSets.collectMany { it.toList().collect { it.author } }.unique()
As you found, pending JENKINS-24141 this is not supported. Changes to Jenkins core are required.
You can get the xml info for a job in which you will find the name of the person who committed the change along with the commit messages.
http://<Jenkins URL>:<Port Number>/job/<Jobname>/<BuildNumber>/api/xml?
Give this a go in your browser. Search for "user".
You can dump this information in a text file to process.
It seems that this feature was implemented inside the email-ext plugin but the author forgot to document the way we are supposed to use this.
Please check https://issues.jenkins-ci.org/browse/JENKINS-34763 -- and add a comment there, asking for an example. I already did.
You can fetch committers email :
committerEmail = sh (
script: 'git --no-pager show -s --format=\'%ae\'',
returnStdout: true
).trim()
and send:
emailext body: 'text you choose', subject: 'subject you choose', recipientProviders: [[$class: 'DevelopersRecipientProvider']], to: committerEmail
taken from : https://medium.com/#dilunika/find-the-git-commit-user-jenkins-pipeline-b6790613f8b5
In the emailext plugin you can provide culprits, developers, requestor etc in the recipientProviders directly.
emailext body: '',
recipientProviders: [culprits(),
developers(),
brokenBuildSuspects(),
brokenTestsSuspects(),
requestor()],
subject: ''
Description
Culprits: Sends email to the list of users who committed a change since the last non-broken build till now. This list at least always include people who made changes in this build, but if the previous build was a failure it also includes the culprit list from there.
Developers: Sends email to all the people who caused a change in the change set.
Broken Build suspects: Sends email to the list of users suspected of causing the build to begin failing.
Broken Test suspects: Sends email to the list of users suspected of causing a unit test to begin failing. This list includes committers and requestors of the build where the test began to fail, and those for any consecutive failed builds prior to the build in which the test began to fail.
Source: Jenkins Pipeline Syntax - Snippet Generator
If you want to notify the culprits who broke the build, You do not need to any checks, Use email plugin in jenkins. This plugin gives you option to send mails to commiter between past good build and current broken build.
If you are using "Editable email notifier plugin" You get option of send mail to culprit.
If you are using email plugin then you get the option "Send separate e-mails to individuals who broke the build".

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