AWS Codebuild should fail when one of the Sonarqube analysis fails
I’ve updated the buildpec.xml with Sonar machine details.
We are using AWS CICD Managed services for generating final jar file.
We are using Sonarqube and we are able to launch Sonar scan from CodeBuild Project where it is performing -> Unit Test, Static Code Analysis and code coverage test.
Infact for code coverage we are using Jacoco plugin, where code coverage failing since we have set Quality Gate (rule) as if code coverage is < 70% it should fail.
In Buildspec.xml we have updated sonar config like below
post_build:
commands:
- mvn sonar:sonar -Dsonar.login=$SONAR_LOGIN -Dsonar.host.url=$SONAR_HOST
- mvn jacoco:prepare-agent test jacoco:report
So AWS Codebuild project is launching the Soanr scan (using command mvn sonar:sonar …)
But for some reason code coverage analysis (result since value we got as 46%) is NOT reflecting back in AMS Codebuild Project.
But in sonarqube final report is failed with reason
“Quality GateFailed”
Coverage 47.6%
Problem Statement:
When Sonarqube scan analysis is failed same should be reflecting in AWS codebuild project.
I’m a bit confused by your account. There seem to be two different concerns: reporting of test coverage, and being able to fail your AWS codebuild project on a failing quality gate.
First the coverage. You say you’ve got these commands in place
- mvn sonar:sonar -Dsonar.login=$SONAR_LOGIN -Dsonar.host.url=$SONAR_HOST
- mvn jacoco:prepare-agent test jacoco:report
Which means means you run tests only after analysis - which would explain why no results show up in SonarQube.
But you also say that coverage results are showing up
So some coverage is being reported. Where is it coming from?
Regarding your AWS codebuild project, I’m guessing this is a matter of using a webhook to return the Quality Gate status back to your AWS project. However, I have no idea how to process that on the AWS side.
Thank you very much for your kind and quick reply.
By the way I am mentioning logs are here
[INFO] Analysing /codebuild/output/src0828xxx15/src/target/jacoco.exec
[INFO] No information about coverage per test.
[INFO] Sensor JaCoCoSensor [java] (done) | time=103ms
[INFO] Sensor SonarJavaXmlFileSensor [java]
[INFO] 1 source files to be analyzed
[INFO] Sensor SonarJavaXmlFileSensor [java] (done) | time=696ms
[INFO] 1/1 source files have been analyzed
[INFO] Sensor XML Sensor [xml]
[INFO] Sensor XML Sensor [xml] (done) | time=110ms
[INFO] Sensor Zero Coverage Sensor
[INFO] Sensor Zero Coverage Sensor (done) | time=22ms
[INFO] Sensor CPD Block Indexer
[INFO] Sensor CPD Block Indexer (done) | time=31ms
[INFO] No SCM system was detected. You can use the ‘sonar.scm.provider’ property to explicitly specify it.
[INFO] 2 files had no CPD blocks
[INFO] Calculating CPD for 7 files
[INFO] CPD calculation finished
[INFO] Analysis report generated in 124ms, dir size=92 KB
[INFO] Analysis reports compressed in 30ms, zip size=44 KB
[INFO] Analysis report uploaded in 93ms
[INFO] ANALYSIS SUCCESSFUL, you can browse http://x.xxx.x.xxx/dashboard/index/xxxxxx
[INFO] Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
[INFO] More about the report processing at http://xxxxx/api/ce/task?id=AWv1qi8OWA40vMloL8da
[INFO] Task total time: 8.833 s
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15.652 s
[INFO] Finished at: 2019-07-15T12:44:59Z
[INFO] ------------------------------------------------------------------------
In the log there is print
[INFO] No SCM system was detected. You can use the ‘sonar.scm.provider’ property to explicitly specify it.
Is this causing the issue?
I mean because of this whether Sonarqube was NOT updating status back to Code PipeLine?
My buildspec.yml file as follows, this is the main file by referring this file AWS Code pipeline executes entire scenario.
version: 0.2
env:
variables:
SONAR_LOGIN: “abcd82f6a6c5a7327111110be58f10254a251234”
SONAR_HOST: “http://x.x.x.x.” #You should use parameter-store here instead
phases:
install:
runtime-versions:
java: openjdk8
pre_build:
commands:
- echo Nothing to do in the pre_build phase…
build:
commands:
- echo Build started on date
- mvn install
- mvn test
post_build:
commands:
- echo Build completed on date
- mvn sonar:sonar -Dsonar.login=$SONAR_LOGIN -Dsonar.host.url=$SONAR_HOST
- mvn jacoco:prepare-agent test jacoco:report
artifacts:
files:
- echo Uploading the Artifacts date
- target/xxxxxxxx-0.0.1-SNAPSHOT.jar
- echo Uploaded the Artifacts date
Also let me clarify once again the issue.
Code coverage is executed by Sonarqube and it is found as 47.6%, since in Sonarqube we have set the criteria if code coverage < 70%, then final report should FAIL.
And it’s failing also, but this results not be reflected back to AWS CodeBuild process.
From the log we can see that
[INFO] No SCM system was detected. You can use the ‘sonar.scm.provider’ property to explicitly specify it.
Is this could be problem for not reporting back the analysis report?