I am trying to find solution for parsing logs from jenkins console and add matched logs into email body and send it via emailext.
I have following log lines in my console. I need to extract all matching lines and add in message body
OPERATION=backup|ENV_NAME=http://example.com:8091|EVENT=backup|START_TIME=2022-11-10 09:20:28.897461|END_TIME=2022-11-10 09:20:30.824839|RESULT=SUCCESS
I have similar log lines on console. all of them will have one common key-value that is "OPERATION=backup" and all the keys . I need to extract all lines having "OPERATION=backup|"
I have tried following but unable to parse it and add in message body .
success {
script {
def operations = manager.getLogMatcher ("(.*)OPERATION=backup(.*)")
}
emailext attachLog: false, mimeType: 'text/plain', subject: "DB backup for '${envNameAlias}' having build #${currentBuild.number}: ${currentBuild.currentResult}",
body: "Job completed..\nSee logs for more info:\n${env.BUILD_URL}/console",
to: 'abc.xyz#ey.org'
}
I am unclear about the steps forward. Please help with some inputs.
While going through belly of stackoverflow for solution, i found many solutions working with groovy post-build plugin, which is not installed in our organisation's jenkins.
solution :
tried following, works partially, i am getting almost whole log now in email then just few lines
success {
emailext attachLog: false, mimeType: 'text/plain', attachmentsPattern: '*', subject: "Couchbase export for build #${currentBuild.number}: ${currentBuild.currentResult}",
to: 'abc.xyz#ey.org',
body: """Job completed..\nSee logs for more info:\n ${env.BUILD_URL}/console \n \n \${BUILD_LOG_REGEX, regex="OPERATION=backup|", linesBefore=0, linesAfter=5, maxMatches=5, showTruncatedLines=false, escapeHtml=true} , regards Jenkins"""
}
Related
I'm using the email Extension plugin for Jenkins 2.332.3 but when I get errors with the configuration of the email Extension my build still goes successful even if I get for example
AuthenticationFailedException message: 535 5.7.3 Authentication unsuccessful
Is there a way to get build failure if I have incorrect configurations of emailext?
My stage:
emailext(
attachmentsPattern: "test.txt",
subject: "Test",
body: "Example test",
replyTo: 'test#test.com'
)
When I have configurations valid I get
DEBUG SMTP: message successfully delivered to mail server
I briefly checked the source code of the plugin and this doesn't seem doable. All the errors are caught and handled gracefully. Check here. Also, the execution of the plugin doesn't even return a status code. Check here.
So AFAIU I don't see a way to know whether the email was sent.
Update
After thoroughly checking the code I observed that there is an option to execute a postscript after sending a mail. This script can be specified globally or within the pipeline. So I came up with a very hacky solution with some groovy. Basically, after sending the mail you can capture the response from the SMTP server. This response will have some details you can use to determine whether it's a success or a failure.
On success, you will see a response like the one below.(I used Gmail SMTP server here)
250 2.0.0 OK 1655253667 cc23-20020a05622a411700b00304f98ad3c1sm7772402qtb.29 - gsmtp
On Authentication failure, you should see something like the below.
535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials q18-20020a05622a04d200b002f906fc8530sm8752555qtx.46 - gsmtp
As I mentioned earlier the pipeline simply swallows all the errors that are thrown and from the script, there is no way to access the current build context. Hence as a workaround, I wrote the response to a file and then marked the status of the Job based on this. Please refer to the following pipeline.
pipeline {
agent any
stages {
stage('MailAndFail') {
steps {
script{
echo "Starting Mailing"
def script = "String response = transport.getLastServerResponse();println \"Mail Response: \" + response;File file = new File(\"/var/jenkins_home/workspace/EMAIEXT2222/MailResponse.txt\");file.write response"
emailext(
to: "test#gmail.com",
subject: "Test",
body: "Example test",
replyTo: 'test#test.com',
postsendScript: "$script"
)
sh "cat MailResponse.txt"
def response = readFile(file: 'MailResponse.txt')
if(!response.contains("2.0.0 OK")) {
echo "BUILD FAILURE!!!!"
currentBuild.result = 'FAILURE'
}
}
}
}
}
}
Note: Make sure you change the file write path to a directory in the workspace /var/jenkins_home/workspace/EMAIEXT2222. Also, the print statements in the script will not be shown in the build console. You can see them in the Jenkins log. Also, make sure you approve the groovy script if you get an error that says the script doesn't have permission to execute. You can do this from here: http://JENKINSHOST/scriptApproval/
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.
I am new to Jenkins. As of now I have added email notification plugin. So once my test is completed now I am getting an email with Build log.
If I want to send Load runner HTML analysis report to mail in the same, what is the procedure?
Can anyone help me here ?
Thanks.
It's possible and it's pretty simple.
Download EmailExt plug-in (https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin)
Then you can use the Pipeline Syntax to easily create it.
However, here's an example your can see as a reference to what you need.
emailext attachmentsPattern: '**/*.html', body: 'Body here', subject: 'Subject here', to: 'mail#gmail.com, mail2#gmail.com, mail3#gmail.com'
attachmentsPattern uses Ant File Syntax (http://ant.apache.org/manual/dirtasks.html)
I don't know if this will help, but i'm using https://wiki.jenkins.io/display/JENKINS/HTML+Publisher+Plugin
to generate html report from the jenkins workspace ( pwd() )
stage('sysdig report'){
publishHTML(target: [
allowMissing : true,
alwaysLinkToLastBuild: false,
keepAll : true,
reportDir : "${workspace}/",
reportFiles : "sysdig-report.html",
reportName : "sysdig-report"
])
You should be able to send the file after.
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.
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".