How to have output of script emailed from Jenkins? - jenkins

I am setting up a Jenkins job that processes some data.
Once the job is finished, I would like to have jenkins run a script that describes the status of the recent processing and email the stdout of that script to a specified email address.
While I realize that it is possible to have the script send the email itself, I am wondering if there exists a more ideal way to have Jenkins send the output of that script. Is there a plugin that is idea for this?
Thanks.

To configure the Email-ext plugin:
To get a portion of the log in the mail, if not all, set the 'Extended E-mail Notification' to send an email for each build that failed, and use the $BUILD_LOG in the body of the message:
In the body of the message (below the '$DEFAULT_CONTENT' ) add the parameter $BUILD_LOG to display the end of the build log
Assuming the log is in plain-text, change 'Content Type' to Plain Text
For the list of built-in Jenkins parameters, go to this page:
http://[your-jenkins-server]/env-vars.html
Cheers
EDIT:
To get to that list of available env-vars via the UI,
there is a link at the Execute shell build-step, for example -

I'm not sure if this is exactly what you're looking for, but the Email-ext plugin for Jenkins sounds like it may do what you want.

Related

Sending robot framework log file as email content via Jenkins is not getting displayed

I have created a Jenkins job to execute robot framework. The results[log.html and report.html] of the Jenkins are getting displayed properly.
Now I am using the email-ext plugin to send the log file as email content to the specified group of users, but the received email doesn't display the content and shows error as "Opening Robot Framework log failed" in the email.
I have set the email-ext plugin as follows:
I wish to send the robot results as email content, not as an attachment, I have tried various methods like Linux, shell scripting etc, but still, it doesn't work. How can the log.html email content can be sent to the email body?
You will not be able to just put the entire content of the robot log.html in an email because it contains javascript.
I've been looking into this the last few days as well and have come to see that using a groovy based template using the jenkins Email-ext plugin is the way to go. Googling for 'jenkins robot framework email template' shows a few hits and one that I'm planning to try and then modify to my liking was this one:
https://github.com/vladwa/robot-email-template
From this I found that we don't have access to all of the test info built in, so I wrote up some modifications to load the robot output.xml as an artifact to then be able to inject any data in that file into the html email report. Here's the gist of that:
https://gist.github.com/harbdog/070f0be66ebae343d6d11e57a6c6fc08
Here's what it looks like for example:
You have to configure publish robot framework test result <post build> and then use {ROBOT_REPORTLINK} in email content section

Send an email when Jenkins has specific exception or error

On Jenkins sometimes errors occurs but I want notify different people depending the error.
IE: When SSH command do an exception I want don't send email or send to a dummy account and when a production check fail send to me. (all this happen in one task)
is it possible? how?
thanks in advance
Use Jenkins Email-ext plugin.
It has the complete flexibility for recipients (and content) based on many conditions, including log content, which allows to analyze which command has failed and change the recipient list accordingly.
Because you don't provide much details about your task and what exactly is "SSH do an exception" or "production check", it is hard to be very specific, but you can, for example, examine log for "production check has failed" or any other desired text and allow or suppress email based on this examination.
Install the plugin
Select the checkbox labeled "Editable Email Notification" in the "Post-build Actions" section.
Leave "Failure - Any" trigger if you want to prrocess mail configuration for any failure; otherwise, delete failure and add desired triggers from the dropdown
There are multiple ways to allow or cancel email sending, the complete description is in documentation on the plugin page. The simplest way is to use Pre-send Script feature. From documentation section Advanced configuration / Pre-send Script:
You may also cancel sending the email by setting the boolean variable "cancel" to true.
In Pre-send Script add a Groovy script to evaluate a condition and set a variable cancel to true or false in order to send or prevent email. For example, based on "production check has failed" line existense in the build log:
cancel = build.logFile.text.readLines().any { it =~ /.*production check has failed.*/ }

Jenkins email-ext trigger e-mail for culprits not working as expected

I'm trying to send e-mail to culprits when the build fails. If I start the build manually the e-mail is sent, but If I start the build through SCM polling e-mail is not send and the console output for the build shows the message:
An attempt to send an e-mail to empty list of recipients, ignored.
Is that the right way the plugin should work (culprit should be a person, not scm polling)?
Im using:
OS: Ubuntu Server 11.04
Jekins: 1.532.1
Email-ext plugin: 2.37.2
Currently (before the next release 2.38) when you want to send emails to the culprits you also need to check the "Developers" checkbox. Without it, the culprits portion will not be used at all.
This will be different going forward, adding Culprits as a recipient list will do what is expected.

Jenkins Email-ext plugin - tokens

Hi I'm using Jenkins Email-ext plugin.
Where can I find a list of all the things I can refer to in the html content of the email?
For instance: I recently found out that you can put parameters values inside the mail using:
${ENV,var="variable_name"}
But say that I want to get build log or build number or build status (passed fail).
Do you know if there is a document listing all data that is referable in this plugin.
Thanks!!
On job configurations page:
Check 'Editable Email Notification' box -> find 'Content Token Reference' (it's between 'Attachments' and 'Trigger for matrix projects') -> Click on ? to the right side of it.

Jenkins - Trigger email based on input parameter

I have several Jenkins jobs where I want an email to be triggered (or not trigger) based on an input parameter.
The use case is that I have one deploy job per project, but that job is parametrized for each environment. We you run the job you can select which environment to deploy to (Dev1, Dev2, QA, etc). Ideally I would like to send out notification emails when a new build is sent to QA, but I do not want to send out notification emails when a new build is sent to Dev, because that happens all the time (with every developer commit) and would flood email boxes.
I've tried googling but haven't yet found a solution. I am currently using the email-ext plugin.
Thanks.
It is a very nasty way to solve the problem, but if you cannot do it by any other means...
Create a dependent job that exists primarily to pass / fail based on the value of the parameter, which would get passed as a parameter to the job. Then you could chain the decision the job made to the email notification.
Of course, if you were going to do this, it would probably be better to just write a small parametrized email sender using javax.mail and have Jenkins run it by "building" an ANT project that actually calls the "java" task. Then you have your own email sender (full control) and could make it a dependent job on the other tasks.
I hope someone chimes in with a simpler way...
In email-ext you can add a "Script - Before Build" or a "Script - After Build" trigger which will trigger on whether the specified groovy script returns true or false.
The help for the script reads:
Use this area to implement your own trigger logic in groovy. The last line will be evaluated as a boolean to determine if the trigger should cause an email to be sent or now.
They don't give many details of what's available in the script, but from the source code on Github, it looks like you have "build" and "project" at least, and some common imports done for you:
cc.addCompilationCustomizers(new ImportCustomizer().addStarImports(
"jenkins",
"jenkins.model",
"hudson",
"hudson.model"));
Binding binding = new Binding();
binding.setVariable("build", build);
binding.setVariable("project", build.getParent());
binding.setVariable("rooturl", JenkinsLocationConfiguration.get().getUrl());
binding.setVariable("out", listener.getLogger());
Caveat, I haven't tried this, but this should work as an example script:
build.buildVariables.get("MY_DEPLOYMENT_ENV") == "QA"
that should get the value of a String or Choice parameter you've created called "MY_DEPLOYMENT_ENV" and trigger the email if the current value is "QA"

Resources