diff --git a/Jenkinsfile_CNP b/Jenkinsfile_CNP index 518958d583..b7d9c36ec2 100644 --- a/Jenkinsfile_CNP +++ b/Jenkinsfile_CNP @@ -155,9 +155,12 @@ withPipeline(type, product, component) { // hmcts/cnp-jenkins-library may fail to copy artifacts after checkstyle error so repeat command (see /src/uk/gov/hmcts/contino/GradleBuilder.groovy) builder.gradle('integration') + builder.gradle('jacocoTestReport') + builder.gradle('sonar') steps.archiveArtifacts allowEmptyArchive: true, artifacts: '**/reports/checkstyle/*.html' steps.archiveArtifacts allowEmptyArchive: true, artifacts: '**/reports/pmd/*.html' + archiveSonarArtifacts() copyIgnore('./build/reports/tests/integration', './Integration Tests/') steps.archiveArtifacts allowEmptyArchive: true, artifacts: '**/Integration Tests/**/*' @@ -251,3 +254,26 @@ withPipeline(type, product, component) { def copyIgnore(filePath, destinationDir) { steps.sh("cp -R '${filePath}' '${destinationDir}' || :") } + +def archiveSonarArtifacts() { + def sonarTaskReport = 'build/sonar/report-task.txt' + steps.archiveArtifacts allowEmptyArchive: true, artifacts: 'build/sonar/**,.scannerwork/report-task.txt' + + if (steps.fileExists(sonarTaskReport)) { + def dashboardLine = steps.readFile(sonarTaskReport).readLines().find { it.startsWith('dashboardUrl=') } + if (dashboardLine) { + steps.echo "SonarQube dashboard: ${dashboardLine.substring('dashboardUrl='.length())}" + } else { + steps.echo "Sonar report task file archived but dashboardUrl is missing in ${sonarTaskReport}" + } + } else if (steps.fileExists('.scannerwork/report-task.txt')) { + def dashboardLine = steps.readFile('.scannerwork/report-task.txt').readLines().find { it.startsWith('dashboardUrl=') } + if (dashboardLine) { + steps.echo "SonarQube dashboard: ${dashboardLine.substring('dashboardUrl='.length())}" + } else { + steps.echo "Sonar report task file archived but dashboardUrl is missing in .scannerwork/report-task.txt" + } + } else { + steps.echo 'Sonar task report not found to archive.' + } +} diff --git a/Jenkinsfile_nightly b/Jenkinsfile_nightly index 3812977cde..bf76a35014 100644 --- a/Jenkinsfile_nightly +++ b/Jenkinsfile_nightly @@ -269,6 +269,7 @@ withNightlyPipeline(type, product, component) { afterAlways('test') { // hmcts/cnp-jenkins-library may fail to copy artifacts after checkstyle error so repeat command (see /src/uk/gov/hmcts/contino/GradleBuilder.groovy) steps.archiveArtifacts allowEmptyArchive: true, artifacts: '**/reports/checkstyle/*.html' + archiveSonarArtifacts() } enableFullFunctionalTest(200) @@ -278,3 +279,26 @@ withNightlyPipeline(type, product, component) { } enableSlackNotifications('#ccd-nightly-builds') } + +def archiveSonarArtifacts() { + def sonarTaskReport = 'build/sonar/report-task.txt' + steps.archiveArtifacts allowEmptyArchive: true, artifacts: 'build/sonar/**,.scannerwork/report-task.txt' + + if (steps.fileExists(sonarTaskReport)) { + def dashboardLine = steps.readFile(sonarTaskReport).readLines().find { it.startsWith('dashboardUrl=') } + if (dashboardLine) { + steps.echo "SonarQube dashboard: ${dashboardLine.substring('dashboardUrl='.length())}" + } else { + steps.echo "Sonar report task file archived but dashboardUrl is missing in ${sonarTaskReport}" + } + } else if (steps.fileExists('.scannerwork/report-task.txt')) { + def dashboardLine = steps.readFile('.scannerwork/report-task.txt').readLines().find { it.startsWith('dashboardUrl=') } + if (dashboardLine) { + steps.echo "SonarQube dashboard: ${dashboardLine.substring('dashboardUrl='.length())}" + } else { + steps.echo "Sonar report task file archived but dashboardUrl is missing in .scannerwork/report-task.txt" + } + } else { + steps.echo 'Sonar task report not found to archive.' + } +} diff --git a/build.gradle b/build.gradle index b709391a03..9472a2c1e6 100644 --- a/build.gradle +++ b/build.gradle @@ -562,12 +562,21 @@ jsonSchema2Pojo { sonarqube { properties { + def sonarProjectKey = System.getenv("SONAR_PROJECT_KEY") ?: "ccd-data-store-api" + def sonarOrganization = System.getenv("SONAR_ORGANIZATION") ?: "hmcts" + def sonarToken = System.getenv("SONAR_TOKEN") ?: System.getenv("SONAR_AUTH_TOKEN") + property "sonar.exclusions", "build/generated-sources/**/*.java," + "**/AppInsightsConfiguration.java," + "**/TestingSupportController.java" + property "sonar.host.url", "https://sonarcloud.io" property "sonar.projectName", "ccd-data-store-api" - property "sonar.projectKey", "ccd-data-store-api" - property "sonar.coverage.jacoco.xmlReportPaths", "${jacocoTestReport.reports.xml.outputLocation}" + property "sonar.projectKey", sonarProjectKey + property "sonar.organization", sonarOrganization + if (sonarToken) { + property "sonar.token", sonarToken + } + property "sonar.coverage.jacoco.xmlReportPaths", "${buildDir}/reports/jacoco/test/jacocoTestReport.xml" } }