Jenkins pipeline email notifications with some part of console output in email - jenkins

I am running an Automated test suite on jenkins and I am able to get the status of the job using Email ext plugin. I want to include only some part of build log in email is there any way that I can do that using groovy syntax? Any help would be appreciated!

def notifyFailed() {
emailext (
subject: "FAILED : Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: '''<p><font size="8" color="red">Build Failure!</font></p>
<p>Check console output at &QUOT;<a href='${BUILD_URL}consoleText'>${JOB_NAME} [${BUILD_NUMBER}]</a>&QUOT;</p>
${BUILD_LOG_REGEX, regex="^.*?*****.*?$", linesBefore=0, linesAfter=999, maxMatches=10, showTruncatedLines=false,escapeHtml=false}''',

Related

Want to add the HTML report as an attachment in mail in Jenkins Pipeline post actions

I want to add the Generated Html report in the mail when using Jenkins pipeline post actions.
Path of the HTML reports in the workspace : /var/lib/jenkins/workspace/State_Check_Listings_Live_Apartmentlove/22/execution/node/3/ws/automation-report/reports/07-11-2022/
But I'm not getting any attachments in the mail please help
Using declarative pipeline script
Used script in post actions
emailext mimeType: 'text/html',
attachmentsPattern: "</var/lib/jenkins/workspace/State_Check_Listings_Live_Apartmentlove/28/execution/node/3/ws/automation-report/reports/07-11-2022/>",
body:'Check console output at $BUILD_URL to view the results. \n\n ${CHANGES} \n\n -------------------------------------------------- \n${BUILD_LOG, maxLines=100, escapeHtml=false}',
to: "${EMAIL_TO}",
subject: 'Stage Second Fails in Jenkins: $PROJECT_NAME - #$BUILD_NUMBER'

how to use ${TEST_COUNTS,var="TYPE"} in pipeline email ext content

I can`t get the numbers of build result(e.g. total, pass) by ${TEST_COUNTS,var="TYPE"}
and email content is
emailext
subject: "Automation Result: Job '${env.JOB_NAME} - ${env.BUILD_NUMBER}'",
body:'''
total:${TEST_COUNTS,var="total"},
pass:${TEST_COUNTS,var="pass"},
fail:${TEST_COUNTS,var="fail"}
''',
to:'$DEFAULT_RECIPIENTS'
I got nothing,it should get the correct number
The email-ext plugin's Token TEST_COUNTS dependents on following two things:
Your job workspace folder or sub-folder includes junit xml report. (It can be generate during job building or copy from other place)
Invoke Publish Junit test result report before Editable email notification in job Post-build Actions
Note: Remember change the Test report XMLs to your value. For example: target/surefire-reports/*.xml
If use pipeline as code, should change to
junit '<your junit xml report file path>' // example: target/surefire-reports/*.xml
emailext
subject: "Automation Result: Job '${env.JOB_NAME} - ${env.BUILD_NUMBER}'",
body:'''
total:${TEST_COUNTS,var="total"},
pass:${TEST_COUNTS,var="pass"},
fail:${TEST_COUNTS,var="fail"}
''',
to:'$DEFAULT_RECIPIENTS'

How to set-up Jenkins ext email reports to send only failed tests from build (without those who have passed)?

I'm working with Junit/Ant .I wrote this script in Default contents, but it sends me a report with passed and failed tests, and I'd want it to be the only with a failed tests.
Jenkins Report!
Configuration :
Project Name : $PROJECT_NAME
Project URL : $PROJECT_URL
Build number: #$BUILD_NUMBER
Job Description: $JOB_DESCRIPTION
Execution Results :
Status : $BUILD_STATUS
Run: ${TEST_COUNTS}, Failed: ${TEST_COUNTS,var="fail"}, Passed: ${TEST_COUNTS,var="pass"}, Skipped: ${TEST_COUNTS,var="skip"}
Report preview :
hi you can create email template and configure the email-ext step like below:
emailext body: '''${SCRIPT, template="groovy-html.template"}''',
subject: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - Successful",
mimeType: 'text/html',to: "to list"
here the groovy-html.template is the default template which jenkins pickup and render the result in that template. You can edit this email template to have only failed tests and save that template in the $JENKINS_HOME/email-templates directory and use this template.
Note : use different name for the template other than groovy-html.template.

Jenkins:Send mail based on execute shell script condition

I have below script in Jenkins
if diff file1.txt file2.txt > file3.txt ; then
echo "no difference"
else
<need to send email notitication>
fi
I heard like we can achieve this using 'Email-ext plugin', I have plugin installed in my Jenkins.
Could any one can explain that how to use Email-ext plugin to send mail based on script condition.
hi you can refer to this link for detailed steps to configure email template. In your pipeline script you can add below step to get notification:
emailext body: ''
'${SCRIPT, template="groovy-html.template"}'
'', subject: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - Successful",
mimeType: 'text/html', to: "email list"
The groovy-html.template is default template which you can use or write your own content in email body and subject.

Can't access $BUILD_LOG in Jenkins pipeline

How do I access $BUILD_LOG in a Jenkins pipeline, or is there a better way of getting the log output?
Going off of this answer, I've been trying to access the $BUILD_LOG environment variable, but when I try
echo "${BUILD_LOG, maxLines=50, escapeHtml=false}"
the build errors out:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 11: unexpected token: BUILD_LOG # line 11, column 29.
echo "${BUILD_LOG, maxLines=50, escapeHtml=false}"
And if I try
echo "$BUILD_LOG"
I get this error:
groovy.lang.MissingPropertyException: No such property: BUILD_LOG for class: groovy.lang.Binding
What am I doing wrong? Or is this the wrong way to access the printed output?
I had the same problem with declarative pipelines and a step like:
emailext(
subject: "foo",
to: "bar",
body: """<p>FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
<p>Console output (last 250 lines):<hr><pre>${BUILD_LOG}</pre></p>"""
I had email ext plugin installed.
The solution was to escape the $-sign of the the macro that should be expanded by the plugin, like so:
...
<p>Console output (last 250 lines):<hr><pre>\${BUILD_LOG}</pre></p>"""
...
And be sure to use double quotes.
This way groovy (?) will first expand all the environment variables, and leaves the escaped variables to be dealt with by email ext plugin.
Still haven't found a solution to use BUILD_LOG parameter in a pipeline job with emailext plugin.
As a small solace, I found a workaround to access build log in another way:
currentBuild.rawBuild.getLog(15)
Where 15 is a number of last log lines I want to show.
The example is:
emailext attachLog: true,
body: "Build failed" +
"<br> See attached log or URL:<br>${env.BUILD_URL}" +
"<br><br> <b>The end of build log is:</b> <br>" +
currentBuild.rawBuild.getLog(15).join("<br>"),
mimeType: 'text/html',
subject: "Build failed",
to: 'myemail#somedomain.com'
Note that you have to approve a few script signatures at the In-Process Script Approval in order to allow usage of this function.
From the answer you linked the BUILD_LOG variable is set by the email-extension plugin. Do you have this configured correctly as this may be your issue.

Resources