how to include sonar scanner in a docker image - docker

I have a dotnet image that is used as an agent for the Jenkins pipeline. Now I want to include sonar scanner in the image so that I can run an analysis and see if the coverage is good. If the coverage is not good then the build should fail. How to include the sonar scanner in my image.
I tried including the skilldlabs/sonar-scanner in my Dockerfile of the dotnet image. but when I run the container it directly executed the sonar cube commands and failed as the default sonarqube address is used.
Below is my current Dockerfile
FROM microsoft/dotnet:2.1-sdk
FROM skilldlabs/sonar-scanner:3.3
COPY some-ca.crt /usr/local/share/ca-certificates
COPY NuGet.Config /build/.nuget/NuGet/
VOLUME [ "/build/sources" ]
WORKDIR /build/sources
When I ran :
docker run --name sonar -it sonar
INFO: Scanner configuration file: /root/sonar-scanner-3.3.0.1492-linux/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarQube Scanner 3.3.0.1492
INFO: Java 1.8.0_191 Oracle Corporation (64-bit)
INFO: Linux 4.9.125-linuxkit amd64
INFO: User cache: /root/.sonar/cache
ERROR: SonarQube server [http://sonarqube:9000] can not be reached
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 5.433s
INFO: Final Memory: 3M/39M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarQube Scanner execution
ERROR: Unable to execute SonarQube
ERROR: Caused by: Fail to get bootstrap index from server
ERROR: Caused by: sonarqube: Try again
ERROR:
ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug
logging.
How can I tell the container to provide the config for the sonar-scanner?

Instead of using the sonar scanner image into my image, I have installed dotnet-sonarscanner using the below command,
dotnet tool install -g dotnet-sonarscanner
I had to install a "coverlet" package to my unit test project by adding the below to my .csproj file of the unit test project.
<PackageReference Include="coverlet.msbuild" Version="2.6.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Now, whenever I want to send sonarqube my coverage results I run below command to generate the coverage file.
dotnet test ./UnitTests/UnitTests.csproj /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
The above command will generate the coverage.opencover.xml file in the project folder.
Now use below commands to send the coverage
dotnet sonarscanner begin /k:"yourprojectkey" /d:sonar.host.url=https://yoursonarqubedomain.com /d:sonar.cs.opencover.reportsPaths="./UnitTests/coverage.opencover.xml" /d:sonar.coverage.exclusions="**Tests*.cs"
dotnet build
dotnet sonarscanner end
you can set the sonarscanner properties like report location and the URL etc., using /d:

Related

integrating sonarqube with jenkins insufficient privileges error

I followed this video https://www.youtube.com/watch?v=jh7utASgKj4 to integrate sonarqube with jenkins. However, I get the insufficient privileges error when scanning the code by sonarqube server. The version of sonarqube server is 6.7. how can I solve this problem?
Here is the analysis properties in the jenkins project
# Required metadata
sonar.projectKey = test-sonar
# Path to source directory
sonar.sources = /var/jenkins_home/workspace/$JOB_NAME/test-sonar/src
# bind sonar.java.binaries property
sonar.java.binaries=/var/jenkins_home/workspace/$JOB_NAME/test-sonar/target/classes
Here is the output of console.
INFO: SCM provider for this project is: git
INFO: 16 files to be analyzed
INFO: 16/16 files analyzed
INFO: 5 files had no CPD blocks
INFO: Calculating CPD for 8 files
INFO: CPD calculation finished
INFO: Analysis report generated in 132ms, dir size=124 KB
INFO: Analysis reports compressed in 48ms, zip size=61 KB
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 48.028s
INFO: Final Memory: 15M/304M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarQube Scanner execution
ERROR: Insufficient privileges
ERROR:
ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.
WARN: Unable to locate 'report-task.txt' in the workspace. Did the SonarScanner succedeed?
ERROR: SonarQube scanner exited with non-zero code: 1
If you are running the Sonar Analysis via Execute shell plugin for Jenkins, you need to supply the Username Password of a User which has the Execute Analysis rights on the SonarQube server you are trying to access.
To supply the username in the shell, you can use
-Dsonar.login=yourLoginName -Dsonar.password=*****
To set the Execute Analysis permission you can ask your DevOps team to give you access or if you are looking into that,
Goto Administration > Security > User/Groups.
Please make sure that you configure the Sonarqube UserID and Password having privileges to access sonar server (execute analysis permissions) in the Sonar plugin for Jenkins. This error might appear for 5.3 and higher versions when the anonymous access to sonar is disabled.

nUnit's results are not displayed in SonarQube

I'm using SonarQube and Jenkins. In Jenkins, I check the unit tests with nUnit and the coverage with opencover. The opencover's report is displayed in Sonar but the nunit's report is not.
I followed the instructions at : Analysing with SonarQube Scanner for Jenkins and at : Unit Test Execution Results Import
My "Begin Analysis and SonarQube for MSBuild" args :
/d:sonar.sources="Foo.Bar.Business, Foo.Bar.Data, Foo.Bar.Web, Foo.Bar.FileGen, Foo.Bar.FileInt, Foo.Bar.Revision"
/d:sonar.binaries="Build/Foo.Bar.Business.dll, Build/Foo.Bar.Data.dll, Build/Foo.Bar.Web.dll, Build/Foo.Bar.FileGen.exe, Build/Foo.Bar.FileInt.exe, Build/Foo.Bar.Revision.exe, Build/Foo.Bar.TableauDeBord.exe"
/d:sonar.exclusions="Foo.Bar.Web/Scripts/JQuery/**/*, Foo.Bar.Web/Scripts/MomentJS/**/*, Foo.Bar.Web/Scripts/CanvasJS/**/*, Foo.Bar.Web/Scripts/JQueryUi/**/*, Foo.Bar.Web/Scripts/DataTables/**/*, Foo.Bar.Web/Content/**/*, Foo.Bar.Web/App_Start/**/*, Foo.Bar.Data/Model/**/*, Model/**/*, **/AssemblyInfo.cs, **/*Test.cs, Foo.Bar.Test.Web/**/*, Foo.Bar.JeuTest/**/*, Foo.Bar.Gestion.Web/**/*, **/*.js"
/d:sonar.tests="Foo.Bar.Test"
/d:sonar.cs.opencover.reportsPaths="D:/Program Files (x86)/Jenkins/workspace/Bar/reports/opencovertests.xml"
/d:sonar.cs.nunit.reportsPaths="D:/Program Files (x86)/Jenkins/workspace/Bar/reports/TestResult.xml"
My "after msbuild"'s command (windows) :
MKDIR "%WORKSPACE%\reports"
MKDIR "%WORKSPACE%\reports-history"
SET COV_PTH="D:\Programmes\opencover.4.6.519"
SET TOOL_PATH="D:\Programmes\NUnit-3.4.1"
SET RPT_PATH="D:\Programmes\ReportGenerator"
%COV_PTH%\OpenCover.Console.exe -target:"%TOOL_PATH%\bin\nunit3-console.exe" -targetargs:"Build\Foo.Bar.Test.dll -result:reports\TestResult.xml;format=nunit2" -filter:"+[Foo.Bar.*]* -[Foo.Bar.Test]* -[Foo.Bar.Data]Foo.Bar.Data.Model.*" -register -output:"reports\opencovertests.xml"
%RPT_PATH%\ReportGenerator.exe "-reports:reports\opencovertests.xml" "-targetdir:reports-ReportGenerator" "-historydir:reports-history"
Versions :
Jenkins : 2.74
SonarQube : 6.5
SonarQube scanner for MSBuild :3.0.2.656
SonarQube Scanner : 3.0.3.778
SonarC# plugin : 6.4.1
OpenCover : 4.6.519
nUnit : 3.4.1
MSBuild : 14
When calling OpenCover.Console.exe you need to use parameter+value:
-register:user instead of -register only.
In same command line, be sure to be located in folder D:/Program Files (x86)/Jenkins/workspace/Bar/, where the report is stored finally.

Sonar runner Execution failure on Jenkins

I am trying to configure a Sonar Plugin on Jenkins, Executing this SonarJob on jenkins results in an error. The output console does not indicate whats causing this error. Looking for a way to debug this issue.
-Some info on the Setup
->The SonarJob workspace is copied from another Job
-> configured Sonar and Sonar Runner in Jenkins-> Configure System
-> Sonar Build Step is Triggered by "invoke Standalone Sonar Analysis" . The output console log is as follows
Thanks In Advance !
Lokesh
Started by user cheetahb#ca.ibm.com
[EnvInject] - Loading node environment variables.
Building on master in workspace /drv-cheetbld3_1/pipeline/jenkins/jobs/SonarQube1/workspace
Copied 2,820 artifacts from "BI-Pattern_3.0_build" build number 41
[workspace] $ /home/cheetahb/sonarhome/bin/sonar-runner Sonar_5.0_new -Dsonar.jdbc.driver=com.mysql.jdbc.Driver -Dsonar.jdbc.url=jdbc:mysql://9.37.213.8:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance ******** ******** -Dsonar.host.url=http:// vottlokesh. ottawa.ibm.com:9000/ -Dsonar.projectBaseDir=/drv-cheetbld3_1/pipeline/jenkins/jobs/SonarQube1/workspace -Dsonar.projectName=biPattern3.0 -Dsonar.projectVersion=1.0 -Dsonar.projectKey=biproject -Dsonar.verbose=true -Dsonar.sources=/drv-cheetbld3_1/pipeline/jenkins/jobs/SonarQube1/workspace
SonarQube Runner 2.4
Java 1.7.0_65 Oracle Corporation (64-bit)
Linux 2.6.32-431.20.5.el6.x86_64 amd64
INFO: Runner configuration file: /home/cheetahb/sonarhome/conf/sonar-runner.properties
INFO: Project configuration file: NONE
INFO: SonarQube Server 5.0
18:23:43.161 INFO - Load global referentials...
18:23:43.181 DEBUG - Download: http:// vottlokesh.ottawa.ibm.com:9000/batch/global (no proxy)
18:23:43.926 INFO - Load global referentials done: 768 ms
18:23:43.932 INFO - User cache: /home/cheetahb/.sonar/cache
18:23:43.947 INFO - Install plugins
18:23:43.947 DEBUG - Download index of plugins
18:23:43.947 DEBUG - Download: http:// vottlokesh.ottawa.ibm.com:9000/deploy/plugins/index.txt (no proxy)
18:23:44.066 INFO - Install JDBC driver
18:23:44.066 DEBUG - Download index of jdbc-driver
18:23:44.067 DEBUG - Download: http:// vottlokesh.ottawa.ibm.com:9000/deploy/jdbc-driver.txt (no proxy)
18:23:44.079 INFO - Create JDBC datasource for jdbc:mysql://9.37.213.8:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
18:23:45.834 DEBUG - Testing JDBC connection
18:23:48.209 DEBUG - Download: http:// vottlokesh.ottawa.ibm.com:9000/api/server (no proxy)
18:23:48.523 INFO - Initializing Hibernate
18:23:48.526 DEBUG - hibernate.generate_statistics: false
18:23:48.527 DEBUG - hibernate.dialect: org.sonar.core.persistence.dialect.MySql$MySqlWithDecimalDialect
18:23:48.527 DEBUG - hibernate.connection.provider_class: org.sonar.jpa.session.CustomHibernateConnectionProvider
18:23:50.751 DEBUG - Loaded 2689 properties from l10n bundles
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
Total time: 8.801s
Final Memory: 35M/355M
INFO: ------------------------------------------------------------------------
ERROR: Error during Sonar runner execution
ERROR: Unable to execute Sonar
ERROR: Caused by: Task Sonar_5.0_new does not exist
ERROR:
ERROR: To see the full stack trace of the errors, re-run SonarQube Runner with the -e switch.
ERROR: Re-run SonarQube Runner using the -X switch to enable full debug logging.
Build step 'Invoke Standalone Sonar Analysis' marked build as failure
Finished: FAILURE
You must get rid of the Sonar_5.0_new label that you set on the Task to run field in your Jenkins job configuration.

Sonar Runner does not use properties of sonar-project.properties

I am using SonarQube Runner 2.4 at jenkins since today but the analysis fail with the message:
SonarQube Runner 2.4
Java 1.7.0_51 Oracle Corporation (64-bit)
INFO: Runner configuration file: /.jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/Sonar_Runner_2.4/conf/sonar-runner.properties
INFO: Project configuration file: NONE
INFO: Default locale: "en_US", source code encoding: "ISO-8859-1" (analysis is platform dependent)
INFO: Work directory: /.jenkins/workspace/Sonar/.sonar
INFO: SonarQube Server 4.3.2
23:21:42.176 INFO - Load batch settings
23:21:42.160 INFO - User cache: /.sonar/cache
23:21:42.270 INFO - Install plugins
23:21:43.322 INFO - Install JDBC driver
23:21:43.329 INFO - Create JDBC datasource for jdbc:mysql://xxx
23:21:45.505 INFO - Initializing Hibernate
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
Total time: 5.214s
Final Memory: 21M/429M
INFO: ------------------------------------------------------------------------
ERROR: Error during Sonar runner execution
ERROR: Unable to execute Sonar
ERROR: Caused by: You must define the following mandatory properties for ‚com.mypackage:MyProject: sonar.sources
ERROR:
ERROR: To see the full stack trace of the errors, re-run SonarQube Runner with the -e switch.
ERROR: Re-run SonarQube Runner using the -X switch to enable full debug logging.
Build step 'Invoke Standalone Sonar Analysis' marked build as failure
[BFA] Scanning build for known causes...
[BFA] Done. 0s
Notifying upstream projects of job completion
Finished: FAILURE
If I add the property "MyProject.sonar.sources=src" In the project properties in Jenkins (I use "Invoke Standalone Sonar Analysis"). I will receive the error message
ERROR: Caused by: Findbugs needs sources to be compiled. Please build project before executing sonar and check the location of compiled classes.
This means the sonar-project.properties (/.jenkins/workspace/Deploy/MyProject/sonar-project.properties) is not read or used because there are all values defined:
sonar.language=java
sonar.projectName=MyProject
sonar.projectVersion=0.1
sonar.binaries=bin
sonar.projectDescription=
sonar.projectKey=MyProjectKey
sonar.sources=src
When sonar is executed I can see the correct path to MyProject where :
-DMyProject.sonar.projectBaseDir=/.jenkins/workspace/Deploy/MyProject
Or does the name of sonar-project.properties changed?
Please build the project in the release mode. After the build happens you will get the dll's produced somewhere. you have to point to that location.

SonarQube Runner 2.4 can not find sonar.sources in sonar-project.properties even it exist

I am using SonarQube Runner 2.4 at jenkins since today but the analysis fail with the message:
SonarQube Runner 2.4
Java 1.7.0_51 Oracle Corporation (64-bit)
INFO: Runner configuration file: /.jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/Sonar_Runner_2.4/conf/sonar-runner.properties
INFO: Project configuration file: NONE
INFO: Default locale: "en_US", source code encoding: "ISO-8859-1" (analysis is platform dependent)
INFO: Work directory: /.jenkins/workspace/Sonar/.sonar
INFO: SonarQube Server 4.3.2
23:21:42.176 INFO - Load batch settings
23:21:42.160 INFO - User cache: /.sonar/cache
23:21:42.270 INFO - Install plugins
23:21:43.322 INFO - Install JDBC driver
23:21:43.329 INFO - Create JDBC datasource for jdbc:mysql://xxx
23:21:45.505 INFO - Initializing Hibernate
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
Total time: 5.214s
Final Memory: 21M/429M
INFO: ------------------------------------------------------------------------
ERROR: Error during Sonar runner execution
ERROR: Unable to execute Sonar
ERROR: Caused by: You must define the following mandatory properties for ‚com.mypackage:MyProject: sonar.sources
ERROR:
ERROR: To see the full stack trace of the errors, re-run SonarQube Runner with the -e switch.
ERROR: Re-run SonarQube Runner using the -X switch to enable full debug logging.
Build step 'Invoke Standalone Sonar Analysis' marked build as failure
[BFA] Scanning build for known causes...
[BFA] Done. 0s
Notifying upstream projects of job completion
Finished: FAILURE
But sonar-project.properties of My_Project contain sonar.sources.
sonar.language=java
sonar.projectName=My_Project
sonar.projectVersion=0.1
sonar.binaries=bin
sonar.projectDescription=
sonar.projectKey=MyProjectKey
sonar.sources=src
Where is the problem?
Greetings
Try adding "sonar.sources=." See if this works or not. Please add the log if the problem still persists.
sonar.sources and sonar.binaries path should be relative to the sonar runner home. You need to check that of the path is right as just src.

Resources