How to configure a Jenkins Pipeline for SonarQube scan - sonarqube

I am trying to configure a jenkins pipeline for my project, but there is something missing here, if anyone could direct on what I am doing wrong:
Below is the pipeline script:
node {
stage('SonarQube analysis') {
// requires SonarQube Scanner 2.8+
def scannerHome = tool 'sonarScanner';
withSonarQubeEnv('SonarQube 6.2') {
bat "${scannerHome}/bin/sonar-runner.bat"
}
}
}
Below is the Jenkins Sonar Plugin related configurations:
Under Manage Jenkins > Configure System:
Under Manage Jenkins > Global Tool Configuration
Below is the error that I get:
D:\jenkins\workspace\Test_Pipeline>D:\sonar-runner-2.4/bin/sonar-runner.bat
D:\sonar-runner-2.4
SonarQube Runner 2.4
Java 1.8.0_92 Oracle Corporation (64-bit)
Windows Server 2008 R2 6.1 amd64
INFO: Runner configuration file: D:\sonar-runner-2.4\conf\sonar-runner.properties
INFO: Project configuration file: NONE
INFO: Default locale: "en_US", source code encoding: "UTF-8"
INFO: Work directory: D:\jenkins\workspace\Test_Pipeline\.\.sonar
INFO: SonarQube Server 6.2
19:48:53.627 INFO - Load global repositories
19:48:53.920 INFO - Load global repositories (done) | time=298ms
19:48:53.951 WARN - Property 'sonar.jdbc.url' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
19:48:53.951 WARN - Property 'sonar.jdbc.username' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
19:48:53.951 WARN - Property 'sonar.jdbc.password' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
19:48:53.951 INFO - User cache: C:\Users\Administrator\.sonar\cache
19:48:54.378 INFO - Load plugins index
19:48:54.378 INFO - Load plugins index (done) | time=0ms
19:48:55.235 INFO - Process project properties
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
Total time: 3.404s
Final Memory: 5M/120M
INFO: ------------------------------------------------------------------------
ERROR: Error during Sonar runner execution
ERROR: Unable to execute Sonar
ERROR: Caused by: You must define the following mandatory properties for 'Unknown': sonar.projectKey, 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.
[Pipeline] }
[Pipeline] // wrap
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
The problem seems that Jenkins does not know where my sonar-project.properties is placed. It is under D:\myprj and has contents as below:
# required metadata
sonar.projectKey=my_prj:my_prj
sonar.projectName=my_prj
sonar.projectVersion=1.00
sonar.sources=my_prj/src
sonar.language=java
Pls suggest how I can make Jenkins pipeline aware of where the sonar-project.properties file is placed

I needed to add the workspace to my script. Here is the script below:
stage('SonarQube analysis') {
ws('D:\\my_prj') {
// requires SonarQube Scanner 2.8+
def scannerHome = tool 'sonarScanner';
withSonarQubeEnv('SonarQube 6.2') {
bat "${scannerHome}/bin/sonar-scanner.bat"
}
}
}

I see you were using Sonar-runner 2.4. I think by running 2.8 version you will have better results. This is my JenkinsFile configuration, by using Declarative Pipeline syntax. I was having the same issue, but I solved it by renaming my sonar-project.properties file, the previous name was "sonar.properties". I hope this may help others.
stage('Sonarqube analysis') {
steps {
script {
scannerHome = tool 'SonarScanner';
}
withSonarQubeEnv('SonarQube') {
bat "${scannerHome}/bin/sonar-scanner.bat"
}
}
}

Related

JMeter UltimateThreadGroup making my Jenkins pipeline fail

