From dc2a9ae0d7cf32ea39fd8826412b8f69d0df0f7c Mon Sep 17 00:00:00 2001 From: marko-bekhta Date: Thu, 17 Oct 2024 18:06:10 +0200 Subject: [PATCH] HV-2057 Add Sonar stage to the build --- Jenkinsfile | 65 +++++++++++++++++++++++++++++++++++++++++++++++ pom.xml | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 137 insertions(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 15eb98791..84d20fbf0 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -267,6 +267,7 @@ stage('Default build') { dir(helper.configuration.maven.localRepositoryPath) { stash name:'default-build-result', includes:"org/hibernate/validator/**" } + stash name:'default-build-jacoco-reports', includes:"**/jacoco.exec" } } } @@ -330,6 +331,62 @@ stage('Non-default environments') { } } +stage('Sonar analysis') { + def sonarCredentialsId = helper.configuration.file?.sonar?.credentials + if (sonarCredentialsId) { + runBuildOnNode { + helper.withMavenWorkspace { + if (enableDefaultBuild && enableDefaultBuildIT) { + unstash name: "default-build-jacoco-reports" + } + environments.content.jdk.enabled.each { JdkBuildEnvironment buildEnv -> + unstash name: "${buildEnv.tag}-build-jacoco-reports" + } + environments.content.wildflyTck.enabled.each { JdkBuildEnvironment buildEnv -> + unstash name: "${buildEnv.tag}-build-jacoco-reports" + } + + // we don't clean to keep the unstashed jacoco reports: + sh "mvn package -Pskip-checks -Pci-build -DskipTests -Pcoverage-report ${toTestJdkArg(environments.content.jdk.default)}" + + + // WARNING: Make sure credentials are evaluated by sh, not Groovy. + // To that end, escape the '$' when referencing the variables. + // See https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#string-interpolation + withCredentials([usernamePassword( + credentialsId: sonarCredentialsId, + usernameVariable: 'SONARCLOUD_ORGANIZATION', + // https://docs.sonarsource.com/sonarqube/latest/analyzing-source-code/scanners/sonarscanner-for-maven/#analyzing + passwordVariable: 'SONAR_TOKEN' + )]) { + // We don't want to use the build cache or build scans for this execution + def miscMavenArgs = '-Dscan=false -Dno-build-cache' + sh """ \ + mvn sonar:sonar \ + ${miscMavenArgs} \ + -Dsonar.organization=\${SONARCLOUD_ORGANIZATION} \ + -Dsonar.host.url=https://sonarcloud.io \ + -Dsonar.projectKey=hibernate_hibernate-validator \ + ${helper.scmSource.pullRequest ? """ \ + -Dsonar.pullrequest.branch=${helper.scmSource.branch.name} \ + -Dsonar.pullrequest.key=${helper.scmSource.pullRequest.id} \ + -Dsonar.pullrequest.base=${helper.scmSource.pullRequest.target.name} \ + ${helper.scmSource.gitHubRepoId ? """ \ + -Dsonar.pullrequest.provider=GitHub \ + -Dsonar.pullrequest.github.repository=${helper.scmSource.gitHubRepoId} \ + """ : ''} \ + """ : """ \ + -Dsonar.branch.name=${helper.scmSource.branch.name} \ + """} \ + """ + } + } + } + } else { + echo "Skipping Sonar report: no credentials." + } +} + } // End of helper.runWithNotification // Job-specific helpers @@ -364,6 +421,7 @@ abstract class BuildEnvironment { abstract String getTag() boolean isDefault() { isDefault } boolean requiresDefaultBuildArtifacts() { true } + boolean generatesCoverage() { true } } class JdkBuildEnvironment extends BuildEnvironment { @@ -386,6 +444,7 @@ class SigTestBuildEnvironment extends BuildEnvironment { String getTag() { "sigtest-jdk$testJavaVersion" } @Override boolean requiresDefaultBuildArtifacts() { true } + boolean generatesCoverage() { false } } void keepOnlyEnvironmentsMatchingFilter(String regex) { @@ -484,6 +543,12 @@ void mavenNonDefaultBuild(BuildEnvironment buildEnv, String args, String project $args \ """ } + + if ( buildEnv.generatesCoverage() ) { + // We allow an empty stash here since it can happen that a PR build is triggered + // but because of incremental build there will be no tests executed and no jacoco files generated: + stash name: "${buildEnv.tag}-build-jacoco-reports", includes:"**/jacoco.exec", allowEmpty: true + } } String toTestJdkArg(BuildEnvironment buildEnv) { diff --git a/pom.xml b/pom.xml index 7815fe1c0..63118c886 100644 --- a/pom.xml +++ b/pom.xml @@ -270,6 +270,7 @@ 3.3.2 2.43.0 0.8.12 + 4.0.0.4121 forbidden-junit.txt @@ -351,6 +352,19 @@ ${java-version.main.release} ${java-version.test.release} + + ${user.dir} + @@ -380,6 +394,7 @@ Allows distinguishing between multiple executions of the same test in test reports. --> default + ${surefire.environment} @@ -399,6 +414,48 @@ false apply + + + + ${rootProject.directory}/build/reports/target/site/jacoco-aggregate/jacoco.xml + + + **/org/hibernate/checkstyle/**, + **/org/hibernate/validator/build/enforcer/**, + + + + **/org/hibernate/checkstyle/**, + **/org/hibernate/validator/build/enforcer/**, + **/src/test/java/** + @@ -650,6 +707,15 @@ org.codehaus.mojo build-helper-maven-plugin + + set-root-location-property + + rootlocation + + + rootProject.directory + + parse-jakarta-validation-spec-version @@ -1517,6 +1583,11 @@ jacoco-maven-plugin ${version.jacoco.plugin} + + org.sonarsource.scanner.maven + sonar-maven-plugin + ${version.sonar.plugin} + @@ -1894,7 +1965,7 @@ failsafe.jvm.args.jacoco - ${project.build.directory}/coverage/jacoco.exec + ${project.build.directory}/${jacoco.environment.sub-directory}/coverage/jacoco.exec