4

I want to send email after post build action in jenkins. Hence I have write jenkinsfile as follows. But I need some groovy script for 1. Attachment for zip file 2. Before attaching the file , I need to convert the folder to zip format.

Note: Please don't suggest email plugin procedure & configuration. I preferred Jenkins file method configuration

 pipeline {
    agent any
    stages {
        stage('Testing') {
            steps {
                sh 'chmod +x mvnw'
                sh './mvnw clean verify serenity:aggregate'
            }
        }
    }
    post {
        failure {
            script {
                mail (to: 'email@gmail.com',
                        subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) failed",
                        body: "Please visit ${env.BUILD_URL} for further information"
                );
                }
            }
         success {
             script {
                mail (to: 'email@gmail.com',
                        subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) success.",
                        body: "Please visit ${env.BUILD_URL} for further information.",


                  );
                }
          }      
    }
}
Amit Nanaware
  • 3,203
  • 1
  • 6
  • 19
ARJUN
  • 51
  • 1
  • 2
  • 8

1 Answers1

7

you need to use jekins zip utility to create zip file for the folder and then use the emailext plugin to send email with attachment see below example:

 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"
          }      
    }
}
Amit Nanaware
  • 3,203
  • 1
  • 6
  • 19
  • Thanks for quick response Amit. Suppose if my test results are present in target folder and I want to zip target.zip. Then what is changes to above code. – ARJUN Jan 09 '19 at 12:25
  • Also please explain about dir in above script – ARJUN Jan 09 '19 at 12:31
  • dir is the directory path pattern to your folder in workspace, which you want to zip e.g. `dir : 'target'` – Amit Nanaware Jan 09 '19 at 13:08
  • Thanks a lot amit. its working fine now. I have one more small question, How to create a variables in Jenkins file and how to use across Jenkins file. – ARJUN Jan 10 '19 at 10:54
  • what will be changes in I just want to send output.text file? do I need to zip it or can I send it without zipping – Ashish Karpe Jan 05 '23 at 09:56
  • I got ERROR: Error accessing files to attach: Expecting Ant GLOB pattern, but saw '/tmp/output.text' – Ashish Karpe Jan 05 '23 at 09:57