How to fail maven goal sonar:sonar based on quality gates rules?
I run it for local builds, for CI I already wrote a script according to http://docs.sonarqube.org/display/SONAR/Breaking+the+CI+Build
So, if quality gates validation fails then goal also should fails
If Sonar Qube Version is bigger than 8.1, following command can be used:
mvn verify sonar:sonar -Dsonar.qualitygate.wait=true
You can use the SonarQube Build Breaker plugin.
It's a controversy feature as explained in the blog post Why You Shouldn't Use Build Breaker.
It is also possible to integrate with Jenkins use web hook (see the blog post Breaking the SonarQube Analysis with Jenkins Pipelines).
I added the script as post step command, so after build succeeded the script check quality gates and breaks the job if they are not ok.
Related
i got a Maven Jenkins Job (not a Pipeline job) which runs a code analysis with sonar (mvn sonar:sonar). The Sonar report is create and everthing is fine. Now I want that the Jenkins job fails or at least gets a warning state if Sonar does not reach the quality gate.
I running Jenkins ver. 2.176.3.
How do I config the job? I only found a solution for a pipeline job with SonarQube (https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-jenkins/)
Thanks ...
Found a solution.
https://github.com/jenkinsci/sonar-quality-gates-plugin/blob/master/README.md
Just install the plugin and add as Post Build Step "Quality Gates SonarQube Plugin".
I am using SonarQube version 6.7 for running analysis of Maven projects using the command mvn clean verify sonar:sonar from the project directory using command prompt and get the results. The sonar configurations in settings.xml for maven (v3.5.3) are added respectively.
In Jenkins (version 2.161), I have installed SonarQube Scanner for Jenkins (v2.8.1) plugin. The SonarQube Server configuration is configured as below.
In the Jenkins maven project, I have configured the post steps as below.
I have checked the Prepare SonarQube Scanner environment in the Build Environment section and the Build goal is -e clean verify sonar:sonar.
My issue is, when the SonarQube analysis for a maven project is triggered through command prompt using the command mvn clean verify sonar:sonar, I am getting the results as expected. But when the Jenkins job is triggered with the configurations above for the same maven project, the results are different and incorrect. What am I missing?
P.S- In the Post-build Actions, I can see the SonarQube analysis with maven is deprecated.
Thanks in advance.
You use different scanners. First you used SonarScanner for Maven (mvn sonar:sonar). Next you used Basi SonarScanner, which requires manual configuration of all options.
The best option to sole it is always use the same scanner. You have Maven project, so you can enable Prepare SonarQube Scanner environment in Build Environment, and next execute Sonar goal $SONAR_MAVEN_GOAL in Build.
As of now I know sonar has dropped support for Build-braker plugin but I am trying to integrate build breaker plugin for sonarqube. I have 5.6 version of sonarqube and build breaker with 1.1 version installed.In Build breaker plugin I have setup sonar.buildbreaker.skip to false. And I run job using Jenkins and maven. for sonarqube analysis I have setup a phase as "prepare-package" where it runs automatically when I do mvn clean install. But, eventhough my quality gate shows failed, my build is still getting success rather than failing. I tried to find documentation but I didn't found better for this. https://docs.sonarqube.org/display/SONARQUBE45/Build+Breaker+Plugin
I got a root maven project, under which there are many independent modules (e.g. module_A, module_B, etc.).
One of these module is my integration-tests module, and it uses all the above external modules.
In order to have code coverage for all modules used by integration-tests, I use a workaround based on maven-ant (see this blog post).
Problem is:
The above generates csv/html report, yet sonarqube jacoco widget parses only jacoco*.exec files - which results in 0% code coverage.
Question is:
EDIT
here's an example project for the problem above.
You don't need to use that workaround. You can provide Sonar with integration tests coverage file with following property (you shall use it while executing sonar:sonar goal):
-Dsonar.jacoco.itReportPath=<coverage_file>
Here is detailed documentation:
http://docs.codehaus.org/display/SONAR/Code+Coverage+by+Integration+Tests+for+Java+Project
To sum up:
Compile all your modules.
Execute integration tests with jacoco enabled.
Execute Maven Sonar build adding mentioned property in command line.
I have prepared example project generating both unit and integration coverage results, you can check it here:
https://github.com/octosan/unit-and-integration-jacoco-coverage-with-sonar
You have to:
download newest Sonarqube version and start it
execute command:
mvn clean install sonar:sonar -Dsonar.jacoco.itReportPath=<absolute_path>/itest/target/jacoco-it.exec
add integration coverage widged in project dashboard in Sonar
I'm looking at using Sonar to reduced the number of plugins I've had to install in Jenkins to get some decent code analysis (and sonar seems to do more and present it better). However when I kick of the sonar job the JUnit / Concordion tests are executed. I don't want these tests run as Jenkins is already executing the tests.
How do I stop the tests executing and just perform code analysis?
I've installed sonar 3.7.3 and executing using the Gradle sonar-runner plugin and specifying the :sonarRunner task.
I'm just facing the same issue and it seems that you can run :sonarqube -x test to avoid having tests run before.
You can set the sonar.dynamicAnalysis property to false. This will skip tests and only perform the static analysis part.
See http://docs.codehaus.org/display/SONAR/Analysis+Parameters#AnalysisParameters-OptionalParameters