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

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

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 send email with csv file attachment in jenkinsfile (Groovy Script)?

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

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.

Jenkinsfile - Post action avoid code duplication for statuses

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

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