email on jenkins user input - jenkins

I have a jenkins pipeline where I wait for user input to proceed or abort. Is there any way I can send the same in email to let the concerned person aware and he can click on the email to procceed Or abort the pipeline.?

It is possible to execute such action by using Jenkins API, this can be achieved using Jenkins Rest Api you can check this SO question.
I haven't tried this solution yet but I guess this pipeline could help you get a better idea of how I would think about implementing such behavior in a pipeline like this:
pipeline {
agent any
stages {
stage('Mail Notification') {
steps {
echo 'Sending Mail'
mail bcc: '',
body: 'Stop job through this link: ${env.BUILD_URL}/job/${env.JOB_NAME}/${env.BUILD_NUMBER}/stop',
cc: '',
from: '',
replyTo: '',
subject: 'Jenkins Job',
to: 'example#domain.com'
}
}
}
}
Jenkins pipeline is aware of such variables like ${env.BUILD_URL} ${env.JOB_NAME} ${env.BUILD_NUMBER}

Related

Jenkins - sending email depends on console output

I have an app in Java and I'd like to run the app with Jenkins.
The app could log info "[Email] example log" or throw a specific exception in some cases.
Now I'd like to use Jenkins to send e-mails after build when Jenkins see in its Console output:
log: [Email] .....
specific exception has been throw
I configured Editable Email Notification and Emailext (all email parameters like smtp etc are done) and I can add it to Post-build Action
[1]: https://i.stack.imgur.com/8psbf.png
but I never used Jenkins before and I don't know which trigger should I choose in that case and where/how to write 'if code' to do what I want.
EDIT: I should probably write some Pre-send Script?
If you would like to send email, then you can directly add it in the pipeline.
Configure System:
Go to Manage Jenkins-> Configure System. Here scroll down to the email notification section. If you are using Gmail then type smtp.gmail.com for the SMTP server. Click on Advanced and select Use SMTP authentication. Enter your Gmail username and password. Select the Use SSL option and enter the port number as 465. Click on Apply and then Save.
Create Jenkins Pipeline Job:
pipeline {
agent any
stages {
stage("Send Email")
{
steps {
emailext body: 'your body', subject: 'your sububject', to: 'abc#gmail.co,'
}
}
}
}
Above method was an example to show you how you can send email via pipeline.
Execute your App and then send Email:
pipeline {
agent any
stages {
stage("App execution")
{
steps {
// Execute your app
bat label: 'Execute your app', script:"yourapp.bat >log.txt "
}
}
stage("Send Email")
{
steps {
emailext body: 'your body', subject: 'your sububject', to: 'abc#gmail.co,'
}
}
}
}
Send email based on conditions
pipeline {
agent any
stages {
stage("App execution")
{
steps {
// Execute your app
bat label: 'Execute your app', script:"yourapp.bat >log.txt "
}
}
stage("Send Email")
{
steps {
// You can add condition based on your log file in previous step :
// Example : please change below code as per your logic
// if log contains "Email" then only send email
if(log.contains(" Email:")){
emailext body: 'your body', subject: 'your subject', to: 'abc#gmail.co,'
}
}
}
}
}

Answer Jenkins Input from slack

