Send an email when Jenkins has specific exception or error - jenkins

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.*/ }

Related

Jenkins - Change subject in email-ext plugin

I am using email-ext plugin to trigger emails after the build fails or become unstable. The build becomes unstable when some of the tests fail and the build fails when the compilation fail.
Can we use conditions to display different customized subject depending on the cause of the email trigger?
Yes, below the Editable email notification interface there should be an Advanced settings.. button which will give you access to various triggers. As you add & configure the triggers, you'll note an Advanced.. button for each of them, which will allow you to fully customize the e-mail template.

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.

How to have output of script emailed from 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.

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"

Provide userName and shortDescription in Jenkins remote API job triggering

I know how to provide build parameters:
wget --post-data='json={"parameter": {"name": "testparam", "value": "HELLO"}}' http://jenkins/job/Job1/build?delay=0sec
But, is it possible to provide a shortDescription and userName in a Jenkins remote API build request via wget/curl?
How should it look like in json or xml? Is there any manual/guidance on the net?
I will use this in along with the problem described in Trigger dynamic set of jobs. I want to provide triggered job with the calling job name and build number.
You may consider using Jenkins CLI (http://[jenkins-host]/cli for help in the browser). You can specify a user to a build CLI command. I'm not sure what you mean by short description when starting a build, though.
Update: Please see Jenkins Wiki Authenticating Scripted Clients. I've created a user foobar ('full name' Foo Bar) and tried the following:
wget --auth-no-challenge --http-user=foobar --http-password=[apiToken] http://jenkins.yourcompany.com/job/your_job/build
Where the token is obtained from user configuration page: http://localhost:8081/user/foobar/configure. It worked. The user has to exist, though. Also, you must specify --auth-no-challenge option, otherwise it kicks off the build as anonymous. The status description says Started by user Foo Bar.
Another Update If everything else fails, you may consider the following workaround: start all builds via the Parameterized Trigger Plugin with an additional boolean parameter that tells the triggered job whether to run or not. In case the job is asked not to run it would fail immediately and call a 'clean-up' job passing to it the build info; the clean-up job then will delete the build from the system.

Resources