Copy published HTML from one Jenkins job directly to another job using Groovy URL Class

Posted on August 27th, 2018 at 18:00 PM

Most workhorse build or test jobs can produce local HTML reports using Jenkins's HTML publisher plugin. Groovy provides an easy way to access these remote reports from another job by exploiting the URL Class eliminating communication code overhead. A simple stage from the GenTable pipeline job that publishes an HTML table is shown below to illustrate the method

    def outhtml = '<table>'
    outhtml += '<tr><td>1</td><td>2</td></tr>'
    outhtml += '<tr><td>3</td><td>4</td></tr>'
    outhtml += '<tr><td>5</td><td>6</td></tr>'
    outhtml += '</table>'

    def filename = 'tablereport.html'
    writeFile file: filename, text: outhtml
    publishHTML (target: [
        allowMissing: false,
        alwaysLinkToLastBuild: false,
        keepAll: true,
        reportDir: '',
        reportFiles: filename,
        reportName: 'Table Report'

The report appears in the GenTable job build page as shown

A separate reading job can use toURL() to grab the remote report from GenTable as shown

    // get remote html 
    def url = "http://jenkins.yourdomain:8080/view/View1/view/View2/job/GenTable/Table_Report/tablereport.html".toURL()
    println url.text

    // simple parse of table contents
    def utext = url.text 
    utext = utext.replaceAll('<table>','\n')
    utext = utext.replaceAll('<tr><td>','')
    utext = utext.replaceAll('</td><td>',' ')
    utext = utext.replaceAll('</td></tr>','\n')
    utext = utext.replaceAll('</table>','')
    println utext     

The ReadReport console looks like this

[Pipeline] node
Running on yournode in /home/jenkins/workspace/ReadTable
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Read)
[Pipeline] echo
[Pipeline] echo
1 2
3 4
5 6
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

The lame parsing code is just there to show that url.text is just a normal string and can be manipulated in any way you want or just embedded in a local report