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.
Related
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'
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
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'
My Jenkinsfile looks like:
try {
sh 'exit 1'
stage("Test") {
sh "node -v"
sh "npm prune"
sh "npm install"
sh "npm test"
}
...
} catch(e) {
currentBuild.result = 'FAILURE'
emailext(body: '${DEFAULT_CONTENT}', mimeType: 'text/html',
replyTo: '$DEFAULT_REPLYTO', subject: '${DEFAULT_SUBJECT}',
to: emailextrecipients([[$class: 'CulpritsRecipientProvider'],
[$class: 'DevelopersRecipientProvider'],
[$class: 'RequesterRecipientProvider']]))
throw e
}
I want to send email only on build failures.
I receive following :
Not sending mail to unregistered user my_primary_github_email#gmail.com
Actually, I have 2 questions.
Is there way to allow sending emails to unregistered users?
Is there way to configure Jenkins to send email to recipient address configured for that repository organization?
For example:
I have 2 emails configured in Github, primary email and secondary email, the second configured to receive emails for organizations`s repositories.
So if the build belongs to organization, I want Jenkins to send email to address configured for that organization and not to primary address.
Add the below line in your jenkins startup script
-Dhudson.tasks.MailSender.SEND_TO_UNKNOWN_USERS=true
The newest Jenkins security directive only allow sending mail to registered user. The line above bypass this configuration.
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 "<a href='${BUILD_URL}consoleText'>${JOB_NAME} [${BUILD_NUMBER}]</a>"</p>
${BUILD_LOG_REGEX, regex="^.*?*****.*?$", linesBefore=0, linesAfter=999, maxMatches=10, showTruncatedLines=false,escapeHtml=false}''',