Sonar scanner report file with taskId and analysisId - sonarqube

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

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.

Jenkins maven project violation report in pipeline project

I am trying to convert jenkins maven project to pipeline project, we have mvn clean install step and next violation plugin can someone help me How to include violation report in pipeline project (check style and findbugs)
In declarative style, using the new Warnings Next Generation plugin, you would do something like
pipeline {
agent any
stages {
... pre-conditions & other stuff previously handled by your jenkins maven job ...
stage('Build') {
steps {
withMaven {
sh 'mvn clean install'
}
}
}
... post-conditions previously handled your jenkins maven job ...
}
post {
always {
recordIssues(
enabledForFailure: true, aggregatingResults: true,
tools: [java(), checkStyle(pattern: 'checkstyle-result.xml', reportEncoding: 'UTF-8'), findBugs(pattern: 'findbugs.xml')]
)
}
}
}
See the pipeline documentation page for more details about syntax etc

Why does Jenkins multibranch pipeline job page show junit test result for each stage of pipeline which makes count of test incorrect

I am using Jenkins Ver:2.176.3 for declarative multibranch pipeline.
I have junit test in my application code base and using maven surefire plugin to run unit test using maven command.
Multibranch job page shows 'Test Result' link and 'Test Result Trend' graph also.
I think this is being displayed/published here due to plugin 'test-results-analyzer'.
In declarative pipeline we have two stages as shown in code sample and we use maven commands.
Now my problem is that this test result count same unit test for each stage of pipeline so the count of unit tests are being double on this Test Result of Jenkins job page.
I tried skipping unit test in stage 'package-IT' using maven option -DskipTests and as per log it does skip unit testing but still see the duplicate test results.
if know please suggest
stages{
stage('compile-N-test') {
agent {label 'agent'}
steps {
script {
// mvn clean test
}
}
}
stage('packaging-IT') {
agent {label 'agent'}
steps {
script {
//mvn verify
}
}
}
Finally i got the solution: in pipeline we are using withMaven() which invoke test result and keep adding it so increasing count.

Parsing pom.xml file within a Jenkins pipeline

I am trying to parse my pom.xml in jenkins pipeline plugin. I intend to modify it and save it back.
My problem is that it gives me "unclassified field java.lang.String version"
My code is as follows:
#NonCPS
groovy.util.Node getPom(path) {
new XmlParser().parseText(readFile(path)).version
}
node {
groovy.util.Node pomNode = getPom("pom.xml")
println pomNode
}
Similar issue has been discussed here: Parsing an XML file within a Jenkins pipeline
Why not use (you need pipeline utility steps plugin for this):
pom = readMavenPom file: 'pom.xml'
Now you have access to all data in the pom (as a Model).

integrate ant junit generated test-results in gradle html report

In my gradle build script, I added doLast method to test task to run ant.junit task to execute tests from few jar files.
test << {
//run ant junit task with reports stored in $buildDir/test-results
//after ant junit completion and "test" task completion,
//how can I get the gradle generated html report include the above test-results?
}
How do I enhance this task to get the gradle html report benefit? I see that ant junit test xml reports are properly getting created in $buildDir/test-results along with other "gradle test" created xmls. However $buildDir/reports/tests" contains only. I was hoping that gradle will pick up the ant junit created test result xml files as well and include in its html report. But this is not happening. How can I get this behaviour?
I tried to create another task of type TestReport. But it also did not help.
task runTestsFromJar( type: TestReport ) {
destinationDir=file("$buildDir/reports/tests")
reportOn tasks.test, files("$buildDir/test-results/binary/test")
}
I am working with gradle 1.8.
I would suggest creating another task of type Test to replace your doAfter closure.
task antTests(type: Test){
//configuration here
}
I believe at that point you can use the TestReport task in a similar manner as your previous attempt
task runTestsFromJar( type: TestReport ) {
destinationDir=file("$buildDir/reports/tests")
reportOn test, antTests
}
My attempts to generate a report based on a directory of existing xml result files, and on the binary subfolder were unsuccessful as well
Based on the response from gradle forum post, it seems generate gradle styled test html report is not available out-of-the box with gradle. Gradle TestReport task seems to be dependent on the binary output files generated by "test" task. These are not generated when the tests are run using ant.JUnitTask from gradle.
I resorted to "ant JUnitReport task" finally to generate at least a meaningful consolidated report.
test << {
//run ant junit task with reports stored in $buildDir/test-results
//after ant junit completion
ant.junitReport( toDir: "$buildDir/reports") {
fileset ( dir:"$buildDir/test-results" )
report ( format:"frames", todir:"$buildDir/reports" )
}
}
This gives a basic html report. One can customize as needed using XSLT.

Resources