Modifying HTML links with new location in Jenkins Email Extension - jenkins

Cucumber Report
I have a cucumber report and want to have the report in the body of the email using Jenkins email extension after every build. Before jenkins sends out the report ,links of the features needs to be updated with new build location, where the cucumberreport.html content should be updated with the latest build link.
Can you help how this can be achieved

There are two options to achieve this
First one is using a groovy script:
reportFile = <location and filename of report>
oldURL = <url in report>
newURL = <url for sending>
sTemplate = readFile "${reportFile}"
sTemplate = sTemplate.replace(oldURL,newURL)
emailext body: "${sTemplate}", subject: "Cucumber Report", to: receiver#email.com
The other option is (if you are on a linux) using sed:
sh """
OLDSTR="<url in report>";NEWSTR="<url for sending>";file="<location and filename of report>";sed -i -- 's#'"$OLDSTR"'#'"$NEWSTR"'#g' "$file"
"""
emailext body: "<location and filename of report>", subject: "Cucumber Report", to: receiver#email.com
CAVEAT: the option using sed will modify the file on disk

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'

Attach latest build generated file (not log file) from Jenkins emailext?

I am running a Jenkins pipline which is calling a script
In following example, running a script which is generating output file with issue_{datetime}.txt
I want to send this file as attachment. However, as file name is generated every time with separate date time stamps not able to find a way to attach file.
e.g.
pipeline {
agent any
stages{
stage('Start Build'){
steps{
python run.py
}
}
}
post {
always {
echo ("${email_id}")
emailext from: 'xyz#yahoo.com', attachmentsPattern: 'resources/*.xlsx',
to: "${email_id}",
}
}
}
You can do it like this try this,
emailext attachmentsPattern: '**/resources/*.xlsx', body: 'Find attachments', subject: 'Attachment', to: 'test#ab.org'
OUTPUT_FILE = sh(script: 'ls -t resources/issue*.xlsx | head -1', returnStdout: true)
emailext from: 'test#yahoo.com', attachmentsPattern:"${OUTPUT_FILE}"

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'

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.

How to get environment variable in Jenkins email notification?

In Jenkins job configuration I have written a bat script in the command window of build section. In one of the script commands I set an environment variable as a system environment variable in the server machine as so:
setx Analysis_URL "http://analysis_url/analysis/%analysis_id%.html
My task now is to get this environment variable value back to Jenkins and include it in my post build notification email content. Is there a simple way to do it ?
In my research I have come across the plugin envInject but I think it is used for setting environment variables, is that right ?
UPDATE 1 :
It turned out that the variable could be accessed by a simple $Analysis_URL in the email content, however, that raised another issue as my environment variable changes its value after each job build, but as Jenkins only takes a copy of the system environment variables I keep getting the same variable value after each build in my email content, it only changes after restarting Jenkins. Is there a way to get the updated system environment variables to Jenkins ?
UPDATE 2 :
EnvInject plugin did the job I wanted. These are the steps that I performed:
Build step "batch command window": added command :
echo ANALYSIS_URL=$ANALYSIS_URL > my.properties
Build step "Inject environment variables": in field "Properies File Path"
$WORKSPACE/my.properties
Post-Build Actions: "Editable Email Notification", Field "Default Content":
Current analysis url: $ANALYSIS_URL
Have you tried the env.Analysis_URL to access to your environment variable ?
Printing Variables in Post Build Email:
Add this to build: execute shell section:
echo count=$count > count.txt
echo distinctcount=$distinctcount > distinctcount.txt
Add this to Inject Environmental Variables section under "Properties File Path":
${WORKSPACE}/count.txt
and do the same also for the second file ${WORKSPACE}/distinctcount.txt (as I have two variables)
Note 1: ${WORKSPACE} is pwd for jenkins and lists the location)
Note 2: You can actually put both variables in just one file by using echo distinctcount=$distinctcount >> count.txt
Call the variable in the "default content" section of "editable email notification":
Count total:$count Distinct Count: $distinctcount
NOTE
Be sure you have email setup as "always" under "advanced settings" at the bottom of the email settings, otherwise it only emails upon first success or any failure:
You can use environment variables and global variables inside script with sh. For example:
pipeline {
environment {
APP_VERSION = "1.2.1"
}
post {
success {
script {
sh "sed -i 's#%BUILD_URL#$BUILD_URL#g' .jenkins/email.html"
sh "sed -i 's#%APP_VERSION#${APP_VERSION}#g' .jenkins/email.html"
emailext attachLog: true, mimeType: 'text/html', body: '${FILE, path=".jenkins/email.html"}', subject: '[$BUILD_STATUS] $PROJECT_NAME', to: 'youremail#mail.com'
}
}
}
}
But the secret is here:
' single quote parses global variables like $BUILD_STATUS $BUILD_URL ...
" double quote parses env variables like ${APP_VERSION}
So, you can use:
subject: '$BUILD_STATUS'
or
subject: "${APP_VERSION}"
Final solution
Transform all global variables you need to environment variables:
pipeline {
environment {
APP_VERSION = "1.2.1"
}
post {
success {
script {
BUILD_STATUS = '$BUILD_STATUS'
PROJECT_NAME = '$PROJECT_NAME'
sh "sed -i 's#%BUILD_URL#$BUILD_URL#g' .jenkins/email.html"
sh "sed -i 's#%APP_VERSION#${APP_VERSION}#g' .jenkins/email.html"
emailext attachLog: true, mimeType: 'text/html', body: '${FILE, path=".jenkins/email.html"}', subject: "${BUILD_STATUS} ${PROJECT_NAME} ${APP_VERSION}", to: 'youremail#mail.com'
}
}
}
}
You can just use: ${yourDefinedVriableName}

Resources