如果开发人员没有为新代码提供超过70%的单元测试代码覆盖率,那么Java项目的Jenkins / SonarQube作业就会失败。我们正在尝试为JavaScript项目设置相同的东西。
我已经投入了很多工作来使用Karma和我为它编写的插件生成类似JUnit的XML文件。无论如何,当我提交没有单元测试的新JavaScript代码时,它不会破坏Jenkins构建。以下是Jenkins工作日志的相关部分:
...
07:13:37.417 INFO - Parsing Unit Test run results in Surefire format from folder /home/tomcat/.jenkins/jobs/frontend_common-jscheck/workspace/frontend_common/karma-reports/jstd
...
07:13:38.337 INFO - Analysing /home/tomcat/.jenkins/jobs/frontend_common-jscheck/workspace/frontend_common/karma-reports/lcov/lcov.txt
07:13:38.535 WARN - Could not resolve 2 file paths in lcov.txt, first unresolved path: /home/tomcat/.jenkins/jobs/frontend_common-jscheck/workspace/frontend_common/WebContent/js/jquery/jquery-2.0.3.js
...
07:13:41.962 INFO - Load previous line hashes of: frontend_common-jscheck:WebContent/js/amp/utils/SlavaUtil.js...
...
07:13:44.427 INFO - Executing post-job class org.sonar.plugins.core.issue.notification.SendIssueNotificationsPostJob
07:13:44.456 INFO - Executing post-job class org.sonar.plugins.buildbreaker.AlertBreaker
07:13:44.459 INFO - Executing post-job class org.sonar.plugins.buildbreaker.ForbiddenConfigurationBreaker
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
Total time: 52.594s
Final Memory: 20M/560M
INFO: ------------------------------------------------------------------------
Finished: SUCCESS
您可以猜到我在SlavaUtil.js
中更改了代码,您可以看到SonarQube会从日志中对其进行分析。我在日志中包含了关于无法从lcov.txt
找到2个文件的警告(其中一个文件显示它确实存在,所以我不知道为什么它找不到它)但是我认为这不是构建不会失败的原因。正如您在日志中看到的那样,它会执行那些我认为应该使构建失败的后期作业类,但它们不会。
问题:如何解决此问题?
更新
我查看了一个未通过质量测试的Java项目Jenkins日志,相关日志如下所示:
10:11:43.780 INFO - Executing post-job class org.sonar.plugins.core.issue.notification.SendIssueNotificationsPostJob
10:11:43.798 INFO - Executing post-job class org.sonar.plugins.buildbreaker.AlertBreaker
10:11:43.801 ERROR - [BUILD BREAKER] Coverage on new code < 70 since previous version (4815 - 2015 Oct 23)
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
Total time: 21.991s
Final Memory: 18M/240M
INFO: ------------------------------------------------------------------------
ERROR: Error during Sonar runner execution
org.sonar.runner.impl.RunnerException: Unable to execute Sonar
at org.sonar.runner.impl.BatchLauncher$1.delegateExecution(BatchLauncher.java:91)
at org.sonar.runner.impl.BatchLauncher$1.run(BatchLauncher.java:75)
at java.security.AccessController.doPrivileged(Native Method)
at org.sonar.runner.impl.BatchLauncher.doExecute(BatchLauncher.java:69)
at org.sonar.runner.impl.BatchLauncher.execute(BatchLauncher.java:50)
at org.sonar.runner.api.EmbeddedRunner.doExecute(EmbeddedRunner.java:102)
at org.sonar.runner.api.Runner.execute(Runner.java:100)
at org.sonar.runner.Main.executeTask(Main.java:70)
at org.sonar.runner.Main.execute(Main.java:59)
at org.sonar.runner.Main.main(Main.java:53)
Caused by: org.sonar.api.utils.SonarException: Alert thresholds have been hit (1 times).
at org.sonar.api.batch.BuildBreaker.fail(BuildBreaker.java:34)
at org.sonar.plugins.buildbreaker.AlertBreaker.analyseMeasures(AlertBreaker.java:57)
at org.sonar.plugins.buildbreaker.AlertBreaker.executeOn(AlertBreaker.java:50)
at org.sonar.batch.phases.PostJobsExecutor.execute(PostJobsExecutor.java:72)
at org.sonar.batch.phases.PostJobsExecutor.execute(PostJobsExecutor.java:61)
at org.sonar.batch.phases.PhaseExecutor.execute(PhaseExecutor.java:135)
at org.sonar.batch.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:222)
at org.sonar.api.platform.ComponentContainer.startComponents(ComponentContainer.java:93)
at org.sonar.api.platform.ComponentContainer.execute(ComponentContainer.java:78)
at org.sonar.batch.scan.ProjectScanContainer.scan(ProjectScanContainer.java:235)
at org.sonar.batch.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:230)
at org.sonar.batch.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:223)
at org.sonar.api.platform.ComponentContainer.startComponents(ComponentContainer.java:93)
at org.sonar.api.platform.ComponentContainer.execute(ComponentContainer.java:78)
at org.sonar.batch.scan.ScanTask.scan(ScanTask.java:65)
at org.sonar.batch.scan.ScanTask.execute(ScanTask.java:52)
at org.sonar.batch.bootstrap.TaskContainer.doAfterStart(TaskContainer.java:128)
at org.sonar.api.platform.ComponentContainer.startComponents(ComponentContainer.java:93)
at org.sonar.api.platform.ComponentContainer.execute(ComponentContainer.java:78)
at org.sonar.batch.bootstrap.BootstrapContainer.executeTask(BootstrapContainer.java:171)
at org.sonar.batch.bootstrapper.Batch.executeTask(Batch.java:95)
at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:67)
at org.sonar.runner.batch.IsolatedLauncher.execute(IsolatedLauncher.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.sonar.runner.impl.BatchLauncher$1.delegateExecution(BatchLauncher.java:87)
... 9 more
ERROR:
ERROR: Re-run SonarQube Runner using the -X switch to enable full debug logging.
Build step 'Invoke Standalone SonarQube Analysis' marked build as failure
从日志中可以看出,应该抛出异常的是AlertBreaker
。我想,我将尝试获取源代码并在调试模式下本地运行。没有看到任何其他方法来解决它。
答案 0 :(得分:0)
构建断路器插件使用项目的质量状态来决定构建是否应该失败(参见documentation)。
运行分析后,在SQ的Web界面中检查项目。
项目是否与质量门相关联?如果不是,则插件正在按预期工作。
如果是的话,该项目的质量门是否为红色?如果是,则构建断路器插件存在问题。如果它不是,它正在按预期工作。
如果质量门报告的覆盖值不是您期望的值,则问题出在分析方面:检查您的设置(包含和所有)以及您正在使用的语言插件的版本。