简体   繁体   中英

Get Jenkins build failure cause in groovy script

I am Jenkins beginner. I want to inject 'reason behind build failure' as an environment variable through groovy script. How to get build failure cause in groovy script?

// Get build variable value as per your jenkins jobs, I am taking one job out of all jobs in my organistaion in this example, You can follow your own jenkins methods to get the build number details to variable build we are using and add BUILD_STRING in function reason as per your generic errors.


def jobName = Hudson.instance.getJob('<ORG_NAME>').getItems()[0]

println jobName

def joblist = jobName.getItems()


println joblist[0]

def build = joblist[0].getLastBuild()

println build

println "Reason is : " 
reason(build)


def reason(build) {
    
                     
                          def BUILD_STRING1 = "Caused:"
                          def BUILD_STRING2 = "ERROR: "

                                build.getLog().eachLine { line ->
                                        if (line =~ /$BUILD_STRING1/ || line =~ /$BUILD_STRING2/ ) {
                                            println "error: $line"  }
                                                          }

                         }


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM