【问题标题】:Merge multiple jest coverage reports in Jenkins pipeline在 Jenkins 管道中合并多个笑话覆盖率报告
【发布时间】:2020-01-19 17:44:41
【问题描述】:

我已收到 CI/CD 管道,我的第二个任务是在输出中获取一些覆盖率报告。我对 Jenkins 很陌生,所以任何指针都会有所帮助!

我在 Jenkins 管道中构建了几个微服务。每个微服务都有一组开玩笑的单元测试。文件夹结构如下,根目录下是Jenkinsfile

.Jenkinsfile (at the root)
./service1/code
./service2/code
...
./serviceN/code

在构建阶段,Jenkins 会遍历子文件夹并构建 docker 镜像。构建完所有服务后,会有一个单元测试阶段循环遍历每个服务/文件夹并执行 jest --coverage
每个服务在文件夹 ./serviceX/coverage/ 中输出一个 cobertura-coverage.xml 文件

我真正想要的是在单元测试阶段结束时将各个覆盖率报告合并到 ./service1/coverage/cobertura-coverage.xml ./service2/coverage/cobertura-coverage.xml

并用类似的方式发布组合覆盖结果

step([$class: "CoberturaPublisher", coberturaReportFile: "${jestEnrichers[count]}/coverage/cobertura-coverage.xml"])

目前我正在运行这样的笑话报道

serviceFolders = ['service1','service2'...] 

for(int count=0; count<serviceFolders.size(); count++){
 echo "Attempting to run JEST tests for ${serviceFolders[count]}"
 sh """
   cd "${WORKSPACE}"/"${serviceFolders[count]}";
   npm install
   npm test
  """
}

我可以在所有服务的构建日志中看到输出

但我不知道如何合并为一份报告 如果我执行这个

step([$class: "CoberturaPublisher", coberturaReportFile: "${service1/coverage/cobertura-coverage.xml"])
            publishHTML target: [
              allowMissing : false,
              alwaysLinkToLastBuild: false,
              keepAll              : true,
              reportDir            : "output/coverage/jest",
              reportFiles          : "index.html",
              reportName           : "Test Report"
            ]

我在 jenkins UI 中看到了一项服务的覆盖率报告,所以我想我可以将它包装在执行的同一个循环中 像这样

serviceFolders = ['service1','service2'...] 

for(int count=0; count<serviceFolders.size(); count++){
 echo "Attempting to run JEST tests for ${serviceFolders[count]}"
 sh """
   cd "${WORKSPACE}"/"${serviceFolders[count]}";
   npm install
   npm test
  """
}
step([$class: "CoberturaPublisher", coberturaReportFile: "${serviceFolders[count]/coverage/cobertura-coverage.xml"])
            publishHTML target: [
              allowMissing : false,
              alwaysLinkToLastBuild: false,
              keepAll              : true,
              reportDir            : "output/coverage/jest",
              reportFiles          : "index.html",
              reportName           : "Test Report"
            ]

Jenkins 报告了一个错误,我只看到了 service1 的覆盖率报告

在循环的第一次迭代之后出现这个错误(在第二个服务的笑话测试运行之前


[Cobertura] Publishing Cobertura coverage results...

[Cobertura] Cobertura coverage report found.

[Pipeline] publishHTML
[htmlpublisher] Archiving HTML reports...
[htmlpublisher] Archiving at BUILD level /home/jenkins/workspace/licies-pipeline_pipeline-testing/output/coverage/jest to /var/jenkins_home/jobs/xxx-yyy-policies-pipeline/branches/pipeline-testing/builds/208/htmlreports/Test_20Report
ERROR: Specified HTML directory '/home/jenkins/workspace/licies-pipeline_pipeline-testing/output/coverage/jest' does not exist.
[Pipeline] echo
Attempting to run JEST tests for hqaddress

【问题讨论】:

    标签: node.js jestjs jenkins-pipeline jenkins-plugins


    【解决方案1】:

    我会在一个 Jest 配置中指定我的测试源,并指定我的路径以通过 regex 进行搜索。

    现在,在微服务的世界中,我会保持这些隔离,并让每个微服务都有我的 Jenkinsfile。这样,如果代码在单个子目录中更新,您就无需启动 CICD 来重新部署您的整个产品。

    【讨论】:

      猜你喜欢
      • 2019-03-16
      • 2019-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-11
      • 1970-01-01
      • 2016-07-12
      • 2021-07-23
      相关资源
      最近更新 更多