Search code examples
jenkinsgroovyjenkins-pipelinejenkins-groovy

In Jenkins pipeline, how to display a Groovy map's contents in an email body?


I have a Jenkins declarative pipeline that executes parallel stages. The exit code of each stage is assigned to Groovy map results and printed to the log console:

def p = [:]         // map for parallel stages
def results = [:]   // map of exit code for each stage

pipeline {

    stages {
        stage('run') {
            steps {
                script {
                    for (file in files_list) {
                        p[file] = {
                            node(myagent) {
                                results[file] = sh returnStatus: true, script:'./myApp $file'
                            }
                        }
                    }

                    parallel p

                    // Print the results
                    for (def key in results.keySet()) {
                        println "file = ${key}, exit code = ${results[key]}"
                    }
                }
            }
        }  
    }

    post {
        success {
            script { emailext (subject: "${env.JOB_NAME}: Build #${env.BUILD_NUMBER} - Successful!",
                                body: '${DEFAULT_CONTENT}',
                                recipientProviders: [buildUser()])
            }
        }
    }
}

How might I include the printout of the map in the body of the notification email sent by the success post-stage?


Solution

  • After your parallel execution, you can iterate over the results, convert them to a string which will be save globally, and finally when sending the mail join the result strings together with the content of your message.
    Something like:

           
    results = ["Execution Results:"]  // List of all the accumulated results
    
    pipeline {
        stages {
            stage('run') {
                steps {
                    script {
                        def outputs = [:]
                        parallel files_list.collectEntries { file ->
                            ["${file}": {
                                node(myagent) {
                                    outputs[file] = sh returnStatus: true, script:'./myApp $file'
                                }
                            }]
                        }
    
                        // Print and accumulate the results
                        outputs.each { file, exitCode ->
                            def line = "file = ${file}, exit code = ${exitCode}"
                            println(line) 
                            results.add(line)
                        }
                    }
                }
            }  
        }
    
        post {
            success {
                emailext subject: "${env.JOB_NAME}: Build #${env.BUILD_NUMBER} - Successful!",
                         body: '${DEFAULT_CONTENT}' + results.join('\n'),  // Join all results item with a new line
                         recipientProviders: [buildUser()])
                }
            }
        }
    }