Search code examples
jenkinsgroovyencodingjenkins-pipelinejenkins-groovy

Jenkins get Build Log without the Color Encoding


I want to send the build log as an attachment post build but its not readable since alot of the text is encoded in base64 related: How to decrypt Jenkins 8mha values

Since this jenkins instance is pretty much always in production i cannot install plugins or add dependencies. Is there anyway to get readable text from the jenkins log?

I read that you can make an API call to jenkins to get the text from the site, which i will try but after that i am pretty lost.

Any Jenkins masters have suggestions?

instead of [8mha:AAAAqR+LCAAAAAAAAP9b85aBtbiIQSajNKU4P08vOT+vOD8nVc+jsiC1KCczL9svvyTVzHb1RttJBUeZGJg8GdhyUvPSSzJ8GJhLi3JKGIR8shLLEvVzEvPS9YNLijLz0q0rihik0IxzhtAgwxgggJGJgaGiAMiQL2HQ0c/KT9J3zkhNzs4vLVE1MtAFYpfEkkQg5ZuYl5ieWgRkBbgF65sa6QMAtVXQDbEAAAA=[0m

i am trying to get readable text like: Started by user ....


Solution

  • So turns out you dont need to decode it because the actual useful information is at the end. Solution: Define a regex pattern and matcher -> filter the string against that pattern and remove everything inside it.

    Unfortunately retrieving the buildLog in the post step is not ideal because it doenst include all lines ...

    def cleanLog(inputString) {
        def pattern = /\u001B\[8mha:(.*?)\u001B\[0m/
        def matcher = (inputString =~ pattern)
    
        def result = inputString
    
        while (matcher.find()) {
            def substringToRemove = matcher.group(0)
            result = result.replace(substringToRemove, "")
        }
    
        return result
    }