From b4ed61197759c6a126cf52e36b775630328e96d2 Mon Sep 17 00:00:00 2001 From: Jisu Lim <69844138+Ji-soo708@users.noreply.github.com> Date: Tue, 26 Dec 2023 20:42:06 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=BD=94=EB=93=9C=20=EC=A0=95=EC=A0=81?= =?UTF-8?q?=20=EB=B6=84=EC=84=9D=EC=9D=84=20=EC=9C=84=ED=95=9C=20Jacoco=20?= =?UTF-8?q?=EB=B0=8F=20SonarCloud=20=EB=8F=84=EC=9E=85=20(#147)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: add jacoco setting * chore: add sonar cloud setting * chore: change sonar cloud setting * chore: integrate gradle build and SonarCloud analysis * chore: fix sonar exclusions * chore: add steps for upload test coverage * fix: fix always() in ci scripts * fix: rollback code * style: delete unused setting * chore: add sonar exclusions property path * chore: change sonar cloud setting * chore: delete dto from test coverage excludes * chore: add dto in coverage --- .github/workflows/ci-backend.yml | 23 +++++++-- build.gradle | 88 ++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.yml index 1b715124..58d95590 100644 --- a/.github/workflows/ci-backend.yml +++ b/.github/workflows/ci-backend.yml @@ -11,7 +11,8 @@ on: - develop jobs: - build: + test: + name: Code Quality Check runs-on: ubuntu-latest steps: @@ -26,5 +27,21 @@ jobs: - name: Grant execute permisson for gradlew run: chmod +x gradlew - - name: Build with Gradle - run: ./gradlew clean build + - name: Cache SonarCloud packages + uses: actions/cache@v3 + with: + path: ~/.sonar/cache + key: ${{ runner.os }}-sonar + restore-keys: ${{ runner.os }}-sonar + + - name: Setup Gradle + uses: gradle/gradle-build-action@v2 + with: + arguments: check + cache-read-only: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/develop' }} + + - name: Build and analyze + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + run: ./gradlew sonar --info --stacktrace diff --git a/build.gradle b/build.gradle index 214d4bee..d780421b 100644 --- a/build.gradle +++ b/build.gradle @@ -2,6 +2,8 @@ plugins { id 'java' id 'org.springframework.boot' version '2.7.9' id 'io.spring.dependency-management' version '1.0.15.RELEASE' + id 'jacoco' + id 'org.sonarqube' version '4.2.1.3168' } group = 'mocacong' @@ -45,6 +47,92 @@ dependencies { testImplementation 'it.ozimov:embedded-redis:0.7.2' } +jacoco { + toolVersion = '0.8.8' +} + test { useJUnitPlatform() + finalizedBy 'jacocoTestReport' +} + +def Qdomains = [] +for (qPattern in "**/QA" .. "**/QZ") { + Qdomains.add(qPattern + "*") +} + +sonar { + properties { + property 'sonar.host.url', 'https://sonarcloud.io' + property 'sonar.organization', 'mocacong' + property 'sonar.projectKey', 'mocacong_Mocacong-Backend' + property 'sonar.coverage.jacoco.xmlReportPaths', 'build/reports/jacoco/index.xml' + property 'sonar.sources', 'src' + property 'sonar.language', 'java' + property 'sonar.sourceEncoding', 'UTF-8' + property 'sonar.exclusions', '**/test/**, **/resources/**, **/*Application*.java, **/*Controller*.java ,**/config/**, **/dto/**' + + '**/exception/**, **/security/**, **/support/**, **/Q*.java' + property 'sonar.test.inclusions', '**/*Test.java' + property 'sonar.java.coveragePlugin', 'jacoco' + } +} + +jacocoTestReport { + dependsOn test + reports{ + html.required.set(true) + xml.required.set(true) + html.destination file("$buildDir/reports/jacoco/index.html") + xml.destination file("$buildDir/reports/jacoco/index.xml") + } + + afterEvaluate { + classDirectories.setFrom( + files(classDirectories.files.collect { + fileTree(dir: it, excludes: + [ + "**/*Application*", + "**/*Controller*", + "**/config/*", + "**/dto/*", + "**/exception/*", + "**/security/*", + "**/support/*" + ]+ Qdomains) + }) + ) + } + finalizedBy 'jacocoTestCoverageVerification' +} + +jacocoTestCoverageVerification { + violationRules { + rule { + failOnViolation = false + enabled = true + element = 'CLASS' + + limit { + counter = 'LINE' + value = 'COVEREDRATIO' + minimum = 0.70 + } + + limit { + counter = 'BRANCH' + value = 'COVEREDRATIO' + minimum = 0.70 + } + + excludes = [ + '**.*Application*', + '**.*Controller*', + '**.config.*', + '**.dto.*', + '**.exception.*', + '**.security.*', + '**.support.*' + ] + Qdomains + } + } }