I have a Jenkinsfile pipeline and in one of its stages I run some jmeter tests and generate the respective report.
This is my stage
stage('Run Non-Functional tests - Windows'){
when { expression { env.OS == 'BAT' }}
steps {
dir('') {
bat 'gradlew.bat jmReport'
}
}
}
and this is my build gradle
import de.qualersoft.jmeter.gradleplugin.task.*
plugins {
id "de.qualersoft.jmeter" version "2.1.0"
}
tasks.register('jmRun',JMeterRunTask) {
jmxFile.set("TestPlan.jmx")
}
tasks.register("jmReport",JMeterReportTask) {
jmxFile.set("TestPlan.jmx")
dependsOn("jmRun")
deleteResults=true
}
When I try to run my pipeline it fails with the following error
Error in NonGUIDriver java.lang.IllegalArgumentException: Problem loading XML from:'D:\ISEP\Mestrado\UnidadesCurriculares\1Ano\1Semestre\2022_2023\ODSOFT\odsoft-22-23-nmb-g303\src\test\jmeter\TestPlan.jmx'. Cause: CannotResolveClassException: kg.apc.jmeter.threads.UltimateThreadGroup
In JMeter, I used the plugin catsug 2.1.0 to do some spike and load tests and I think that's the reason why the exception is thrown.
I tried to use a simple file where the test plan didn't use the plugin and pipeline works fine.
How can I solve my problem?
I added as a dependency but now I am getting another error
dependencies {
jmeterPlugin("kg.apc:jmeter-plugins-casutg:2.10")
jmeterPlugin("org.apache.jmeter:bom:5.4.1")
}
Execution failed for task ':jmRun'.
> Could not resolve all dependencies for configuration ':jmeterPlugin'.
> Could not find org.apache.jmeter:bom:5.4.1.
Required by:
project :
> Could not find org.apache.jmeter:bom:5.4.1.
Required by:
project : > org.apache.jmeter:ApacheJMeter_core:5.4.1
project : > org.apache.jmeter:ApacheJMeter_core:5.4.1 > org.apache.jmeter:jorphan:5.4.1
The error means that the Ultimate Thread Group plugin is not installed, in order to add it you need to explicitly specify the plugin as the dependency for your project:
dependencies {
jmeterPlugin("kg.apc:jmeter-plugins-casutg:2.10")
}
You might also be interested in Jenkins Performance Plugin which allows displaying performance trend charts and conditionally mark build as failed if metrics are worse than in the previous/reference build.

How do I fork a gradle groovy compile process using a different Java version?

I have a Groovy project which is mostly built using Java 7, but I want to build one module using Java 8. Following the documentation for the gradle Groovy plugin, I have added the following to build.gradle for the module:
// Set up for using Java 8
def java8Home = getJava8Home()
def javaExecutable = new File(java8Home, 'bin/java')
println "Java executable = $javaExecutable"
tasks.withType(AbstractCompile) {
options.with {
fork = true
forkOptions.javaHome = file(java8Home)
}
println 'COMPILE ' + name + ': ' + options.forkOptions.javaHome
}
tasks.withType(Test) {
executable = javaExecutable
println ' TEST ' + name + ': ' + executable
}
Now when I run gradlew project-name:browserTest, I see lines like the following
Java executable = ...//jdk1.8.0_144.jdk/Contents/Home/bin/java
COMPILE compileBrowserTestGroovy: .../jdk1.8.0_144.jdk/Contents/Home
...
TEST browserTest: .../jdk1.8.0_144.jdk/Contents/Home/bin/java
...
So I expect a forked compile daemon for compileBrowserTestGroovy using Java 8. However I get an error (Unsupported major.minor version 52.0)showing that it is using Java 7, and I see the following when running with -debug
16:14:03.760 [INFO] [org.gradle.process.internal.DefaultExecHandle] Starting process 'Gradle Worker Daemon 1'. Working directory: /Users/alasdair/workspace/project-name Command: /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/bin/java -Djava.security.manager=worker.org.gradle.process.internal.worker.child.BootstrapSecurityManager -Dfile.encoding=UTF-8 -Duser.country=GB -Duser.language=en -Duser.variant -cp /Users/alasdair/.gradle/caches/3.5/workerMain/gradle-worker.jar worker.org.gradle.process.internal.worker.GradleWorkerMain 'Gradle Worker Daemon 1'
What am I doing wrong?
UPDATE:
I created a small test project with a java 7 and java 8 module. Set up as suggested it works perfectly. The difference seems to be around 'Worker Daemons' ie looking at the debug output above gradle is starting process 'Gradle Worker Daemon 1' (using java 7), whereas the log from the test project shows
16:10:30.559 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter] Executing actions for task ':java-8:compileJava'.
16:10:30.581 [DEBUG] [org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler] Compiler arguments: -source 1.8 -target 1.8 -d /Users/alasdair/workspace/test-project/java-8/build/classes/main -g -sourcepath -proc:none -XDuseUnsharedTable=true -classpath /Users/alasdair/workspace/test-project/java-8/src/main/java/Foo.java
16:10:30.582 [INFO] [org.gradle.api.internal.tasks.compile.CommandLineJavaCompiler] Compiling with Java command line compiler '/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/javac'.
16:10:30.597 [INFO] [org.gradle.process.internal.DefaultExecHandle] Starting process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/javac''. Working directory: /Users/alasdair/workspace/test-project/java-8 Command: /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/javac #/Users/alasdair/workspace/test-project/java-8/build/tmp/compileJava/java-compiler-args.txt
So it seems to be forking and starting a compile with the correct jvm. Why is my main build starting these 'Daemon Workers'? I have in gradle.properties org.gradle.daemon=false. I have tried various other things but this remains the question, as it seems to be just creating and using a new daemon instead of forking with the correct jvm as requested

