Jenkins Pipeline publish html report
Asked Answered
C

4

10

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_* _*

Cusped answered 1/6, 2017 at 11:47 Comment(2)
Can you give a little more information about your jenkins setup? Are there multiple workers / slave nodes? Is your downstream job being triggered set to use a hardcoded workspace? Is the node label of your downstream job the same?Insurmountable
@Saagar, See my answer, it can help: https://mcmap.net/q/901538/-jenkins-could-not-generate-html-report-folder-on-slave-machineAmanita
S
12

The HTML Publisher plugin does not seem to understand wildcards. What you could do in your Pipeline is using Linux's copy command, since that can work with wildcards.

This copies over the contents of all directories in the [Docs_LoadTest]/jmeter/reports folder to a jmeter_results folder in the local workspace:

sh 'cp -r /var/lib/jenkins/workspace/Docs_LoadTest/target/jmeter/reports/*/. target/jmeter_results/'

Note that you must clean both your target folder in the Docs_LoadTest folder and your Pipeline in between runs, else multiple reports will be copied over with this solution.

A better solution:

Would be to apply this trick in the Docs_LoadTest and use the Publish Artifact and Copy Artifact features. This works around having to hardcode the path to the other job and will work even if the Pipeline executes on another slave than the Docs_LoadTest. This does require the Copy Artifacts plugin.

Assuming Docs_LoadTest is a Freestyle job:

  1. Add an Execute Shell Build step that copies the results to a fixed folder, e.g. jmeter_results:

    mkdir -p target/jmeter_results/ cp -r target/jmeter/reports/*/. target/jmeter_results/

  2. Then add an Archive Artifacts Post Build Archive Step with the following files to archive:

    target/jmeter_results/*

In your Pipeline:

  1. Use the Copy Artifact step to copy the files to target/jmeter_results folder in the local workspace:

    step ([$class: 'CopyArtifact', projectName: 'Docs_LoadTest', filter: 'target/jmeter_results/*']);

  2. Change the call to the HTML publisher to use this folder:

    publishHTML([allowMissing: false, alwaysLinkToLastBuild: true, keepAll: true, reportDir: 'target/jmeter_results', reportFiles: 'index.html', reportName: 'Docs Loadtest Dashboard' ])

Spice answered 1/6, 2017 at 15:32 Comment(0)
C
8

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.

Concertmaster answered 24/10, 2017 at 8:17 Comment(3)
This answer creates many menu entries. If you'd rather have a single menu entry with multiple tabs, you can something like this: reportDir: 'target/jmeter/reports', reportFiles: files.join(','), reportName: 'jMeter Reports'. User reportTitles to give pretty names to the tabs.Arvind
Hi @Raphael, but in this case there will be multiple folders beneath the target/jmeter/reports folder. I am afraid you may have to specify the report dir specifically as suggested in this answer.Leavening
@JamesSelvakumar @Arvind We can use multiple directory locations as a reportFiles so then it will create a single entry in the menu and all the report files added as tabs. linkPlainclothesman
S
1

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.

Stettin answered 26/2, 2020 at 7:3 Comment(0)
C
0

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
            ]  
        }
    }
}
Characterize answered 3/8, 2022 at 22:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.