Jenkins Pipeline publish html report

Viewed 29799

Maven clean install generate new html file in following location

/var/lib/jenkins/workspace/Docs_LoadTest/target/jmeter/reports/DocsJmeterTests_20170601_151330/index.html

Here "DocsJmeterTests_20170601_151330" will change for every run. So i am trying to publish html report using publish html report plugin. Following is my Pipeline script

node {
build job: 'Docs_LoadTest'
stage('Results') {
publishHTML([allowMissing: false,
         alwaysLinkToLastBuild: true,
         keepAll: true,
         reportDir: 
        '/var/lib/jenkins/workspace/Docs_LoadTest/target/jmeter/reports/*/',
         reportFiles: 'index.html',
         reportName: 'Docs Loadtest Dashboard'
         ])

 }
 }

Getting following error while running the job

[htmlpublisher] Archiving HTML reports...
[htmlpublisher] Archiving at BUILD level /var/lib/jenkins/workspace/Docs_LoadTest/target/jmeter/reports/* to /var/lib/jenkins/jobs/Docs_Pipeline/builds/10/htmlreports/Docs_Loadtest_Dashboard
ERROR: Specified HTML directory '/var/lib/jenkins/workspace/Docs_LoadTest/target/jmeter/reports/*' does not exist.

Even we tried following options didnt worked

/var/lib/jenkins/workspace/Docs_LoadTest/target/jmeter/reports/**/ /var/lib/jenkins/workspace/Docs_LoadTest/target/jmeter/reports/DocsJmeterTests_* /var/lib/jenkins/workspace/Docs_LoadTest/target/jmeter/reports/DocsJmeterTests_* _*

4 Answers

I was having similar problem, only that I wanted to publish multiple reports.

What I ended up doing was I added simple groovy script to iterate through files in reports directory. You can use same/similar approach to get file name.

 stage('publish reports') {
        steps {
            unstash 'source'

            script {
                sh 'ls target/jmeter/reports > listFiles.txt'
                def files = readFile("listFiles.txt").split("\\r?\\n");
                sh 'rm -f listFiles.txt'

                for (i = 0; i < files.size(); i++) {
                    publishHTML target: [
                        allowMissing:false,
                        alwaysLinkToLastBuild: false,
                        keepAll:true,
                        reportDir: 'target/jmeter/reports/' + files[i],
                        reportFiles: 'index.html',
                        reportName: files[i]
                    ]
                }                   
            }           
        }
    }

Note: this example is used in declarative pipeline. Docs about readFile function.

I have tried simply the followings.

stage('Test-Junit') {
        steps {
            sh 'gradle test'
        }
        post {
            always {
                script {
                    def moduleNames = ["app", "core", ...]
                    for(i=0; i<moduleNames.size(); i++ ) {
                        publishHTML target: [
                            allowMissing:false,
                            alwaysLinkToLastBuild: false,
                            keepAll:true,
                            reportDir: moduleNames[i] + '/build/reports/tests/test',
                            reportFiles: 'index.html',
                            reportName: 'Test Report:' + moduleNames[i]
                        ]
                    }
                }   
            }
        }
    }

It will make all modules report and thus you can find them on left nav-bar of project dash-board.

It is not exactly the same scenario, but decided to publish my code because was really hard to understand, clarify and get documentation and accurate examples on how to publish different reports in just one final consolidated report, using the publishHTML plug-in for Jenkins.

A bit of background, we are executing different packages of testing, but some test cases can't run together because they could kill each other, so we need to execute, from the same code, in two different time frames due that we run test cases in parallel.

The solution was to execute by tags, so once the different execution using a Jenkins DSL - pipeline happens the builds produce just one report with different tabs on it.

So this is the final code that works for me:

pipeline {
    agent any
   
    stages {        
        stage('Git') {
            steps {
                git .....
            }
        }
        
        stage('Exec-1') {
            steps {
                bat 'mvn -B clean verify -Dcucumber.filter.tags=@exec1 -Dserenity.outputDirectory=reports/site/serenity/exec1'
        }

        stage('Exec-2') {
            steps {
                bat 'mvn -B clean verify -Dcucumber.filter.tags=@exec2 -Dserenity.outputDirectory=reports/site/serenity/exec2'
            }
        }
        
        stage('Exec-3') {
            steps {
                bat 'mvn -B clean verify -Dcucumber.filter.tags=@exec3 -Dserenity.outputDirectory=reports/site/serenity/exec3'
            }
        }
    }

    post {
        always {  
            publishHTML target: [
                reportName: 'Test',
                reportDir: 'reports/site/serenity',
                reportFiles: 'exec1/index.html, exec2/index.html, exec3/index.html', 
                reportTitles: 'Exec-1, Exec-2, Exec-3', 
                keepAll: true,
                alwaysLinkToLastBuild: true,
                allowMissing: false
            ]  
        }
    }
}
Related