I am using Jenkins and Emailext plugin for emailing. Here is a test pipeline, which works fine:
pipeline
{
agent any
stages
{
stage ('Start')
{
steps
{
echo 'Hello'
// error('Abort to test failure.')
}
}
}
post
{
success
{
emailext (
subject: '$DEFAULT_SUBJECT',
body: '$DEFAULT_CONTENT',
recipientProviders: [ requestor(), ? ]
)
}
failure
{
emailext (
subject: '$DEFAULT_SUBJECT',
body: '$DEFAULT_CONTENT',
recipientProviders: [developers(), requestor(), culprits()]
)
}
}
}
I would like to send success emails to requestor and to "default recipients" which I specified under Manage Jenkins -> Configure System -> Extended E-mail Notification.
How do I add "default recipients" ($DEFAULT_RECIPIENTS) to recipientProviders?
Try like this:
success
{
emailext (
subject: '$DEFAULT_SUBJECT',
body: '$DEFAULT_CONTENT',
to: '$DEFAULT_RECIPIENTS',
recipientProviders: [ requestor() ]
)
}
recipientProviders parameter is used to add additional recipients.
This will send the email to both recipientProviders and DEFAULT_RECIPIENTS whenever job is success.
Related
pipeline {
agent any
stages {
stage("Approval") {
steps {
script {
// Get the input in environment variable 'cmnt'
env.cmnt = input(
message: 'Do you want to approve the pipeline?',
parameters: [
text(defaultValue: '',
description: 'Enter your comment...',
name: 'COMMENT')
])
}
}
}
}
post{
success{
emailext (
subject: "Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' is approved.",
body: """<p>APPROVER COMMENT: '${env.cmnt}'</p>""",
recipientProviders: [[$class: 'DevelopersRecipientProvider']]
)
}
aborted{
emailext (
subject: "Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' is aborted by approver",
body: """<p>APPROVER COMMENT: '${env.cmnt}'</p>""",
recipientProviders: [[$class: 'DevelopersRecipientProvider']]
)
}
}
}
For success/Proceed button click, I am able to print comment inside email body whereas getting null as value in case of abort button click by approver at the time of approving the pipeline.
How to send allure or html report as an attachment in the Jenkins email notifications. This is example of the pipeline i am using in my Pipeline script in Jenkins. I have setup my email notifcations, however i want to get some sort of report in the email. Please note that providing a link is not enough because my tests are setup on a different machine with the reports.
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'echo "Fail!"; exit 1'
}
}
}
post {
always {
echo 'This will always run'
}
success {
echo 'This will run only if successful'
}
failure {
mail bcc: '', body: "<b>Example</b><br>Project: ${env.JOB_NAME} <br>Build Number: ${env.BUILD_NUMBER} <br> URL de build: ${env.BUILD_URL}", cc: '', charset: 'UTF-8', from: '', mimeType: 'text/html', replyTo: '', subject: "ERROR CI: Project name -> ${env.JOB_NAME}", to: "foo#foomail.com";
}
unstable {
echo 'This will run only if the run was marked as unstable'
}
changed {
echo 'This will run only if the state of the Pipeline has changed'
echo 'For example, if the Pipeline was previously failing but is now successful'
}
}
}
You should use emailext plugin not mail
failure {
emailext(
attachmentsPattern: "<path to report>",
body: '',
subject: "",
to: ""
)
}
i wanted to send an email to developers to do manual testing and a link to approve the build and then to do the build in production.Can anyone achieve this using jenkins
You can have a step on your pipeline as per below. They cannot approve it on the email, but you can send the URL of the job build on the email:
stage("Stage with input") {
steps {
def userInput = false
script {
// build your custom email message here
emailext (
subject: "Job: ${env.JOB_NAME} - ${env.BUILD_NUMBER}",
body: """Job ${env.JOB_NAME} [${env.BUILD_URL}]""",
recipientProviders: "someone#somewhere.com"
)
def userInput = input(id: 'Proceed1', message: 'Promote build?', parameters: [[$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Please confirm you agree with this']])
echo 'userInput: ' + userInput
if(userInput == true) {
// do action
} else {
// not do action
echo "Action was aborted."
}
}
}
}
Check here email ext plugin.
And here the input step.
I am using Jenkins pipeline to run a build.
How can I avoid code duplication for 2 post statuses that execute the same code (failure & unstable)?
example code snippet:
post {
failure
{
emailext(
attachmentsPattern: '**/log.txt',
body: "Something is wrong with ${env.BUILD_URL}",
subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
to: "test#test.gmail"
)
}
unstable
{
emailext(
attachmentsPattern: '**/log.txt',
body: "Something is wrong with ${env.BUILD_URL}",
subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
to: "test#test.gmail"
)
}
You can write a function and use it, f.e.
post {
failure
{
sendMail()
}
unstable
{
sendMail()
}
def sendMail() {
emailext(
attachmentsPattern: '**/log.txt',
body: "Something is wrong with ${env.BUILD_URL}",
subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
to: "test#test.gmail"
)
}
I am trying to attach the content of some specific log files in mail body while nofifying a job execution failiure via email.
stage("Checkout Fusion Source") {
parallel 'A': {
node('LinuxNode') {
try {
echo "Hello World(Linux)"
} catch (Exception e) {
mail body: 'Failed!',
subject: 'Job has failed in Linux!',
to: 'abc#xyz.com',
attachmentsPattern: '/path/to/log/file/log_linux.out'
}
mail body: 'Passed!',
subject: 'Job has passed in Linux!',
to: 'abc#xyz.com',
attachmentsPattern: '/path/to/log/file/log_linux.out'
}
}, 'B': {
node('AixNode') {
try {
echo "Hello World(AIX)"
} catch (Exception e) {
mail body: 'Failed!',
subject: 'Job has failed in AIX!',
to: 'abc#xyz.com',
attachmentsPattern: '/path/to/log/file/log_aix.out'
}
mail body: 'Passed!',
subject: 'Job has passed in AIX!',
to: 'abc#xyz.com',
attachmentsPattern: '/path/to/log/file/log_aix.out'
}
}
}
This attachmentsPattern is not helping for the same.
P.S. My Jenkins version is 2.46.3.
Install the email-extension plugin and try something like this in your pipeline workflow.
emailext attachLog: true, body: "${currentBuild.result}: ${BUILD_URL}", compressLog: true, replyTo: 'email#xxx.com',
subject: "Build Notification: ${JOB_NAME}-Build# ${BUILD_NUMBER} ${currentBuild.result}", to: 'email123#xxx.com'