Sonar scanner report file with taskId and analysisId

What I have for JAVA
I am using Jenkins as my CI/CD server and I created a Jenkinsfile for my JAVA project and for the scanning and quality I am using the maven sonar plugin. The mvn sonar:sonar command generate a file at target/sonar/report-task.txt. The file contains information related with the scanning process and using that information I am able to call the SonarQube REST API with the taskId generated and then I am able to call the REST API using analysisId and decide if the pipeline is broken based on the quality conditions.
What I want for Javascript (any other type of project)
I am trying to do something similar for a Javascript project but this time and using the sonar-scanner from command line but I realized that there is not file generated as report-task.txt ( I believe this file is only generated by maven sonar-plugin). So I will like to know if there is a way to generate that kind of information.
I really need the taskId value in order to do dynamically calls to SonarQube REST API once the scanner process has started.
Since you're using a Jenkinsfile there's no need to do this manually. From the docs
node {
stage('SCM') {
git 'https://github.com/foo/bar.git'
}
stage('SonarQube analysis') {
withSonarQubeEnv('My SonarQube Server') {
sh 'mvn clean package sonar:sonar'
} // SonarQube taskId is automatically attached to the pipeline context
}
}
// No need to occupy a node
stage("Quality Gate"){
timeout(time: 1, unit: 'HOURS') { // Just in case something goes wrong, pipeline will be killed after a timeout
def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
If you're not using Maven to build and analyze, then just sub-in the correct commands as appropriate.
The file contains information related with the scanning process is:
.scannerwork/report-task.txt

sonarqube-gradle-plugin: Wrong base directory derived for sub-projects two levels deep

I have a multi-project with the following structure:
:very:bestappever
:bestappever-distrib
The SonarQubePlugin gives
:sonarqube
09:35:05.139 INFO - Load global repositories
09:35:05.252 INFO - Load global repositories (done) | time=114ms
09:35:05.254 INFO - Server id: 20160428045618
09:35:05.256 INFO - User cache: C:\Users\tk09\.sonar\cache
09:35:05.265 INFO - Install plugins
09:35:05.401 INFO - Install JDBC driver
09:35:05.414 INFO - Create JDBC datasource for jdbc:mysql://10.48.213.38:3306/sonar
09:35:06.456 INFO - Initializing Hibernate
:sonarqube FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':sonarqube'.
> The base directory of the module ':very:bestappever' does not exist: C:\Workarea\verybestappever\very\:very:bestappever
You may note, that the full path of project ':very:bestappever' is inserted at the wrong place.
This may be related to https://jira.sonarsource.com/browse/SONARGRADL-12 from which I've adopted the bestappever example by moving the :bestappever project to :very:bestappever. You can find this example at https://github.com/ThiporKong/sonarqube-gradle-issue.
Am I missing something? Are there any configuration options that can be used to work with deeply nested gradle multi-projects? I've tried the proposals from Sonarqube how to configure gradle sub-projects correctly? but to no avail.
Thanks and regards,
Thipor
Problem also reported on SonarQube user group. A ticket was created (https://jira.sonarsource.com/browse/SONARGRADL-14) and a bugfix release 2.0.1 was deployed on Gradle plugin portal.

Gradle Hello World Error

I tried my first program in Gradle but I got an error. I wrote a hello world script but I am unable to run it.
My gradle details are:
------------------------------------------------------------
Gradle 2.0
------------------------------------------------------------
Build time: 2014-07-01 07:45:34 UTC
Build number: none
Revision: b6ead6fa452dfdadec484059191eb641d817226c
Groovy: 2.3.3
Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM: 1.8.0_11 (Oracle Corporation 25.11-b03)
OS: Windows 7 6.1 x86
My build.gradle is:
task hello << {
println 'Hello world!'
}
When I run gradle -q --info hello, I get the following error:
Starting Build
Settings evaluated using empty settings script.
Projects loaded. Root project using build file 'D:\Gradle\build.gradle'.
Included projects: [root project 'Gradle']
Evaluating root project 'Gradle' using build file 'D:\Gradle\build.gradle'.
FAILURE: Build failed with an exception.
* Where:
Build file 'D:\Gradle\build.gradle' line: 1
* What went wrong:
A problem occurred evaluating root project 'Gradle'.
> Could not find property 'hello' on root project 'Gradle'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to
get more log output.
BUILD FAILED
Total time: 7.048 secs
The stacktrace is as follows:
Exception is:
org.gradle.api.GradleScriptException: A problem occurred evaluating root project
'Gradle'.
at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptR
unnerImpl.run(DefaultScriptRunnerFactory.java:54)
at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.
apply(DefaultScriptPluginFactory.java:187)
at org.gradle.configuration.project.BuildScriptProcessor.execute(BuildSc
riptProcessor.java:39)
at org.gradle.configuration.project.BuildScriptProcessor.execute(BuildSc
riptProcessor.java:26)
at org.gradle.configuration.project.ConfigureActionsProjectEvaluator.eva
luate(ConfigureActionsProjectEvaluator.java:34)
at org.gradle.configuration.project.LifecycleProjectEvaluator.evaluate(L
ifecycleProjectEvaluator.java:55)
at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProj
ect.java:470)
at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProj
ect.java:79)
at org.gradle.configuration.DefaultBuildConfigurer.configure(DefaultBuil
dConfigurer.java:31)
at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(Default
GradleLauncher.java:128)
at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradle
Launcher.java:105)
at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLaun
cher.java:85)
at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildCon
troller.run(InProcessBuildActionExecuter.java:81)
at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.jav
a:33)
at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.jav
a:24)
at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProce
ssBuildActionExecuter.java:39)
at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProce
ssBuildActionExecuter.java:29)
at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:50)
at org.gradle.internal.Actions$RunnableActionAdapter.execute(Actions.jav
a:171)
at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.
execute(CommandLineActionFactory.java:237)
at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.
execute(CommandLineActionFactory.java:210)
at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRunti
meValidationAction.java:35)
at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRunti
meValidationAction.java:24)
at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(
CommandLineActionFactory.java:206)
at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(
CommandLineActionFactory.java:169)
at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionRep
ortingAction.java:33)
at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionRep
ortingAction.java:22)
at org.gradle.launcher.Main.doAction(Main.java:33)
at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBoots
trap.java:54)
at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.j
ava:35)
at org.gradle.launcher.GradleMain.main(GradleMain.java:23)
Caused by: groovy.lang.MissingPropertyException: Could not find property 'hello'
on root project 'Gradle'.
at org.gradle.api.internal.AbstractDynamicObject.propertyMissingExceptio
n(AbstractDynamicObject.java:43)
at org.gradle.api.internal.AbstractDynamicObject.getProperty(AbstractDyn
amicObject.java:35)
at org.gradle.api.internal.CompositeDynamicObject.getProperty(CompositeD
ynamicObject.java:94)
at org.gradle.groovy.scripts.BasicScript.propertyMissing(BasicScript.jav
a:66)
at build_4hki6gadurnm5c9ejslud0e844.run(D:\Gradle\build.gradle:1)
at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptR
unnerImpl.run(DefaultScriptRunnerFactory.java:52)
... 31 more
BUILD FAILED
Total time: 26.219 secs
amicObject.java:35)
at org.gradle.api.internal.CompositeDynamicObject.getProperty(CompositeD
ynamicObject.java:94)
at org.gradle.groovy.scripts.BasicScript.propertyMissing(BasicScript.jav
a:66)
at build_4hki6gadurnm5c9ejslud0e844.run(D:\Gradle\build.gradle:1)
at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptR
unnerImpl.run(DefaultScriptRunnerFactory.java:52)
... 31 more
I know it's a little bit late, but for those who face the same problem (as you and me) i'll post my solution.
The problem is in the build.gradle encoding, i'm running windows (default encoding ansi) and my build.gradle was UTF-8, I've changed the build.gradle encoding to match my OS encoding and everything works fine now.
To resolve the issue, try to build your project first by using the command:
gradle build
Once this is done, run the task inside your gradle file by saying:
gradle -q hello
gradle -q --info hello
Hope this helps!
I too got this problem when I wrote my first gradle project. It is because of your file encoding. Do not copy the helloword project. Type these words by your self.
task helloword << {
println 'pringln hello word'
}
All I had to do was update kotlin version in gradle, I changed it from "1.2.40" to "1.3.72"
buildscript {
ext.kotlin_version = '1.3.72'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

Resources