如何编写jenkins插件导入last_successful_build工件报告到当前版本

时间:2018-05-29 07:24:17

标签: jenkins groovy

我在

之后写了一个插件

http://www.baeldung.com/jenkins-custom-plugin

它会生成一个HTML报告

            File artifactsDir = build.getArtifactsDir();
            String path = artifactsDir.getCanonicalPath() + REPORT_TEMPLATE_PATH;
            File reportFile = new File("path");
            // write report's text to the report's file

对于下一次构建,我想导入此报告文件以查看更改

我尝试了这些,但没有一个可以使用

build.getPreviousSuccessfulBuild().getArtifactManager().root() + REPORT_TEMPLATE_PATH
// fail with File not found, but the file is there in bash
build.getPreviousSuccessfulBuild().getArtifactsDir() + REPORT_TEMPLATE_PATH
// null pointer exception, seems to be generated by getArtifactsDir()
build.getPreviousBuild().getArtifactManager().root() + REPORT_TEMPLATE_PATH

那么如何在当前版本中获取最后一个成功的构建报告文件?

1 个答案:

答案 0 :(得分:0)

这就是我在管道作业中所做的事情。我剥离了部分原始代码,希望不要引入错误。为了清楚起见,我还删除了错误处理:

snap

当我比较我们的解决方案时:当我从工件中获取相对路径时,您不用// find last successful build def lastSuccessfulBuild = currentBuild.getPreviousBuild() while (lastSuccessfulBuild && (lastSuccessfulBuild.currentResult != 'SUCCESS')) { lastSuccessfulBuild = lastSuccessfulBuild.getPreviousBuild() } // here I go for a file named 'crc.txt' // this works only if you have a // archiveArtifacts artifacts: 'crc.txt', fingerprint: true // somewhere in your build def build = lastSuccessfulBuild?.getRawBuild() def artifact = build.getArtifacts().find { it.fileName == 'crc.txt' } def uri = build.artifactManager.root().child(artifact.relativePath).toURI() def content = uri.toURL().text ,并且在child()中有相对路径。