How to send email with csv file attachment in jenkinsfile (Groovy Script)? - jenkins

please help me to attach and send csv file along with email in groovy script .
right now i have below code , when i was changed csv instead of zip , facing error like ,
no DSL method ,
pipeline {
agent any
stages {
stage('Testing') {
steps{
bat "del test.zip"
zip zipFile: 'test.zip', archive: false, dir: 'directory pattern as per your structure'
}
}
}
post {
failure {
emailext attachmentsPattern: 'test.zip', body: '''${SCRIPT, template="groovy-html.template"}''',
subject: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - Failed",
mimeType: 'text/html',to: "email id"
}
success {
emailext attachmentsPattern: 'test.zip', body: '''${SCRIPT, template="groovy-html.template"}''',
subject: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - Successful",
mimeType: 'text/html',to: "email id"
}
}
}

Related

How to send the contents of Jenkins workspace as email attachment following the run

How can the contents of Jenkins build workspace be sent as an email attachment, following a test run?. Below is my declarative pipeline code snippet. I can see the folders and files in the workspace following pipeline run, but it doesn't attach except the build log:-
stage('Send Test Report') {
steps {
script {
def testEmailGroup = "saba#abc.com"
// Test teams email
emailext(
subject: "STARTED: jOB '${env.JOB_NAME} [${env.BUILD_NUMBER}]' RESULT: ${currentBuild.currentResult}",
attachLog: true, attachmentsPattern: "**/${WORKSPACE}/*.zip",compressLog: true,
body: "Check console output at ${env.BUILD_URL}" ,
to: testEmailGroup)
}
}
}
You first need to drop workspace contents into a file, here is a snippet similar of what we have on our Jenkins:
stages {
stage('CreateAndSendReport') {
steps {
sh 'echo "Contents" > contents.txt'
}
}
}
post {
always {
archiveArtifacts artifacts: 'contents.txt', onlyIfSuccessful: true
emailext attachLog: true, attachmentsPattern: 'contents.txt',
body: "${currentBuild.currentResult}: Job ${env.JOB_NAME} build ${env.BUILD_NUMBER}\n Link: ${env.BUILD_URL}",
recipientProviders: [developers(), requestor()],
subject: "Jenkins Build Report ${currentBuild.currentResult}: Job ${env.JOB_NAME}"
}
}

How to add "default recipients" ($DEFAULT_RECIPIENTS) to recipientProviders in Jenkins EmailExt?

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.

How to send allure or html report as an attachment in the Jenkins email notifications

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: ""
)
}

emailext attachmentsPattern: '**/target/overview-features.html' gives exception

emailext attachmentsPattern: '**/target/overview-features.html' is giving Error in Jenkinsfile in Maven Project
post {
success {
emailext
attachmentsPattern: "**/overview-features.html",
body: '''${SCRIPT, template="groovy-html.template"}''',
mimeType: 'text/html',
to: "test#gmail.com",
subject: "Success Pipeline: ${currentBuild.fullDisplayName}"
}
}

Attach log files in notification mail in jenkins pipeline workflow

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'

Resources