Bepoz
파즈의 공부 일기
Bepoz
전체 방문자
오늘
어제
  • 분류 전체보기 (232)
    • 공부 기록들 (85)
      • 우테코 (17)
    • Spring (85)
    • Java (43)
    • Infra (17)
    • 책 정리 (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Bepoz

파즈의 공부 일기

Infra

[Jenkins] 간략히보는 SonarQube와 Jenkins 연동하기

2021. 8. 6. 17:25

간략히보는 SonarQube와 Jenkins 연동하기

  1. EC2를 하나 파서 도커를 설치한다.

  2. sudo apt-get update && \ sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common && \ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - && \ sudo apt-key fingerprint 0EBFCD88 && \ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" && \ sudo apt-get update && \ sudo apt-get install -y docker-ce && \ sudo usermod -aG docker ubuntu && \ sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && \ sudo chmod +x /usr/local/bin/docker-compose && \ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

  3. sonarqube를 도커위에 올린다.스크린샷 2021-08-06 오후 5 18 26 스크린샷 2021-08-06 오후 5 18 40

  4. docker run -d --name sonarqube -p 8000:9000 sonarqube //밑의 스샷은 9000:9000 인데 실제로는 8000:9000 으로 실행했음

  5. http://{sonarqube_ip}:8000/sonar 에 접속한다. 초기 id/pw는 admin/admin 이다.

  6. 토큰을 생성하고, 프로젝트를 만든다.

    image

  7. image

  8. image

  9. 젠킨스에서 SonarQube 플러그인을 설치한다.

image

  1. 젠킨스 메인화면 > Jenkins 관리 > Global Tool Configuration에서 SonarQube Scanner 를 설정해준다.

  2. image

  3. 젠킨스 메인화면 > Jenkins 관리 > 시스템 설정에서 SonarQube servers 에 소나큐브 url을 등록해준다.

  4. image

  5. 자바 프로젝트에 Jacoco 설정을 해준다.

    jacocoTestReport {
       dependsOn bootJar
    
       reports {
           html.enabled true
           xml.enabled true
           csv.enabled false
    
           html.destination file("src/jacoco/jacoco.html")
           xml.destination file("src/jacoco/jacoco.xml")
       }
    
       afterEvaluate {
           classDirectories.setFrom(files(classDirectories.files.collect {
               fileTree(dir: it,
                       exclude: [
                               '**/TyfServerApplication*'
                       ])
           }))
       }
    
       finalizedBy 'jacocoTestCoverageVerification'
    }
    
    jacocoTestCoverageVerification {
       violationRules {
           rule {
               enabled = true
               element = 'CLASS'
    
               limit {
                   counter = 'METHOD'
                   value = 'COVEREDRATIO'
                   minimum = 0.6
               }
    
               excludes = [
                       '**.*TyfServerApplication*'
               ]
           }
       }

    jacocoTestReport 에서 excludes 는 분석대상에서 제외하는 것,
    jacocoTestCoverageVerification 에서 excludes 는 테스트 대상에서 제외

  6. image

  7. 젠킨스 item 구성의 Build 탭에서 Execute SonarQube Scanner 를 등록해준다.

    image

    sonar.login = {소나큐브에서 발급한 token}
    sonar.projectKey = {소나큐브에서 설정한 key 이름}
    sonar.projectName= {소나큐브에서 설정한 project 이름}
    sonar.host.url = {소나큐브 url}
    sonar.report.export.path = sonar-report.json
    sonar.sources=server/src/main/java, server/src/main/resources
    sonar.java.coveragePlugin = jacoco
    sonar.coverage.jacoco.xmlReportPaths= server/src/jacoco/jacoco.xml (자코코 xml 생성 위치)
    sonar.test.inclusions=**/*Test.java (include 할 클래스들)
    sonar.exclusions=**/*TyfServerApplication.java, */Q*.java (exclude 할 클래스들)
    sonar.projectVersion=1.0
    sonar.sourceEncoding=UTF-8
    sonar.java.binaries=server/build/classes

    각 속성 값들에 대한 정보는 https://sonarqubekr.atlassian.net/wiki/spaces/SON/pages/388027 이곳에서 확인가능

  8. Analysis properties 에는 다음과 같이 작성

  9. image


'Infra' 카테고리의 다른 글

쿼리 튜닝 미션 적용기  (0) 2021.10.13
[Flyway] 간략히 알아보는 Flyway 적용법  (0) 2021.10.05
[DB] Mysql Order By 쿼리 ONLY_FULL_GROUP_BY 문제 해결  (0) 2021.08.06
[Nginx] 하나의 Nginx로 여러 upstream 처리하기  (2) 2021.08.04
[Jenkins] PR 라벨에 따른 젠킨스 빌드 유발 설정  (0) 2021.08.04
    'Infra' 카테고리의 다른 글
    • 쿼리 튜닝 미션 적용기
    • [Flyway] 간략히 알아보는 Flyway 적용법
    • [DB] Mysql Order By 쿼리 ONLY_FULL_GROUP_BY 문제 해결
    • [Nginx] 하나의 Nginx로 여러 upstream 처리하기
    Bepoz
    Bepoz
    https://github.com/Be-poz/TIL

    티스토리툴바