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

Viewed 31

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

        }
1 Answers

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