在Jenkins管道中,如何在电子邮件正文中显示Groovy map的内容?

erhoui1w  于 12个月前  发布在  Jenkins
关注(0)|答案(1)|浏览(142)

我有一个Jenkins声明式管道,它执行并行阶段。每个阶段的退出代码被分配给Groovy mapresults,并打印到日志控制台:

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

字符串
如何在成功后阶段发送的通知电子邮件的正文中包含Map的名称?

nvbavucw

nvbavucw1#

在并行执行之后,您可以将结果转换为全局保存的字符串,最后在发送邮件时将结果字符串与消息内容连接在一起。
比如说:

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

字符串

相关问题