I am working currently on integration between Jenkins to Slack,
I want to fully control Jenkins from slack, basically, I want to trigger jobs, and I want to answer input if it exists.
for e.g
pipeline{
agent any
stages{
stage('Test Notification success stage'){
steps{
script{
env.createofflinepkg = input message: 'User input required',
ok: 'Submit',
parameters: [choice(name: 'Create Offline Package', choices: "Create\nSkip", description: 'Create Offline Package or Skip')]
}
slackSend (channel: 'input-response',color: '#ffff00', message: "Yellow at general : Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}
}
I want this to be sent to slack and then I could answer this from slack, there is a way to do this?
Thanks in Advance.
I don't know if this is exactly what you are after. If you are sending ${env.BUILD_URL}/input URL to slack, that should redirect you directly to the approval page from Slack to Jenkins approval page.

How to send post build notification to more than one recepient in a Jenkins pipeline

I'm setting up a Jenkins pipeline wherein I want to send post build notification to more than one receipents. i'm not able to find how to set "CC", Can someone help.
An example of my pipeline is as under:
pipeline {
agent any
stages {
stage('No-op') {
steps {
sh 'ls'
}
}
}
post {
failure {
mail to: 'team#example.com',
subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
body: "Something is wrong with ${env.BUILD_URL}"
}
}
}
The above example is working fine for me, but i want to modify the following line to send notification to multiple people (preferably in CC):
mail to: 'team#example.com',
I'm using Jenkins ver. 2.41
I don't know if you can CC them, but to send to multiple recipients try using a comma-delimited list:
mail to: 'team#example.com,blah#example.com',
Use Snippet generator to explore more options regarding most of the steps. You can CC or even BCC like:
Pipeline Command :
mail bcc: 'foo#example.com', body: 'Test CC Pipeline', cc: 'xyz#example.com', from: '', replyTo: '', subject: 'Testing CC', to: 'abc#example.com'
For the emailext script, use cc within to section as below:
emailext body: 'testing',subject: 'testing', to: 'xyz#example.com,cc:abc#example.com'
reference: https://issues.jenkins-ci.org/plugins/servlet/mobile#issus/JENKINS-6703
For scripted pipeline:
mail bcc: '', body: 'Build Report', cc: '', from: '', replyTo: '', subject: 'Build Finished', to: 'example1h#xyz.com,example2#xyz.com'
I also had trouble with 'cc:' failing to send out emails. I used the 'to:' line and specified multiple users. You can do this with variables as well, if you have a list of emails you want to pull in. For example, I declared primaryOwnerEmail and secondaryOwners [list of emails] and pulled them in the 'to:' line below.
stage ("Notify Developers of Success"){
env.ForEmailPlugin = env.WORKSPACE
emailext attachmentsPattern: 'Checkmarx\\Reports\\*.pdf',
to: "${primaryOwnerEmail},${secondaryOwners}",
subject: "${env.JOB_NAME} - (${env.BUILD_NUMBER}) Finished Successfuly!",
body: "Check console output at ${env.BUILD_URL} to view the results"
}
For multiple receivers, either you can add them to to section or under cc section.
For mail pipeline syntax, see below ex :
mail to: 'user1h#domain.com, user2#domain.com', cc: 'cc1#domain.com, cc2#domain.com', bcc: '', body: 'your_body_content', from: '', replyTo: '', subject: 'your_subject_line'
For emailext pipeline syntax, append cc within to see below:
emailext to: '''user1h#domain.com, user2#domain.com, cc:cc1#domain.com, cc:cc2#domain.com''', body: 'your_body_content', from: 'sender#domain.com', subject: 'your_subject_line',

How to invoke email-ext plugin from Jenkins declarative script?

I am writing a simple Jenkins declarative script to run 'make' and send an email with the result (success/failure).
I can send a simple email using:
post {
success {
mail to:"myname#me.com", subject:"${currentBuild.fullDisplayName} - Failed!", body: "Success!"
}
failure {
mail to:"myname#me.com", subject:"${currentBuild.fullDisplayName} - Failed!", body: "Failure!"
}
}
The resulting email is rather simplistic.
How can I call the email-ext plugin from the script to send an old-style post-build email? (I guess this should use email-ext's groovy-text.template).
I would like to be able to access lists like CulpritsRecipientProvider and to include the tail of the console log.
You can use it in this way:
emailext (
subject: "STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: """<p>STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
<p>Check console output at &QUOT;<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>&QUOT;</p>""",
recipientProviders: [[$class: 'DevelopersRecipientProvider']]
)
For more information you can check:
Sending Notifications in Pipeline
Email Extension Plugin

Pipeline Input step, on abort send email

I haven't seen a way to capture an abort on the input option in the pipeline
Or a post pipeline step to send an email. I'm assuming this requires some groovy.
I assume that you have a jenkinsfile or a pipeline script with an input step in it.
try {
input message: "Send deployment request?"
} catch (err) {
mail body: "Not a deployment request", to: "support#email.dot", from: "me#me.com", subject:"testando"
}

Resources