How to copy the Junit report from Jenkins inside email? - email

I'm running my testing project by using Jenkins. I can see the Junit report inside Jenkins but now i want to copy the Junit result table inside Jenkins email after every completed run.
I expected the Junit report table can appear inside the email which send from Jenkins automatically.So, whoever receive the email they can know the status of the project.

If you are using Jenkins pipeline using JenkinsFile , this can be done as shown below .
generally all unit test cases reports are generated in failsafe-reports in target folder. so try to give the correct pattern to find out the files .
def call() {
def junitPattern = '**/failsafe-reports/*.xml';
def subject =" write your subject" ;
String emailTo = "xxx" ;
def body = " write your mail body"
def providers = [[$class: 'CulpritsRecipientProvider'], [$class: DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']]
emailext to: emailTo, recipientProviders: providers, compressLog: true, subject: subject, attachmentsPattern: junitPattern , body: body
}

Related

Adding HTML report that change dynamically to mail after build in Jenkins

I already built a stage that sends mail in the end of the build with the status of it and it works perfect. I want to add to this mail an html report of tests that ren in the build. the problem is that the name of the .html file includes the date and time that it was created after every build. This is my mail sending script
post{
always{
echo '------------------------------- SENDING INFORMATIVE MAIL -------------------------------'
emailext subject: "[Jenkins] ${currentBuild.displayName} : ${currentBuild.currentResult}",
body: '''${SCRIPT, template="groovy-html.template"}''', mimeType: 'text/html', attachLog: true,
to: """${RECIPIENTS}"""}
}
this it the file name of the last test:
ADU_TEST_REPORT_02_03_2022_09_31.html
the part the changes every build is this: _02_03_2022_09_31
it is in the path: C:\Jenkins\workspace\Deprtment\Builds\DevOps Builds\Checks\Test_Reports
I tried to add this
(${FILE,path="C:\Jenkins\workspace\Deprtment\Builds\DevOps Builds\Checks\Test_Reports\ADU_TEST_REPORT*.html"})
to the script but it didn't work. What do I do wrong? and do anybody knows how to do it the right way?
------------------------------------ U P D A T E-------------------------------------------
I wrote a python script that writes the name of the most updated file name (with correct date and time) to a .txt file and then i read that txt with groovy in the pipeline. but when i add this string to the file path it sends me an email with the error:
ERROR: File 'C:JenkinsworkspaceDeprtmentBuildsDevOps
BuildsChecksTest_Reports${testReportName}' does not exist
What am i doing wrong? this is my code:
post{
always{
script{
echo '------------------------------- RUN PYTHON SCRIPT THAT FINDS TEST REPORT NAME -------------------------------'
bat """${python_27} "C:\\Users\\dp1234\\Desktop\\TestReportName.py" """
echo '------------------------------- PYTHON SCRIPT EXECUTED - TEST REPORT NAME SAVED IN .txt -------------------------------'
echo '------------------------------- RECEIVE TEST REPORT NAME -------------------------------'
def testReportName = readFile "C:\\Users\\dp1234\\Desktop\\testReports.txt"
echo """${testReportName}"""
echo '------------------------------- TEST REPORT NAME SAVED -------------------------------'
echo '------------------------------- SENDING INFORMATIVE MAIL -------------------------------'
emailext subject: "[Jenkins] ${currentBuild.displayName} : ${currentBuild.currentResult}",
body: '''${SCRIPT, template="groovy-html.template"}''', mimeType: 'text/html', attachLog: true,
//body: '''${FILE, path="C:\\Jenkins\\workspace\\Deprtment\\Builds\\DevOps Builds\\Checks\\Test_Reports\\${testReportName}"}''', mimeType: 'text/html', attachLog: true,
to: """${RECIPIENTS}"""}
}
}

Using environment variable for 'recipients:' in Jenkins Pipeline Mailer

I have a list of email ids in a Jenkins environment variable(emailsdl) and trying to use this for recipients: in Jenkins Pipeline Mailer as below:
mail (to: 'Mailer', recipients: '${env.emailsdl}',
subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) is waiting for input",
body: "Please go to ${env.BUILD_URL}.")
With the above code I am not receiving email and receiving an error:
Email not sent. No recipients of any kind specified ('to', 'cc', 'bcc').
But when I replace ${env.emailsdl} with real email(xyxyx#foo.com) then it does trigger an email. I even tried env['emailsdl'] and it didn't work.
Is there a way I can pass environment variable for recipients in this case?
In groovy if you use single quoted string it will not be interpolated, which means that in the string '${env.emailsdl}' the variable env.emailsdl will not be replaced. You need to use double quoted string: "${env.emailsdl}"
#Gergely: Your suggestion helped me resolving my issue. The problem is that I have a local environment variable which was assigned with a value from other global environment variable: globalVar = xyxyx#foo.com and emailsdl=${globalVar} is in job's local properties. Now I am calling this emailsdl in pipeline script. This has been resolved by:
env.((env.emailsdl).replaceAll("\$", ""))

Display HTML page inside mail body with Email-ext plugin

I'm facing a little problem using Email-ext plugin on Jenkins.
I added the following line to inject my surefire html result to the email sent by the plugin :
${FILE,path="server/target/site/surefire-report.html"}
and I put contenant type as HTML (text/html)
I'm using Jenkins 1.651.1 and Email-ext plugin 2.42
I receive an e-mail in the end of my build with the following contenent
${FILE,path="server/target/site/surefire-report.html"}
It's like the plugin did not understand any thing.
Do you have an idea ?
any update on that ?
my project structure is like this :
workspace
master
commun
server/target/surefire-report/surefire-report.html
If you use a custom path this answer is for you
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 groovy sh command 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
Please note that file path is relative to the workspace , if file resides in the root of your workspace , filename alone in the path will work , if it resides in a different location try somthing like this below :
${FILE,path="target/surefire-reports/emailable-report.html"}
I was also having the prob where the mail had the command with error coould not find file. Like mentioned above, the html file should be present exactly under the jenkins workspace folder. if not then give the relative path, even an extra "/" in front seems to throw error.
ex:
My file is in C:\CICDJenkins\ReadyAPIReport\summary.html
My jenkins workpace (im using custom workspace) is C:\CICDJenkins
what worked for me is:
${FILE, path="ReadyAPI_report/overview-summary.html"}

adding mail notification in evaluatecommand of shellcommand in buildbot

Basically I have inherited ShellCommand to overwrite evaluatecommand.
In evaluatecommand, I parse the log and find the actual maintainer of the package to send a mail notification.
Everything other than mailnotification does not work fine.
class CustomShellCommand(ShellCommand):
command = None
parser = None
haltOnFailure = True
buildername = ''
ci = None
def __init__(self,command, ci, buildername, **kwargs):
self.ci = ci
self.command = command
self.buildername = buildername
ShellCommand.__init__(self, **kwargs)
if len(self.command) > 0 and self.command[0] == 'make_isolated':
self.parser = ParseLog()
self.addLogObserver('stdio', self.parser)
self.setDefaultWorkdir("build")
def evaluateCommand(self, cmd):
if self.parser is not None:
self.parser.packages
for pkg in self.parser.packages:
emails = get_maintainer_emails()
if cmd.rc > 0:
mn = add_mail_notifiers([self.buildername], emails[-1])
self.ci.masterconfig['services'].append(mn)
return util.FAILURE
else:
return util.SUCCESS
But when I add mail notifiers in init it works, but does not work in evaluate command.
Any pointers would be appreciated.
I am no buildbot expert, I just started using it 2 months ago in my new job. But here I think the MailNotifier is something related to the master, and more precisely to the config. For your ShellCommand, I suppose the master executes the __init__ when it loads its config. But the evaluateCommand I think is only executed on runtime by the slave, and they cannot change the config of the master...
Here we have written an external script to send personalized mail for failed builds. It has a builder which triggers it once a day, early in the morning after the nightly builds have finished and before people arrive at the office. We will investigate how to do this more generally as only one of our project has this feature, the other projects' failures are summarized in a general mail sent to everyone. Maybe there is something to do with the SetProperty, but I cannot tell for now...

SoapUI print full URL path of rest request using groovy

I'm using soapui with groovy script step
I want to print the full url of my REST request.
I tried using:
myFile.append( testRunner.testCase.testSteps["My Test Name"].getProperty( "requestUri" ));
and I got null.
You will not be able to see the request info from a test step groovy script. However, the groovy script assertion has access to that information.
You can use this to easily retrieve the full endpoint:
def endpoint = messageExchange.getEndpoint()
The below is working fine for me. you can use the same code just need to change your step name.
Note: Make sure your same Test step should have run prior to below code.else you will get the error
[Cannot invoke method getURL() on null object], see error log for details.
Working Code:
def tr=testRunner.testCase.getTestStepByName("TriggerRequestTransactionsReportsService_V)
def String endPointUrlSave= tr.getHttpRequest().getResponse().getURL();
log.info "Your EndpointUrl is : " + endPointUrlSave;