简体   繁体   English

Jenkins 管道发布 html 报告

[英]Jenkins Pipeline publish html report

Maven clean install generate new html file in following location Maven 全新安装在以下位置生成新的 html 文件

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

Here "DocsJmeterTests_20170601_151330" will change for every run.这里的“DocsJmeterTests_20170601_151330”每次运行都会改变。 So i am trying to publish html report using publish html report plugin.所以我正在尝试使用发布 html 报告插件发布 html 报告。 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] 归档 HTML 报告...
[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 [htmlpublisher] 在 BUILD 级别归档 /var/lib/jenkins/workspace/Docs_LoadTest/target/jmeter/reports/* 到 /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.错误:指定的 HTML 目录“/var/lib/jenkins/workspace/Docs_LoadTest/target/jmeter/reports/*”不存在。

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_* _* /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/目标/jmeter/reports/DocsJmeterTests_* _*

The HTML Publisher plugin does not seem to understand wildcards. HTML Publisher 插件似乎无法理解通配符。 What you could do in your Pipeline is using Linux's copy command, since that can work with wildcards.你可以在你的流水线中做的是使用 Linux 的 copy 命令,因为它可以使用通配符。

This copies over the contents of all directories in the [Docs_LoadTest]/jmeter/reports folder to a jmeter_results folder in the local workspace:[Docs_LoadTest]/jmeter/reports文件夹中所有目录的内容复制到本地工作区中的jmeter_results文件夹:

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.请注意,您必须在Docs_LoadTest运行之间Docs_LoadTest文件夹中的目标文件夹和管道,否则将使用此解决方案复制多个报告。

A better solution:更好的解决方案:

Would be to apply this trick in the Docs_LoadTest and use the Publish Artifact and Copy Artifact features.将在 Docs_LoadTest 中应用此技巧并使用 Publish Artifact 和 Copy Artifact 功能。 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.这解决了必须硬编码另一个作业的路径的问题,即使流水线在 Docs_LoadTest 之外的另一个从站上执行也能正常工作。 This does require the Copy Artifacts plugin .这确实需要Copy Artifacts 插件

Assuming Docs_LoadTest is a Freestyle job:假设Docs_LoadTest是自由式工作:

  1. Add an Execute Shell Build step that copies the results to a fixed folder, eg jmeter_results :添加一个Execute Shell Build 步骤,将结果复制到一个固定文件夹,例如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:然后添加一个 Archive Artifacts Post Build Archive Step 和以下文件来存档:

    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:使用 Copy Artifact 步骤将文件复制到本地工作区中的target/jmeter_results文件夹:

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

  2. Change the call to the HTML publisher to use this folder:更改对 HTML 发布者的调用以使用此文件夹:

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

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.我最终做的是添加简单的 groovy 脚本来遍历报告目录中的文件。 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.关于readFile函数的文档。

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.这不是完全相同的场景,但决定发布我的代码,因为真的很难理解、澄清和获取有关如何在一份最终合并报告中发布不同报告的文档和准确示例,使用 Jenkins 的 publishHTML 插件。

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.解决方案是按标签执行,因此一旦使用 Jenkins DSL 执行不同的执行 - 管道发生,构建只会生成一个带有不同选项卡的报告。

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
            ]  
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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